I am trying to parse a xml file and arrange it into a table separating the contents as isElement, isAttribute, Value, Text.
How do I use ElementTree module to achieve this? I know this is possible using the minidom module.
The reason I want to use ElementTree is due to is effencicy. An exmaple of what I am trying to achive is available here: http://python.zirael.org/e-gtk-treeview4.html
Any advice on how to seprate the xml contents into element, subelemnt etc. using the ElementTree module?
This is what I have so far:
import xml.etree.cElementTree as ET
filetree = ET.ElementTree(file = "some_file.xml")
for child in filetree.iter():
print child.tag, child.text, child.attrib
For the following example xml file:
<?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>
I get this as output:
data
{}
country
{'name': 'Liechtenstein'}
rank 1 {}
year 2008 {}
gdppc 141100 {}
neighbor None {'direction': 'E', 'name': 'Austria'}
neighbor None {'direction': 'W', 'name': 'Switzerland'}
country
{'name': 'Singapore'}
rank 4 {}
year 2011 {}
gdppc 59900 {}
neighbor None {'direction': 'N', 'name': 'Malaysia'}
country
{'name': 'Panama'}
rank 68 {}
year 2011 {}
gdppc 13600 {}
neighbor None {'direction': 'W', 'name': 'Costa Rica'}
neighbor None {'direction': 'E', 'name': 'Colombia'}
I did find something simialr on another post but it uses the DOM module. Walk through all XML nodes in an element-nested structure
Based on the comment received, this is what I want to achieve:
data (type Element)
country(Element)
Text = None
name(Attribute)
value: Liechtenstein
rank(Element)
Text = 1
year(Element)
Text = 2008
gdppc(Element)
Text = 141100
neighbour(Element)
name(Attribute)
value: Austria
direction(Attribute)
value: E
neighbour(Element)
name(Attribute)
value: Switzerland
direction(Attribute)
value: W
country(Element)
Text = None
name(Attribute)
value: Singapore
rank(Element)
Text = 4
I want to be able to presente my data in a tree like structure as above. To do this I need to keeep track of their relationship. Hope this clarifies the question.