Python xml.etree.ElementTree解析XML文件

(一)简介
  1. xml.etree.ElementTree模块实现了一个简单而高效的API用于解析和创建XML数据。xml.etree.ElementTree模块对于恶意构造的数据是不安全的。如果您需要解析不受信任或未经验证的数据,请参阅XML漏洞。
  2. 参考文献:https://docs.python.org/3.6/library/xml.etree.elementtree.html
1.XML tree and elements

XML是一种固有的分层数据格式,最自然的表示方法是使用树。ET有两个类为此目的—ElementTree表示整个XML文档为树,元素表示此树中的单个节点。与整个文档的交互(读取和写入文件)通常是在ElementTree级别上完成的。与单个XML元素及其子元素的交互是在元素级别上完成的。

2.解析XML文件

我们将使用以下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>

我们可以通过读取文件来导入这些数据:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

作为一个元素,root有一个标签和一个属性字典:

>>> root.tag
'data'
>>> root.attrib
{}

它也有子节点,我们可以迭代:

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}

孩子节点是嵌套的,我们可以通过索引访问特定的子节点:


>>> root[0][1].text
'2008'

注:并不是所有的XML元素输入将作为解析数元素的结尾,目前这个模块跳过了所有XML注解、处理指令和文档类型声明。然而创建tree使用的是该模块的API,而不是解析XML文本。通过将自定义TreeBuilder实例传递给XMLParser构造函数,可以访问文档类型声明。

3.获取XML中的元素

Element有一些有用的方法,可以帮助递归地遍历它下面的所有子树(它的子树,它们的子树,等等)比如:Element.iter():

>>> for neighbor in root.iter('neighbor'):
...     print(neighbor.attrib)
...
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}

1.Element.findall(): 只找到带有标签的元素,该标签是当前元素的直接子元素。 2.Element.find() :找到第一个带有特定标签的子元素。
3. Element.text:访问标签的内容
4. Element.get():访问标签的属性值

>>> for country in root.findall('country'):
...     rank = country.find('rank').text
...     name = country.get('name')
...     print(name, rank)
...
Liechtenstein 1
Singapore 4
Panama 68
4.修改XML文件

ElementTree提供了一种构建XML文档并将xml写入文件的简单方法。
1.ElementTree.write() :创建xml文件或向xml中写入数据。
2.Element.set():添加和修改标签的属性和属性值。
3.Element.append():添加孩子节点

假设我们想在每个国家的排名中添加一个,并将更新后的属性添加到rank元素:

>>> for rank in root.iter('rank'):
...     new_rank = int(rank.text) + 1
...     rank.text = str(new_rank)
...     rank.set('updated', 'yes')
...
>>> tree.write('output.xml')

修改后XML是这样的:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

我们使用Element.remove()删除标签,假设我们想要移除所有排名高于50的国家:

>>> for country in root.findall('country'):
...     rank = int(country.find('rank').text)
...     if rank > 50:
...         root.remove(country)
...
>>> tree.write('output.xml')

删除满足条件标签后XML是这样的:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
</data>
5.创建XML文档

1.SubElement():用于创建新的子元素。

>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> c = ET.SubElement(a, 'c')
>>> d = ET.SubElement(c, 'd')
>>> ET.dump(a)
<a><b /><c><d /></c></a>
6.解析XML名称空间

If the XML input has namespaces, tags and attributes with prefixes in the form prefix:sometag get expanded to {uri}sometag where the prefix is replaced by the full URI. Also, if there is a default namespace, that full URI gets prepended to all of the non-prefixed tags.

这里有一个XML示例它包含两个名称空间,一个是前缀“ fictional”另一个是默认的名称空间:

