1 2008 141100

GetTreeNode xml in python

<?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 simple XML as above and I need to print the treeNode of this xml shown below xml, im new to python, dont know how to achieve this, please someone can help solve this

Thank you in advance

+3


source to share


1 answer


I don't know of any direct APIs that can give you a result, but you can recursively print out each node and its attributes and then get your children and do the same.

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 xml as shown 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

      

+1


source







All Articles