如何使用 ElementTree 在 Python 中递归迭代 XML 标签? [英] how to recursively iterate over XML tags in Python using ElementTree?

查看:40
本文介绍了如何使用 ElementTree 在 Python 中递归迭代 XML 标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ElementTree 遍历树中的所有节点.

我会这样做:

 tree = ET.parse("/tmp/test.xml")root = tree.getroot()对于 root 中的孩子:###和孩子一起做点什么

问题是 child 是一个 Element 对象而不是 ElementTree 对象,所以我不能进一步研究它并递归迭代它的元素.有没有办法对根"进行不同的迭代,以便它迭代树中的顶级节点(直接子节点)并返回与根本身相同的类?

解决方案

要遍历所有节点,请使用 iter 方法/library/xml.etree.elementtree.html#elementtree-objects" rel="nofollow noreferrer">ElementTree,不是根元素.

根是一个元素,就像树中的其他元素一样,只有它自己的属性和子元素的上下文.ElementTree 具有所有元素的上下文.

例如,给定这个 xml

<数据><国家名称=列支敦士登"><rank>1</rank><年>2008</年><gdppc>141100</gdppc><邻居名称=奥地利"方向=E"/><邻居名称=瑞士"方向=W"/></国家><国家名称=新加坡"><rank>4</rank><年>2011</年><gdppc>59900</gdppc><邻居名称=马来西亚"方向=N"/></国家><国家名称=巴拿马"><rank>68</rank><年>2011</年><gdppc>13600</gdppc><邻居名称=哥斯达黎加"方向=W"/><邻居名称=哥伦比亚"方向=E"/></国家></数据>

您可以执行以下操作

<预><代码>>>>导入 xml.etree.ElementTree 作为 ET>>>tree = ET.parse('test.xml')>>>对于 tree.iter() 中的元素:... 打印元素...<0x10b2d7b50处的元素'数据'><0x10b2d7b90处的元素国家"><0x10b2d7bd0处的元素等级"><0x10b2d7c50 处的元素年份"><0x10b2d7d10 处的元素gdppc"><0x10b2d7e90 处的元素邻居"><0x10b2d7ed0 处的元素邻居"><0x10b2d7f10处的元素国家"><0x10b2d7f50处的元素等级"><0x10b2d7f90 处的元素年份"><0x10b2d7fd0 处的元素gdppc"><0x10b2db050 处的元素邻居"><0x10b2db090 处的元素国家"><0x10b2db0d0处的元素等级"><0x10b2db110 处的元素年份"><0x10b2db150 处的元素gdppc"><0x10b2db190 处的元素邻居"><0x10b2db1d0 处的元素邻居">

I am trying to iterate over all nodes in a tree using ElementTree.

I do something like:

  tree = ET.parse("/tmp/test.xml")

  root = tree.getroot()

  for child in root:
       ### do something with child

The problem is that child is an Element object and not ElementTree object, so I can't further look into it and recurse to iterate over its elements. Is there a way to iterate differently over "root" so that it iterates over the top level nodes in the tree (immediate children) and return the same class as root itself?

解决方案

To iterate over all nodes, use the iter method on the ElementTree, not the root Element.

The root is an Element, just like the other elements in the tree and only really has context of its own attributes and children. The ElementTree has the context for all Elements.

For example, given this 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>

You can do the following

>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('test.xml')
>>> for elem in tree.iter():
...     print elem
... 
<Element 'data' at 0x10b2d7b50>
<Element 'country' at 0x10b2d7b90>
<Element 'rank' at 0x10b2d7bd0>
<Element 'year' at 0x10b2d7c50>
<Element 'gdppc' at 0x10b2d7d10>
<Element 'neighbor' at 0x10b2d7e90>
<Element 'neighbor' at 0x10b2d7ed0>
<Element 'country' at 0x10b2d7f10>
<Element 'rank' at 0x10b2d7f50>
<Element 'year' at 0x10b2d7f90>
<Element 'gdppc' at 0x10b2d7fd0>
<Element 'neighbor' at 0x10b2db050>
<Element 'country' at 0x10b2db090>
<Element 'rank' at 0x10b2db0d0>
<Element 'year' at 0x10b2db110>
<Element 'gdppc' at 0x10b2db150>
<Element 'neighbor' at 0x10b2db190>
<Element 'neighbor' at 0x10b2db1d0>

这篇关于如何使用 ElementTree 在 Python 中递归迭代 XML 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