<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example.com"
        xmlns="http://people.example.com">
    <actor>
        <name>John Cleese</name>
        <fictional:character>Lancelot</fictional:character>
        <fictional:character>Archie Leach</fictional:character>
    </actor>
    <actor>
        <name>Eric Idle</name>
        <fictional:character>Sir Robin</fictional:character>
        <fictional:character>Gunther</fictional:character>
        <fictional:character>Commander Clement</fictional:character>
    </actor>
</actors>

搜索名称空间XML示例的更好方法是使用自己的前缀创建字典,并在搜索函数中使用这些前缀:

ns = {'real_person': 'http://people.example.com',
      'role': 'http://characters.example.com'}

for actor in root.findall('real_person:actor', ns):
    name = actor.find('real_person:name', ns)
    print(name.text)
    for char in actor.findall('role:character', ns):
        print(' |-->', char.text)

输出结果:

John Cleese
 |--> Lancelot
 |--> Archie Leach
Eric Idle
 |--> Sir Robin
 |--> Gunther
 |--> Commander Clement
7.对XPath的支持

xml.etree.ElementTree 模块对XPath表达式在树中定位元素提供了有限的支持。它支持一小部分缩写语法,完整的XPath引擎不在模块的范围内。

下面的示例演示了模块的一些XPath功能。我们将使用解析XML部分中的countrydata XML文档:

import xml.etree.ElementTree as ET

root = ET.fromstring(countrydata)

# Top-level elements
root.findall(".")

# All 'neighbor' grand-children of 'country' children of the top-level
# elements
root.findall("./country/neighbor")

# Nodes with name='Singapore' that have a 'year' child
root.findall(".//year/..[@name='Singapore']")

# 'year' nodes that are children of nodes with name='Singapore'
root.findall(".//*[@name='Singapore']/year")

# All 'neighbor' nodes that are the second child of their parent
root.findall(".//neighbor[2]")
01.支持XPath语法

如图所示:
这里写图片描述


(二)参考API
1.函数

1.xml.etree.ElementTree.Comment(text=None):注释元素工厂,这个工厂函数创建一个特殊的元素,将被序列化为XML注释标准的序列化器。注释字符串可以是bytestring或Unicode字符串,文本是包含注释字符串的字符串,返回表示注释的元素实例。

注意:XMLParser忽略了输入中的注释,而不是为它们创建注释对象。如果ElementTree使用其中一个元素方法插入到树中,它只会包含注释节点。

2.xml.etree.ElementTree.dump(elem):把元素树或元素结构写入sys.stdout。这个函数只用于调试。

3.xml.etree.ElementTree.fromstring(text):从字符串常量解析XML,XML()方法解析的方式也一样,它们返回的都是一个Element实例。

4.xml.etree.ElementTree.iselement(element):检查对手是否是有效的element对象,如果是element对象返回true,否则返回false.

5.xml.etree.ElementTree.iterparse(source, events=None, parser=None):在元素树中逐步解析XML,并且报告用户的情况。资源是文件名或包含xml数据的文件对象。一系列的事件需要被报告,支持事件的字符串有 “start”, “end”, “start-ns” and “end-ns”,如果忽略事件,则只报告“end”事件。解析器是一个可选的解析器实例。如果没有给出,则使用标准的XMLParser解析器。解析器必须是XMLParser的子类,并且只能使用默认的TreeBuilder作为目标。使用iterparse()函数返回一个迭代器对象。

6.xml.etree.ElementTree.parse(source, parser=None):把XML文件解析成 element tree,资源是一个文件名或包含XML数据的文件对象,解析器是一个可选的解析器实例。如果没有指定parser的参数值,默认使用的是XMLParser解析器。调用此函数返回ElementTree实例对象。

7.xml.etree.ElementTree.ProcessingInstruction(target, text=None):返回一个元素实例,表示一个处理指令。

8.xml.etree.ElementTree.register_namespace(prefix, uri):注册命名空间前缀, registry是全局的,任何现有的前缀或名称空间URI的映射都将被删除。这个命名空间中的标签和属性将被给定的前缀给序列化。

