-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-xml.py
More file actions
31 lines (21 loc) · 688 Bytes
/
16-xml.py
File metadata and controls
31 lines (21 loc) · 688 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
29
30
31
import xml.sax
import xml.sax.handler
import pprint
class XMLHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.buffer = ""
self.mapping = {}
def startElement(self, name, attributes):
self.buffer = ""
def characters(self, data):
self.buffer += data
def endElement(self, name):
self.mapping[name] = self.buffer
def getDict(self):
return self.mapping
data = '<?xml version="1.0" encoding="UTF-8"?><note><to>World</to><from>Linvo</from><heading>Hi</heading><body>Hello ' \
'World!</body></note> '
xh = XMLHandler()
xml.sax.parseString(data.encode(), xh)
ret = xh.getDict()
pprint.pprint(ret)