-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_sax.py
More file actions
28 lines (28 loc) · 776 Bytes
/
xml_sax.py
File metadata and controls
28 lines (28 loc) · 776 Bytes
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
#!/usr/bin/python
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
#begin bookHandler
class bookHandler(ContentHandler):
inAuthor = False
inTitle = False
def startElement(self, name, attributes):
if name == "book":
print( "*****Book*****")
if name == "title":
self.inTitle = True
print("Title: ",)
if name == "author":
self.inAuthor = True
print("Author: ",)
def endElement(self, name):
if name == "title":
self.inTitle = False
if name == "author":
self.inAuthor = False
def characters(self, content):
if self.inTitle or self.inAuthor:
print(content)
#end bookHandler
parser = make_parser()
parser.setContentHandler(bookHandler())
parser.parse("library.xml")