1. 示例用法
参照官方文档,创建country_data.xml测试文档,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<? xml version = "1.0" ?> < data > < country name = "Liechtenstein" > < rank >1</ rank > < year >2008</ year > < gdppc >141100</ gdppc > < neighbor name = "Austria" direction = "E" /> < neighbor name = "Switzerland" direction = "W" /> </ country > < country name = "Singapore" > < rank >4</ rank > < year >2011</ year > < gdppc >59900</ gdppc > < neighbor name = "Malaysia" direction = "N" /> </ country > < country name = "Panama" > < rank >68</ rank > < year >2011</ year > < gdppc >13600</ gdppc > < neighbor name = "Costa Rica" direction = "W" /> < neighbor name = "Colombia" direction = "E" /> </ country > </ data > |
使用如下代码,将数据读出,打印
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from xml.etree.ElementTree data = ElementTree.ElementTree( file = 'country_data.xml' ) country_list = data.findall( 'country' ) #找到所有名为‘country'的tag,返回一个Element对象列表。 for country in country_list: name = country.attrib.get( 'name' , '') print name, ' ' , for item in country: if item.tag = = 'neighbor' : name = item.attrib.get( 'name' , '') direction = item.attrib.get( 'direction' , '') print '{0} ({1})' . format (name, direction), ' ' , else : print item.text, ' ' , print '' |
其中
data = ElementTree.ElementTree(file='country_data.xml')
获得一个ElementTree对象,也可以使用
tree = ElementTree.parse('country_data.xml')
Element对象具有如下属性和操作
elem.tag | 这个Element对象的名字(tag) |
elem.text | 文档内容 |
elem.attrib | 属性值字典 |
elem.tail | 与属性一起存储的其他数据 |
elem[n] 返回elem的第n个子元素
elem[n] = new_elem 将elem的第n个子元素更改为不同的元素new_elem
del elem[n] 删除子元素
len(elem) 子元素的数量
elem.find(path)
elem.getchildren() 按文档顺序返回所有子元素
elem.items()将所有元素的属性值以(name, value)对列表形式返回
遇到非法格式的xml
ExpatError: no element found
bad.xml为空文档时,内容如下:
1
|
<? xml version = "1.0" ?> |
执行如下python代码,遇到xml.parser.expat.ExpatError异常:
1
2
|
import xml.etree.ElementTree as ET ET.parse( 'bad.xml' ) |
xml.parsers.expat.ExpatError: no element found: line 3, column 0
ExpatError: mismatched tag
bad.xml中找不到对应结束标记符时,内容如下:
1
2
3
|
<? xml version = "1.0" ?> < note > </ Note > |
因为区分大小写,所以</Note> 不能作为<note>的结束标记。
xml.parsers.expat.ExpatError: mismatched tag: line 3, column 2
ExpatError: not well-formed(invalid token)
bad.xml中属性值未包含在双引号(")之中时,遇到如下异常:
1
2
3
|
<? xml version = "1.0" ?> < note id = hello > </ note > |
bad.xml中非法符号,在"if salary < 1000 then"语句的‘<',如下:
1
2
3
4
|
<? xml version = "1.0" ?> < note id = "hello" > if salary < 1000 then </note |
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 2, column 9
以上就是python标准库ElementTree处理xml的详细内容,更多关于python ElementTree处理xml的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/wangst4321/article/details/8697838