欢迎光临
我们一直在努力

怎么用python标准库ElementTree处理xml

本篇内容介绍了“怎么用python标准库ElementTree处理xml”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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"语句的&lsquo;<',如下:

<?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”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注云搜网网站,小编将为大家输出更多高质量的实用文章!

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。