In [1]:
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

In [2]:
root.tag


Out[2]:
'data'

In [3]:
root.attrib


Out[3]:
{}

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


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

In [4]:


In [4]:


In [4]:


In [4]:


In [4]:


In [25]:
tree2 = ET.parse('Z1_data.xml')
root2 = tree2.getroot()

In [26]:
for child in root2:
    print child.tag, child.attrib


{http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message}Header {}
{http://www.federalreserve.gov/structure/compact/common}DataSet {'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'http://www.federalreserve.gov/structure/compact/Z1_Z1 Z1_Z1.xsd', 'id': 'Z1'}
{http://www.federalreserve.gov/structure/compact/common}DataSet {'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'http://www.federalreserve.gov/structure/compact/Z1_OTHER Z1_OTHER.xsd', 'id': 'OTHER'}

In [29]:
for series in root2.iter('DataSet'):
    print series.attrib

In [15]:
for child in root2:
    print child.tag, child.attrib


{http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message}Header {}
{http://www.federalreserve.gov/structure/compact/common}DataSet {'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'http://www.federalreserve.gov/structure/compact/Z1_Z1 Z1_Z1.xsd', 'id': 'Z1'}
{http://www.federalreserve.gov/structure/compact/common}DataSet {'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'http://www.federalreserve.gov/structure/compact/Z1_OTHER Z1_OTHER.xsd', 'id': 'OTHER'}

In [21]:
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 [28]:
for country in root2.findall('kf:Series'):
    rank = country.find('CURRENCY').text
    name = country.get('FREQ')
    print name, rank


  File "<string>", line unknown
SyntaxError: prefix 'kf' not found in prefix map

In [51]:
x = 0
namespaces = {'frb': 'xmlns:frb="http://www.federalreserve.gov/structure/compact/common/frb#','kf': 'xmlns:kf="http://www.federalreserve.gov/structure/compact/Z1_Z1/kf#'} # add more as needed
# for elem in tree.iterfind('kf:Series', namespaces):
#     x+=1
#     print elem.tag, elem.attrib
# print x

root.findall('frb:DataSet/kf:Series', namespaces)


Out[51]:
[]

In [ ]:
namespaces = {'owl': 'http://www.w3.org/2002/07/owl#'} # add more as needed

root.findall('owl:Class', namespaces)

In [52]:
def parser(file_name):
    document = etree.parse(file_name)
#     titles = document.findall('.//title')
    Series = document.findall('.//{http://www.mediawiki.org/xml/export-0.7/}kf:Series')
    print Series

In [ ]: