I was following the documentation of how to use xml.etree to parse data from an xml file, but it seems that important information seem to be missing.
I am using the same example:
<?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>
and for each country I am trying to get the year associated to that country. I tried the following code:
import sys
import xml.etree.ElementTree as ET
tree = ET.parse(sys.argv[1])
root = tree.getroot()
for child in root:
print(child.tag, child.attrib. child.get('year')) # or child['year'], or child.find('year').text
but none of these seem to work. How do I extract the value for year for each of the three countries?
Expected output:
country {'name': 'Liechtenstein'} 2008
country {'name': 'Singapore'} 2011
country {'name': 'Panama'} 2011
Addendum:
I found a way to get the 'year':
import sys
import xml.etree.ElementTree as ET
tree = ET.parse(sys.argv[1])
root = tree.getroot()
for child in root:
for elem in list(child):
if elem.tag == 'year':
print(child.tag, child.attrib, elem.text)
Is there no simpler way?