xml数据解析

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

if __name__ == '__main__':
    data = '''
    
      
        {0}
        333323213232
        ZF0025_01_001
        00006450563
        1.0
        ZF0025010011000100
        core_merchant_no format error:格式校验未通过
        F
      
    '''
    xh = XMLHandler()
    xml.sax.parseString(data, xh)
    ret = xh.getDict()
    pprint.pprint(ret)

你可能感兴趣的:(xml数据解析)