您的位置:首页 > 编程语言 > Python开发

Python 解析xml文件

2015-01-25 13:29 281 查看
python有三种方法解析XML,分别是SAX,DOM,以及ElementTree,其中ElmentTree比较容易使用,其API比较方便友好。代码可用性好,速度快,消耗内存少。

xml中的元素主要有:tag,value,attribute

 


一个简单的python 解析xml的例子如下:

Xml文件为:
<?xml version="1.0" encoding="utf-8"?>
<info>
<list id='001' price="20" width="60">
<head>auto_userone</head>
<name>Jordy</name>
<number>12345678</number>
</list>

<list id='002' price="20" width="60">
<head>auto_usertwo</head>
<name>tester</name>
<number>34443678</number>
</list>
</info>

Python代码为:

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as et

def print_node(node):
print "==================================="
print "node.tag:%s" % node.tag
print "node.text:%s" % node.text
print "node.attribute: %s" %node.attrib

#读取xml文件
def load_xml_file(fileName):
root =et.parse(fileName)
nodes = root.getiterator("list")
for node in nodes:
print_node(node)

if __name__ == '__main__':
load_xml_file(r'sample.xml')




我们可以看到 xml的Attribute是一个字典,因此也可以用字典的方法遍历Attribute,获取具体属性的值。

def print_node(node):
print "==================================="
print "node.tag:%s" % node.tag
print "node.text:%s" % node.text
#print "node.attr:%s" % node.attrib
mapAttrib=node.attrib
for key in mapAttrib:
print "%s=%s" %(key,mapAttrib[key])

如果知道具体attribute 的名字,也可以用get方法获取属性的值:

def print_node(node):
print "==================================="
print "node.tag:%s" % node.tag
print "node.text:%s" % node.text
print "id:%s" % node.get("id")

如何遍历xml的元素呢? ElementTree提供了四种方法,这四种方法分别是getiterator,getchildren,find 和findall,具体使用方法如下:

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as et

def print_node(node): print "===================================" print "node.tag:%s" % node.tag print "node.text:%s" % node.text print "id:%s" % node.get("id")
#读取xml文件
def load_xml_file(fileName):
root =et.parse(fileName)

#使用getiterator
nodes = root.getiterator("list")
for node in nodes:
print_node(node)

#使用getchildren()
children = nodes[0].getchildren()
for child in children:
print_node(child)

#使用find方法
specNode =root.find("list")
if specNode is not None:
print_node(specNode)

#使用findall方法
node_findall = root.findall("list/name")[1]
print_node(node_findall)

if __name__ == '__main__':
load_xml_file(r'F:\python\Advance\xml\sample.xml')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: