python解析xml模块封装代码_python

有如下的xml文件:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?> 
<root> 
<childs> 
<child name='first' >1</child> 
<child value="2">2</child> 
</childs> 
</root>

下面介绍python解析xml文件的几种方法,使用python模块实现。

方式1,python模块实现自动遍历所有节点:

复制代码 代码如下:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
from xml.sax.handler import ContentHandler 
from xml.sax import parse
class TestHandle(ContentHandler): 
    def __init__(self, inlist): 
        self.inlist = inlist 

    def startElement(self,name,attrs): 
        print 'name:',name, 'attrs:',attrs.keys() 

    def endElement(self,name): 
        print 'endname',name 

    def characters(self,chars): 
        print 'chars',chars 
        self.inlist.append(chars) 

             
if __name__ == '__main__': 
    lt = [] 
    parse('test.xml', TestHandle(lt)) 
    print lt

结果:
[html] view plaincopy
name: root attrs: [] 
chars  

name: childs attrs: [] 
chars  

name: child attrs: [u'name'] 
chars 1 
endname child 
chars  

name: child attrs: [u'value'] 
chars 2 
endname child 
chars  

endname childs 
chars  

endname root 
[u'\n', u'\n', u'1', u'\n', u'2', u'\n', u'\n']

方式2,python模块实现获取根节点,按需查找指定节点:

复制代码 代码如下:

#!/usr/bin/env python   
# -*- coding: utf-8 -*-   
from xml.dom import minidom   
xmlstr = '''''<?xml version="1.0" encoding="UTF-8"?>
<hash>
    <request name='first'>/2/photos/square/type.xml</request>
    <error_code>21301</error_code>
    <error>auth faild!</error>
</hash>
''' 
def doxml(xmlstr): 
    dom = minidom.parseString(xmlstr)     
    print 'Dom:'     
    print dom.toxml()   

    root = dom.firstChild     
    print 'root:'     
    print root.toxml()   

    childs = root.childNodes   
    for child in childs: 
        print child.toxml() 
        if child.nodeType == child.TEXT_NODE: 
            pass 
        else: 
            print 'child node attribute name:', child.getAttribute('name') 
            print 'child node name:', child.nodeName 
            print 'child node len:',len(child.childNodes) 
            print 'child data:',child.childNodes[0].data 
            print '=======================================' 
            print 'more help info to see:' 
            for med in dir(child): 
                print help(med)     

               
if __name__ == '__main__':   
    doxml(xmlstr)

结果:
[html] view plaincopy
Dom: 
<?xml version="1.0" ?><hash> 
    <request name="first">/2/photos/square/type.xml</request> 
    <error_code>21301</error_code> 
    <error>auth faild!</error> 
</hash> 
root: 
<hash> 
    <request name="first">/2/photos/square/type.xml</request> 
    <error_code>21301</error_code> 
    <error>auth faild!</error> 
</hash> 

<request name="first">/2/photos/square/type.xml</request> 
child node attribute name: first 
child node name: request 
child node len: 1 
child data: /2/photos/square/type.xml 
======================================= 
more help info to see: 
两种方法各有其优点,python的xml处理模块太多,目前只用到这2个。

=====补充分割线================
实际工作中发现python的mimidom无法解析其它编码的xml,只能解析utf-8的编码,而其xml文件的头部申明也必须是utf-8,为其它编码会报错误。
网上的解决办法都是替换xml文件头部的编码申明,然后转换编码为utf-8再用minidom解码,实际测试为可行,不过有点累赘的感觉。

本节是 python解析xml模块封装代码 的第二部分。
====写xml内容的分割线=========

复制代码 代码如下:

#!\urs\bin\env python 
#encoding: utf-8 
from xml.dom import minidom 

class xmlwrite: 
    def __init__(self, resultfile): 
        self.resultfile = resultfile 
        self.rootname = 'api' 
        self.__create_xml_dom() 

    def __create_xml_dom(self): 
        xmlimpl = minidom.getDOMImplementation() 
        self.dom = xmlimpl.createDocument(None, self.rootname, None) 
        self.root = self.dom.documentElement 

    def __get_spec_node(self, xpath): 
        patharr = xpath.split(r'/') 
        parentnode = self.root 
        exist = 1 
        for nodename in patharr: 
            if nodename.strip() == '': 
                continue 
            if not exist: 
                return None 
            spcindex = nodename.find('[') 
            if spcindex > -1: 
                index = int(nodename[spcindex+1:-1]) 
            else: 
                index = 0 
            count = 0 
            childs = parentnode.childNodes 
            for child in childs: 
                if child.nodeName == nodename[:spcindex]: 
                    if count == index: 
                        parentnode = child 
                        exist = 1 
                        break 
                    count += 1 
                    continue 
                else: 
                    exist = 0 
        return parentnode 

         
    def write_node(self, parent, nodename, value, attribute=None, CDATA=False): 
        node = self.dom.createElement(nodename) 
        if value: 
            if CDATA: 
                nodedata = self.dom.createCDATASection(value) 
            else: 
                nodedata = self.dom.createTextNode(value) 
            node.appendChild(nodedata) 
            if attribute and isinstance(attribute, dict): 
                for key, value in attribute.items(): 
                    node.setAttribute(key, value)    
        try: 
            parentnode = self.__get_spec_node(parent) 
        except: 
            print 'Get parent Node Fail, Use the Root as parent Node' 
            parentnode = self.root 
        parentnode.appendChild(node) 

     
    def write_start_time(self, time): 
        self.write_node('/','StartTime', time) 

    def write_end_time(self, time): 
        self.write_node('/','EndTime', time)     

    def write_pass_count(self, count): 
        self.write_node('/','PassCount', count)    

    def write_fail_count(self, count): 
        self.write_node('/','FailCount', count)    

    def write_case(self): 
        self.write_node('/','Case', None)    

    def write_case_no(self, index, value): 
        self.write_node('/Case[%s]/' % index,'No', value) 

    def write_case_url(self, index, value): 
        self.write_node('/Case[%s]/' % index,'URL', value) 

    def write_case_dbdata(self, index, value): 
        self.write_node('/Case[%s]/' % index,'DBData', value) 

    def write_case_apidata(self, index, value): 
        self.write_node('/Case[%s]/' % index,'APIData', value) 

    def write_case_dbsql(self, index, value): 
        self.write_node('/Case[%s]/' % index,'DBSQL', value, CDATA=True) 

    def write_case_apixpath(self, index, value): 
        self.write_node('/Case[%s]/' % index,'APIXPath', value)        

    def save_xml(self): 
        myfile = file(self.resultfile, 'w') 
        self.dom.writexml(myfile, encoding='utf-8') 
        myfile.close() 

if __name__ == '__main__': 
      xr = xmlwrite(r'D:\test.xml') 
      xr.write_start_time('2223') 
      xr.write_end_time('444')       
      xr.write_pass_count('22') 
      xr.write_fail_count('33')   
      xr.write_case() 
      xr.write_case() 
      xr.write_case_no(0, '0') 
      xr.write_case_url(0, 'http://www.google.com')    
      xr.write_case_url(0, 'http://www.google.com')    
      xr.write_case_dbsql(0, 'select * from ') 
      xr.write_case_dbdata(0, 'dbtata') 
      xr.write_case_apixpath(0, '/xpath') 
      xr.write_case_apidata(0, 'apidata') 
      xr.write_case_no(1, '1')        
      xr.write_case_url(1, 'http://www.baidu.com')    
      xr.write_case_url(1, 'http://www.baidu.com')    
      xr.write_case_dbsql(1, 'select 1 from ') 
      xr.write_case_dbdata(1, 'dbtata1') 
      xr.write_case_apixpath(1, '/xpath1') 
      xr.write_case_apidata(1, 'apidata1') 
      xr.save_xml()

以上封装了minidom,支持通过xpath来写节点,不支持xpath带属性的匹配,但支持带索引的匹配。
比如:/root/child[1], 表示root的第2个child节点。

时间: 2024-08-21 08:17:29

python解析xml模块封装代码_python的相关文章

python 解析XML python模块xml.dom解析xml实例代码_python

一 .python模块 xml.dom 解析XML的APIminidom.parse(filename)加载读取XML文件 doc.documentElement获取XML文档对象 node.getAttribute(AttributeName)获取XML节点属性值 node.getElementsByTagName(TagName)获取XML节点对象集合 node.childNodes #返回子节点列表. node.childNodes[index].nodeValue获取XML节点值 nod

用Python解析XML的几种常见方法的介绍_python

一.简介        XML(eXtensible Markup Language)指可扩展标记语言,被设计用来传输和存储数据,已经日趋成为当前许多新生技术的核心,在不同的领域都有着不同的应用.它是web发展到一定阶段的必然产物,既具有SGML的核心特征,又有着HTML的简单特性,还具有明确和结构良好等许多新的特性.         python解析XML常见的有三种方法:一是xml.dom.*模块,它是W3C DOM API的实现,若需要处理DOM API则该模块很适合,注意xml.dom包

Python解析xml中dom元素的方法_python

本文实例讲述了Python解析xml中dom元素的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: from xml.dom import minidom try:     xmlfile = open("path.xml", "a+")     #xmldoc = minidom.parse( sys.argv[1])     xmldoc = minidom.parse(xmlfile) except :     #updatelogger.

python解析xml文件实例分析

  本文实例讲述了python解析xml文件的方法.分享给大家供大家参考.具体如下: python解析xml非常方便.在dive into python中也有讲解. 如果xml的结构如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?xml version="1.0" encoding="utf-8"?> <books> <book> <author>zoer</a

程序员-python解析xml文件如何读取&amp;amp;lt;br /&amp;amp;gt;标签的内容?

问题描述 python解析xml文件如何读取<br />标签的内容? Xml文本如下: 想读取标签claim-text的内容,代码如下: from xml.dom import minidom doc = minidom.parse("201410447057NEW.xml") root = doc.documentElement claimtext = root.getElementsByTagName("claim-text") print clai

php DOMDocument 创建,加载,解析xml文档代码

 代码如下 复制代码 $xml_str = "<?xml version='1.0'?>"; $xml_str .= "<books>"; $xml_str .= "<book>"; $xml_str .= "<title>harry potter</title>"; $xml_str .= "<author>j.k.rowling</au

python网页请求urllib2模块简单封装代码_python

对python网页请求模块urllib2进行简单的封装. 例子: 复制代码 代码如下: #!/usr/bin/python#coding: utf-8import base64import urllibimport urllib2import time class SendRequest:  '''  This class use to set and request the http, and get the info of response.  e.g. set Authorization

使用python解析xml成对应的html示例分享_python

SAX将dd.xml解析成html.当然啦,如果得到了xml对应的xsl文件可以直接用libxml2将其转换成html. 复制代码 代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*-#---------------------------------------#   程序:XML解析器#   版本:01.0#   作者:mupeng#   日期:2013-12-18#   语言:Python 2.7#   功能:将xml解析成对应的html#

深入解读Python解析XML的几种方式

在XML解析方面,Python贯彻了自己"开箱即用"(batteries included)的原则.在自带的标准库中,Python提供了大量可以用于处理XML语言的包和工具,数量之多,甚至让Python编程新手无从选择. 本文将介绍深入解读利用Python语言解析XML文件的几种方式,并以笔者推荐使用的ElementTree模块为例,演示具体使用方法和场景.文中所使用的Python版本为2.7. 什么是XML? XML是可扩展标记语言(Extensible Markup Languag