GetTreeNode of a xml in python

2020-07-17 15:42发布

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

I have a simple XML like above and i need to print treeNode of that xml shown below the xml, im a beginner in python , dont know how to achive this, please someone can help in this to solve

Thanks in advance

标签: python xml
1条回答
家丑人穷心不美
2楼-- · 2020-07-17 16:18

I do not know any direct APIs that can give you the result, but you can recursively print each node and its attribs and then get its children and do the same thing there.

Example -

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+'/')

For an xml like below -

>>> 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)

This gives me -

>>> 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
查看更多
登录 后发表回答