In [1]:
from xml.etree import ElementTree as ET
In [3]:
document_tree = ET.parse( './data/mondial_database_less.xml' )
In [4]:
# print names of all countries
for child in document_tree.getroot():
print child.find('name').text
In [5]:
# print names of all countries and their cities
for element in document_tree.iterfind('country'):
print '* ' + element.find('name').text + ':',
capitals_string = ''
for subelement in element.getiterator('city'):
capitals_string += subelement.find('name').text + ', '
print capitals_string[:-2]
Using data in 'data/mondial_database.xml', the examples above, and refering to https://docs.python.org/2.7/library/xml.etree.elementtree.html, find
In [6]:
document = ET.parse( './data/mondial_database.xml' )