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.