#!/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
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有