-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_xml_sax.py
54 lines (46 loc) · 1.56 KB
/
read_xml_sax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import xml.sax
'''
XML.SAX provides methods to read xml data from a file. This can't be used to
modify the xml file.
'''
class SampleData(xml.sax.ContentHandler):
def startElement(self, name, attrs):
'''
This method over-rides the startElement method of the
xml.sax.ContentHandler class.
'''
self.current = name
if self.current == 'country':
print('-----Country-----')
print('Country Name: {}'.format(attrs['name']))
if self.current == 'neighbor':
self.neighbor = attrs['name']
def characters(self, content):
'''
This method over-rides the characters method of the
xml.sax.ContentHandler class.
'''
if self.current == 'rank':
self.rank = content
elif self.current == 'year':
self.year = content
elif self.current == 'gdppc':
self.gdppc = content
def endElement(self, name):
'''
This method over-rides the endElement method of the
xml.sax.ContentHandler class.
'''
if self.current == 'rank':
print('Rank: {}'.format(self.rank))
elif self.current == 'year':
print('Year: {}'.format(self.year))
elif self.current == 'gdppc':
print('GDP PC: {}'.format(self.gdppc))
elif self.current == 'neighbor':
print('Neighbor: {}'.format(self.neighbor))
self.current = ''
handler = SampleData()
parser = xml.sax.make_parser()
parser.setContentHandler(handler)
parser.parse('sample_data.xml')