python标准库ElementTree处理xml

Felcia ·
更新时间:2024-09-20
· 215 次阅读

目录

1. 示例用法

Element对象具有如下属性和操作

遇到非法格式的xml

ExpatError: no element found

ExpatError: mismatched tag

ExpatError: not well-formed(invalid token)

1. 示例用法

参照官方文档,创建country_data.xml测试文档,内容如下:

<?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>

使用如下代码,将数据读出,打印

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为空文档时,内容如下:

<?xml version="1.0"?>

执行如下python代码,遇到xml.parser.expat.ExpatError异常:

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中找不到对应结束标记符时,内容如下:

<?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中属性值未包含在双引号(&quot;)之中时,遇到如下异常:

<?xml version="1.0"?> <note id=hello> </note>

bad.xml中非法符号,在"if salary < 1000 then"语句的‘<',如下:

<?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的资料请关注软件开发网其它相关文章!



XML Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号