After checking the web for half a day, the built-in xml. sax is lightweight and simple.
Given that even the simplest examples of code on the Internet are confusing, it is better to organize and back up a copy by yourself. In most common cases, you can use it directly without modifying it.
BTW: JSON is easy to use, NND!
[Python] www.2cto.com
#-*-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>
Xh = XMLHandler ()
Xml. sax. parseString (data, xh)
Ret = xh. getDict ()
Import pprint
Pprint. pprint (ret)
Author: linvo