python3读取xml字符串
1import xml.sax
2import xml.sax.handler
3import pprint
4
5
6class XMLHandler(xml.sax.handler.ContentHandler):
7def__init__(self):
8 self.buffer = ""python处理xml文件
9 self.mapping = {}
10
11def startElement(self, name, attributes):
12 self.buffer = ""
13
14def characters(self, data):
15 self.buffer += data
16
17def endElement(self, name):
18 self.mapping[name] = self.buffer
19
20def getDict(self):
21return self.mapping
22
23
24 data = '<?xml version="1.0" encoding="UTF-8"?><note><to>World</to><from>Linvo</from><heading>Hi</heading><body>Hello World!</body></note>' 25
26 xh = XMLHandler()
27 xml.sax.de(), xh)
28 ret = xh.getDict()
29
30 pprint.pprint(ret)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论