I am having the below XML file:
<hierachy>
<att>
<Order>1</Order>
<attval>Data</attval>
<children>
<att>
<Order>1</Order>
<attval>Studyval</attval>
</att>
<att>
<Order>2</Order>
<attval>Site</attval>
</att>
</children>
</att>
<att>
<Order>2</Order>
<attval>Info</attval>
<children>
<att>
<Order>1</Order>
<attval>age</attval>
</att>
<att>
<Order>2</Order>
<attval>gender</attval>
</att>
</children>
</att>
</hierachy>
I want to convert it to a CSV file like this:
Data,Studyval
Date,Site
Info,age
Info,gender
The main problem is that both the parent and child names are the same - 'att' and 'attval'. How can I distinguish between them both and give me the output?
I have followed the below code but it is printing the same things twice:
import xml.etree.cElementTree as ET
tree = ET.parse('input.xml')
rebase = tree.getroot()
list = []
for att in rebase.findall('att'):
name = att.find('attval').text
for each_att in att.findall('attval'):
try:
val = att.find('attval').text
print name, val
except AttributeError:
print name