GetTreeNode of a 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 above , need print treenode of xml shown below xml, im beginner in python , dont know how achive this, please can in solve

thanks in advance

i not know direct apis can give result, can recursively print each node , attribs , children , same thing there.

example -

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

for xml 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 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 

Comments