xml.etree.ElementTree模块的作用
基于事件和基于文档的APID来解析XML,可以使用XPath表达式搜索已解析的文件,具有对文档的增删改查的功能,该方式需要注意大xml文件,因为是一次性加载到内存,
所以如果是大xml文件,不推荐使用该模块解析,应该使用sax方式。
测试解析的内容
1
2008
141100
4
2011
59900
68
2011
13600
test.xml
1、解析xml文档
from xml.etree importElementTree
with open('test.xml', 'tr', encoding='utf-8') as rf:
tree=ElementTree.parse(rf)print(tree)
ElementTree_parse_xml.py
运行结果
#返回ElementTree对象
2、遍历解析XML树,获取节点名字
from xml.etree importElementTree
with open('test.xml', 'tr', encoding='utf-8') as rf:
tree=ElementTree.parse(rf)for node intree.iter():print(node.tag)
ElementTree_dump_xml.py
运行结果
#打印所有节点名字
data
country
rank
year
gdppc
neighbor
neighbor
country
rank
year
gdppc
neighbor
country
rank
year
gdppc
neighbor
neighbor
3、遍历解析XML树,获取属性值
from xml.etree importElementTree
with open('test.xml', 'tr', encoding='utf-8') as rf:
tree=ElementTree.parse(rf)for node in tree.iter('neighbor'):
attr_name= node.attrib.get('name')
attr_direction= node.attrib.get('direction')#如果两个值都不为空,则打印两个值,否则打印一个值
if attr_name andattr_direction:print('{:<25}{:<25}'.format(attr_name, attr_direction))else:print('{:<25}'.format(attr_name))
ElementTree_show_name_direction.py
运行结果
Austria E
Switzerland W
Malaysia N
Costa Rica W
Colombia E
4、利用XPath在XML文档中查找节点
from xml.etree importElementTree
with open('test.xml', 'tr', encoding='utf-8') as rf:
tree=ElementTree.parse(rf)for node in tree.findall('.//neighbor'):
name= node.attrib.get('name')ifname:print(name)
ElementTree_find_feeds_by_tag.py
运行结果
Austria
Switzerland
Malaysia
Costa Rica
Colombia
5、利用XPath在XML文档中查找更深一层的节点
from xml.etree importElementTree
with open('test.xml', 'tr', encoding='utf-8') as rf:
tree=ElementTree.parse(rf)for node in tree.findall('.//neighbor/neighbor'):
name= node.attrib.get('name')ifname:print(name)
ElementTree_find_feeds_by_structure.py
运行结果
Malaysia
6、利用XPath表达式,查询节点的属性名和值
from xml.etree importElementTree
with o