9.xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra):此函数是一个Subelement工厂,这个函数用于创建 element 实例,并将其添加到现有的 element 中。

10.xml.etree.ElementTree.tostring(element, encoding=“us-ascii”, method=“xml”, *, short_empty_elements=True): 转化为字符串。
01.element:表示一个element实例
02. encoding:默认编码是"us-ascii"
03.method:默认是"xml",可以选择“html”、“text”

11.xml.etree.ElementTree.tostringlist(element, encoding=“us-ascii”, method=“xml”, *, short_empty_elements=True):转化成字符串列表。

12.xml.etree.ElementTree.XML(text, parser=None):
01.text :包含xml数据的字符串。
02.parser:解析器默认是XMLParser
03.返回的是一个Element实例

13.xml.etree.ElementTree.XMLID(text, parser=None):返回一个包含Element实例和字典的元组。

2.Element 对象
class xml.etree.ElementTree.Element(tag, attrib={}, **extra)

1.tag: 标签
2.text: 去除标签,获得标签中的内容。
3.attrib: 获取标签中的属性和属性值。
4.tail: 这个属性可以用来保存与元素相关联的附加数据。它的值通常是字符串,但可能是特定于应用程序的对象。

Element 对象的方法

1.clear():清除所有子元素和所有属性,并将文本和尾部属性设置为None。

2.get(attribute_name, default=None):通过指定属性名获取属性值。

3.items():以键值对的形式返回元素属性。

4.keys():以列表的方式返回元素名。

5.set(attribute_name,attribute_value):在某标签中设置属性和属性值。

6.append(subelement):将元素子元素添加到元素的子元素内部列表的末尾。

7.extend(subelements):追加子元素。

8.find(match, namespaces=None):找到第一个匹配的子元素,match可以是标签名或者path。返回Elememt实例或None。

9.findall(match, namespaces=None):找到所有匹配的子元素,返回的是一个元素列表。

10.findtext(match, default=None, namespaces=None):找到匹配第一个子元素的文本。返回的是匹配元素中的文本内容。

11.getchildren():Python3.2后使用 list(elem) 或 iteration.

12.getiterator(tag=None):Python3.2后使用 Element.iter()

13.iter(tag=None):以当前元素为根创建树迭代器。迭代器遍历这个元素和它下面的所有元素(深度优先级)。如果标签不是None或’*’,那么只有标签等于标签的元素才会从迭代器返回。如果在迭代过程中修改树结构,则结果是未定义的。

14.iterfind(match, namespaces=None):匹配满足条件的子元素,返回元素。

15.itertext():创建一个文本迭代器。迭代器循环遍历此元素和所有子元素,以文档顺序,并返回所有内部文本。

16.makeelement(tag, attrib):此方法使用SubElement()函数代替。

17.remove(subelement):删除子元素。

3.ElementTree 对象

class xml.etree.ElementTree.ElementTree(element=None, file=None):

ElementTree是一个包装器类,这个类表示一个完整的元素层次结构,并为标准XML的序列化添加了一些额外的支持。

1._setroot(element):替换根元素,原来的根元素中的内容会消失。

2.find(match, namespaces=None):从根元素开始匹配和 Element.find()作用一样。

3.findall(match, namespaces=None):从根元素开始匹配和 Element.findall()作用一样。

4.findtext(match, default=None, namespaces=None):从根元素开始匹配和 Element.findtext()作用一样。

5.getiterator(tag=None):Python3.2后使用 ElementTree.iter() 代替。

6.iter(tag=None):迭代所有元素

7.iterfind(match, namespaces=None):从根元素开始匹配和 Element.iterfind()作用一样。

8.parse(source, parser=None):解析xml文本,返回根元素。

9.write(file, encoding=“us-ascii”, xml_declaration=None, default_namespace=None, method=“xml”, *, short_empty_elements=True):写出XML文本。

  • 73
    点赞
  • 372
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值