python中xml的GetTreeNode 1. 2008 141100 4. 2011 59900 68 2011 13600 /资料 /数据/国家 /资料/country@name列支敦士登 /数据/国家/排名68 /2011年数据/国家/年度 /数据/国家/gdppc 13600 /数据/国家/邻居 /数据/国家/neighbor@directionE /数据/国家/neighbor@name奥地利
我有一个像上面这样的简单XML,我需要打印XML下面显示的treeNode,我是python的初学者,不知道如何实现这一点,请有人可以帮助解决这个问题python中xml的GetTreeNode 1. 2008 141100 4. 2011 59900 68 2011 13600 /资料 /数据/国家 /资料/country@name列支敦士登 /数据/国家/排名68 /2011年数据/国家/年度 /数据/国家/gdppc 13600 /数据/国家/邻居 /数据/国家/neighbor@directionE /数据/国家/neighbor@name奥地利,python,xml,Python,Xml,我有一个像上面这样的简单XML,我需要打印XML下面显示的treeNode,我是python的初学者,不知道如何实现这一点,请有人可以帮助解决这个问题 提前感谢我不知道有什么直接API可以提供结果,但您可以递归地打印每个节点及其属性,然后获取其子节点并在那里执行相同的操作 范例- <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank>
提前感谢我不知道有什么直接API可以提供结果,但您可以递归地打印每个节点及其属性,然后获取其子节点并在那里执行相同的操作 范例-
<?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-
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+'/')
如何从treeNode反向转换为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