python dom解析xml 总结

最近开发了一个程序,需要解析xml,因此写了个小demo学习研究

一、xml.dom 解析XML的API描述

minidom.parse(filename)
加载读取XML文件

doc.documentElement
获取XML文档对象

node.getAttribute(AttributeName)
获取XML节点属性值

node.getElementsByTagName(TagName)
获取XML节点对象集合

node.childNodes #返回子节点列表。

node.childNodes[index].nodeValue
获取XML节点值


node.firstChild
#访问第一个节点。等价于pagexml.childNodes[0]

 

doc = minidom.parse(filename)
doc.toxml('UTF-8')
返回Node节点的xml表示的文本


Node.attributes["id"]
a.name #就是上面的 "id"
a.value #属性的值 
访问元素属性

二、直接上代码

2.1 如下是安卓手机源码中的一段xml文件,String.xml

<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
       <!-- Settings item for boolean option to enable/disable MMS delivery reports -->
    <string name="pref_title_mms_delivery_reports">Request delivery report</string>
    <!-- Settings item for boolean option to enable/disable MMS group messages -->
    <string name="pref_title_mms_group_mms">Group messaging</string>
    <!-- Settings item for boolean option to enable/disable MMS read reports -->
    <string name="pref_title_mms_read_reports">Request read report</string>
    <!-- Settings item for boolean option to enable/disable SMS delivery reports -->
    <string name="pref_title_sms_delivery_reports">Request delivery report</string>
    <!-- Settings item for boolean option to enable/disable auto-deletion of messages -->
    <string name="pref_title_auto_delete">Delete old messages</string>
    <!-- Settings item for integer option to set SMS delete limit -->
  </resources>


2.2 解析此xml

#!/usr/bin/python
from  xml.dom import  minidom


def get_attrvalue(node, attrname):
     return node.getAttribute(attrname) if node else ''

def get_nodevalue(node, index = 0):
    return node.childNodes[index].nodeValue if node else ''

def get_xmlnode(node,name):
    return node.getElementsByTagName(name) if node else []

def xml_to_string(filename='String.xml'):
    doc = minidom.parse(filename)
    return doc.toxml('UTF-8')

def get_xml_data(filename='/home/android/sprd4.4/demo1/src/com/demo1/String.xml'):
    doc = minidom.parse(filename) 
    root = doc.documentElement

    string_nodes = get_xmlnode(root,'string')

    for node in string_nodes :
	print node 
	nodeName = node.childNodes[0].data
        mms_name = get_attrvalue(node,'name') 

	print "mms_name ===== %s===== nodeName======%s===nodeString=== %s" % (mms_name,node.nodeName,nodeName)


if __name__ == "__main__":

    get_xml_data()
    



后续更新其它的解析方法

你可能感兴趣的:(xml,dom解析xml)