gpt4 book ai didi

python - python中xml的GetTreeNode

转载 作者:数据小太阳 更新时间:2023-10-29 02:10:22 26 4
gpt4 key购买 nike

<?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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com