python自定义解析简单xml格式文件

因为公司内部的接口返回的字串支持2种形式:php数组,xml;结果php数组python不能直接用,而xml字符串的格式不是标准的,所以也不能用标准模块解析。【不标准的地方是某些节点会的名称是以数字开头的】,所以写个简单的脚步来解析一下文件,用来做接口测试。

#!/usr/bin/env python  
#encoding: utf-8
import re

class xmlparse:
    
    def __init__(self, xmlstr):
        self.xmlstr = xmlstr
        self.xmldom = self.__convet2utf8()
        self.xmlnodelist = []
        self.xpath = ''
        
    def __convet2utf8(self):
        headstr = self.__get_head()
        xmldomstr = self.xmlstr.replace(headstr, '')
        if 'gbk' in headstr:
            xmldomstr = xmldomstr.decode('gbk').encode('utf-8')
        elif 'gb2312' in headstr:
            xmldomstr = self.xmlstr.decode('gb2312').encode('utf-8')
        return xmldomstr
    
    def __get_head(self):
        headpat = r'<\?xml.*\?>'
        headpatobj = re.compile(headpat)
        headregobj = headpatobj.match(self.xmlstr)  
        if headregobj:
            headstr = headregobj.group()
            return headstr
        else:
            return ''
    
        
    def parse(self, xpath): 
        self.xpath = xpath
        xpatlist = []
        xpatharr = self.xpath.split('/')
        for xnode in xpatharr:
            if xnode:
                spcindex = xnode.find('[')
                if spcindex > -1:
                    index = int(xnode[spcindex+1:-1])
                    xnode = xnode[:spcindex]            
                else:
                    index = 0;
                temppat = ('<%s>(.*?)</%s>' % (xnode, xnode),index)               
                xpatlist.append(temppat)
        
        xmlnodestr = self.xmldom
        for xpat,index in xpatlist:
            xmlnodelist = re.findall(xpat,xmlnodestr) 
            xmlnodestr = xmlnodelist[index]
            if xmlnodestr.startswith(r'<![CDATA['):
                xmlnodestr = xmlnodestr.replace(r'<![CDATA[','')[:-3]
                
        self.xmlnodelist = xmlnodelist
        return xmlnodestr        
                
                
if '__main__' == __name__:
    xmlstr = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?><resultObject><a><product_id>aaaaa</product_id><product_name><![CDATA[bbbbb]]></a><b><product_id>bbbbb</product_id><product_name><![CDATA[bbbbb]]></b></product_name></resultObject>'                
    xpath1 = '/product_id'
    xpath2 = '/product_id[1]'
    xpath3 = '/a/product_id'
    xp = xmlparse(xmlstr)  
    print 'xmlstr:',xp.xmlstr
    print 'xmldom:',xp.xmldom  
    print '------------------------------'
    getstr = xp.parse(xpath1)
    print 'xpath:',xp.xpath      
    print 'get list:',xp.xmlnodelist     
    print 'get string:', getstr                 
    print '------------------------------'
    getstr = xp.parse(xpath2)
    print 'xpath:',xp.xpath      
    print 'get list:',xp.xmlnodelist     
    print 'get string:', getstr         
    print '------------------------------'
    getstr = xp.parse(xpath3)
    print 'xpath:',xp.xpath      
    print 'get list:',xp.xmlnodelist     
    print 'get string:', getstr  
                  

运行结果:

xmlstr: <?xml version="1.0" encoding="utf-8" standalone="yes" ?><resultObject><a><product_id>aaaaa</product_id><product_name><![CDATA[bbbbb]]></a><b><product_id>bbbbb</product_id><product_name><![CDATA[bbbbb]]></b></product_name></resultObject>
xmldom: <resultObject><a><product_id>aaaaa</product_id><product_name><![CDATA[bbbbb]]></a><b><product_id>bbbbb</product_id><product_name><![CDATA[bbbbb]]></b></product_name></resultObject>
------------------------------
xpath: /product_id
get list: ['aaaaa', 'bbbbb']
get string: aaaaa
------------------------------
xpath: /product_id[1]
get list: ['aaaaa', 'bbbbb']
get string: bbbbb
------------------------------
xpath: /a/product_id
get list: ['aaaaa']
get string: aaaaa

因为返回的xml格式比较简单,没有带属性的节点,所以处理起来就比较简单了。但测试还是发现有一个bug。即当相同节点嵌套时会出现正则匹配出问题,该问题的可以通过避免在xpath中出现有嵌套节点的名称来解决,否则只有重写复杂的机制了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上帝De助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值