-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml_sax.py
55 lines (41 loc) · 1.32 KB
/
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
import requests
import xml.sax
class MyContentHandler(xml.sax.ContentHandler):
def __init__(self):
self.slideCount = 0
self.itemCount = 0
self.isInTitle = False
# Handle startElement
def startElement(self, tagName, attrs):
if tagName == "slideshow":
print("Slideshow title: " + attrs["title"])
elif tagName == "slide":
self.slideCount += 1
elif tagName == "item":
self.itemCount += 1
elif tagName == "title":
self.isInTitle = True
# Handle endElement
def endElement(self, tagName):
if tagName == "title":
self.isInTitle = False
# Handle text data
def characters(self, chars):
if self.isInTitle:
print("Title: " + chars)
# Handle startDocument
def startDocument(self):
print("About to Start")
# Handle endDocument
def endDocument(self):
print("Finishing up!")
# main function
handler = MyContentHandler()
url = "http://httpbin.org/xml"
response = requests.get(url)
# print(response.text)
# Call the parseString method on the XML text content recieved
xml.sax.parseString(response.text, handler)
# print slide and item count
print("slides count {}".format(handler.slideCount))
print("slides item count {}".format(handler.itemCount))