Python解析XML字符串,網上查了半天,還就內置的xml.sax輕量級、簡便。
鑒於網上的代碼哪怕是最簡單的示例,也帶有很大迷惑性,最後還是自己整理備份一份為好,對於大多數普通情況直接用即可,無須修改。
BTW:還是JSON好用。
- # -*- coding: utf-8 -*-
-
- import xml.sax
- import xml.sax.handler
-
- 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, xh)
- ret = xh.getDict()
-
- import pprint
- pprint.pprint(ret)