温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用python标准库ElementTree处理xml

发布时间:2022-05-20 14:23:51 来源:亿速云 阅读:212 作者:iii 栏目:开发技术

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI