In [1]:
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
In [2]:
root.tag
Out[2]:
In [3]:
root.attrib
Out[3]:
In [4]:
for child in root:
print child.tag, child.attrib
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
In [29]:
for series in root2.iter('DataSet'):
print series.attrib
In [15]:
for child in root2:
print child.tag, child.attrib
In [21]:
for country in root.findall('country'):
rank = country.find('rank').text
name = country.get('name')
print name, rank
In [28]:
for country in root2.findall('kf:Series'):
rank = country.find('CURRENCY').text
name = country.get('FREQ')
print name, rank
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 [ ]: