python - python中xml的GetTreeNode

标签 python 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>

/data   
/data/country   
/data/country@name  Liechtenstein
/data/country/rank  68
/data/country/year  2011
/data/country/gdppc 13600
/data/country/neighbor  
/data/country/neighbor@direction    E
/data/country/neighbor@name Austria

我有一个像上面这样的简单 XML,我需要打印 xml 下面显示的那个 xml 的 treeNode,我是 python 的初学者,不知道如何实现这个,请有人帮助解决这个问题

提前致谢

最佳答案

我不知道有什么直接的 API 可以给你结果,但你可以递归地打印每个节点及其属性,然后获取它的子节点并在那里做同样的事情。

例子-

def walker(root, str):
    print(str+root.tag, (root.text and root.text.strip()) or '')
    for attrib in root.attrib:
            print(str+root.tag+'@'+attrib,root.attrib[attrib])
    for child in root:
            walker(child,str+root.tag+'/')

对于像下面这样的 xml -

>>> s = """<?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
>>> r = ET.fromstring(s)

这给了我-

>>> walker(r,'/')
/data
/data/country
/data/country@name Liechtenstein
/data/country/rank 1
/data/country/year 2008
/data/country/gdppc 141100
/data/country/neighbor
/data/country/neighbor@direction E
/data/country/neighbor@name Austria
/data/country/neighbor
/data/country/neighbor@direction W
/data/country/neighbor@name Switzerland
/data/country
/data/country@name Singapore
/data/country/rank 4
/data/country/year 2011
/data/country/gdppc 59900
/data/country/neighbor
/data/country/neighbor@direction N
/data/country/neighbor@name Malaysia
/data/country
/data/country@name Panama
/data/country/rank 68
/data/country/year 2011
/data/country/gdppc 13600
/data/country/neighbor
/data/country/neighbor@direction W
/data/country/neighbor@name Costa Rica
/data/country/neighbor
/data/country/neighbor@direction E
/data/country/neighbor@name Colombia

关于python - python中xml的GetTreeNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31532734/

相关文章:

python - 如何使用 Python 获取电子邮件文本内容?

python - 中断外部循环 Python 函数其他选项

python - Django 的序列数据库?

python - 单引号字符串与双引号字符串

java - 获取像 "Two classes have the same XML type name..."这样的 JAXB 异常

xml - && 在 XML 中(逻辑操作)

python - Blobstore 上传处理程序时 Google App Engine 中的编码问题

regex - 如何使用 sed 修复 xml 问题

java - 如何在两种情况下都具有根元素的 Jersey 中获取 JSON 和 XML 响应?

python - 如何将 Pandas 数据框转换为 XML?