Python parse XML file operation instance _python

Source: Internet
Author: User

This article illustrates the implementation of Python parsing XML file operations. Share to everyone for your reference. The specific methods are as follows:

The contents of the XML file are as follows:

<?xml version= "1.0"?> 
<!--simple XML document__chapter 8--> 
<book> 
  <title> 
    Sample XML thing 
  </title> 
  <author> 
    <name> 
      <first> 
        ma 
      </ first> 
      <last> 
        xiaoju 
      </last> 
    </name> 
    <affiliation> 
      Springs Widgets, Inc. 
    </affiliation> 
  </author> 
  <chapter number= "1" > 
    <title> 
    </title> 
    <para> 
      I widgets are greate. Should buy lots of them forom 
      <company> 
        spirngy widgts, Inc 
      </company> 
    </para > 
  </chapter> 
</book> 

Python code:

From xml.dom import minidom, Node import Re, TextWrap class Samplescanner: "" "" "Def __init__ (self, doc): "" Constructor "" "Assert (Isinstance doc, Minidom.)  Document)) for the child in doc.childNodes:if Child.nodetype = = Node.element_node and \ child.tagname = = "Book": Self.handle_book [child] def handle_book (self, node): For child in Node.childnode S:if child.nodetype!= Node.ELEMENT_NODE:continue If child.tagname = "title": Print "Bo" 
      OK Titile is: ", Self.gettext (child.childnodes) If child.tagname = =" Author ": Self.handle_author (Child) if Child.tagname = = "chapter": Self.handle_chapter (Child) def handle_chapter (Self, node): n 
    Umber = Node.getattribute ("number") print "number:", Number Title_node = Node.getelementsbytagname ("title") 
     Print "title:", Self.gettext (Title_node) for child in Node.childnodes: If Child.nodetype!= Node.ELEMENT_NODE:continue if child.tagname = = "Para": Self.handle_chapter _para (Child) def handle_chapter_para (Self, node): Company = "" Company = Self.gettext (node.geteleme 
    Ntsbytagname ("Company") print "Chapter:para:company", company def Handle_author (Self, node):  For child in Node.childNodes:if child.nodetype!= Node.ELEMENT_NODE:continue if child.tagname = = ' Name ': Self.handle_author_name (child) if child.tagname = = "Affiliation": print "affiliation:", Self.gettext (child.childnodes) def handle_author_name (Self, node): the "" Last = "" For CH ILD in Node.childNodes:if child.nodetype!= Node.ELEMENT_NODE:continue If child.tagname = "I ": Self.gettext (child.childnodes) If child.tagname = ' last ': last = Self.gettext (child.c 
         
  Hildnodes)  Print "firstname:%s,lastname:%s"% (I, last) def gettext (self, nodelist): Retlist = [] For node in nodelist:if Node.nodetype = = Node.TEXT_NODE:retlist.append (node.wholetext) elif node. HasChildNodes:retlist.append (Self.gettext (node.childnodes)) return re.sub (' \s+ ', ' ", ', '. Join (ret 

 List) if __name__== "__main__": Doc = Minidom.parse ("simple.xml") sample = Samplescanner (DOC)

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.