I'm using python to parse a XML file but I have a problem. I'm getting the values in form of a dictionary but if there are two or more same values then they are not repeating. I'm sure there is a way to solve it but I'm new on python and parsing XML...
Here is an example of XML:
<Root>
<Child1>
</Child1>
<Child2>
<Data DId = "1">
<Group ID = "">
<Sport Name="Cricket" Team="6" />
<Sport Name="Football" Team="6" />
<Sport Name="Hockey" Team="5" />
</Group>
</Data>
<Data DId = "2">
<Group ID = "">
<Sport Name="Rugby" Team="6" />
<Sport Name="Baseball" Team="10" />
<Sport Name="Swimming" Team="6" />
</Group>
</Data>
</Child2>
</Root>
I want to get Sport's tag value separated by Data. The code I have tried is:
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
dict1 = {}
for i in root.iter('Sport'):
dict1[i.attrib['Name']] = [j.text for j in i]
dict1[i.attrib['Team']] = [k.text for k in i]
print(dict1)
But I am not able to get Team value for each sport.