In [2]:
import xml.etree.ElementTree as ET

In [4]:
tree = ET.parse('country_data.xml')

In [5]:
root = tree.getroot()

In [7]:
root.tag


Out[7]:
'data'

In [8]:
root.attrib


Out[8]:
{}

In [9]:
for child in root:
    print child.tag, child.attrib


country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}

In [10]:
root[0][1].text


Out[10]:
'2008'

In [11]:
for neighbor in root.iter('neighbor'):
    print neighbor.attrib


{'direction': 'E', 'name': 'Austria'}
{'direction': 'W', 'name': 'Switzerland'}
{'direction': 'N', 'name': 'Malaysia'}
{'direction': 'W', 'name': 'Costa Rica'}
{'direction': 'E', 'name': 'Colombia'}

In [12]:
for country in root.findall('country'):
    rank = country.find('rank').text
    name = country.get('name')
    print name, rank


Liechtenstein 1
Singapore 4
Panama 68

In [ ]: