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

yipeiwu_com5年前Python基础

本文实例讲述了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中出现有嵌套节点的名称来解决,否则只有重写复杂的机制了。

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python初学之用户登录的实现过程(实例讲解)

要求编写登录接口: 1. 输入用户名和密码 2.认证成功后显示欢迎信息 3.用户名输错,提示用户不存在,重新输入(5次错误,提示尝试次数过多,退出程序) 4.用户名正确,密码错误,提示...

详解python中自定义超时异常的几种方法

最近在项目中调用第三方接口时候,经常会出现请求超时的情况,或者参数的问题导致调用异代码异常。针对超时异常,查询了python 相关文档,没有并发现完善的包来根据用户自定义的时间来抛出超时...

django框架基于queryset和双下划线的跨表查询操作详解

django框架基于queryset和双下划线的跨表查询操作详解

本文实例讲述了django框架基于queryset和双下划线的跨表查询操作。分享给大家供大家参考,具体如下: 前面篇随笔写的是基于对象的跨表查询:对象.objects.filter(。。...

python中的迭代和可迭代对象代码示例

什么是迭代(iteration)呢? 给定一个list或者tuple,通过for循环来遍历这个list或者tuple、这种遍历就是迭代(iteration)。只要是可迭代的对象都可以进行...

Python 复平面绘图实例

Python 复平面绘图实例

前言 在学校太闲,就写了这个程序,可以在复平面绘制曲线,画圆什么的很轻松,f(z) = e^(1j * z),螺旋线,函数图象等都可以。 效果图 说明 此程序使用turtle绘图,可以...