python parsing XML file Operations examples, common techniques for manipulating XML files .
XML file Contents:
<?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> First</title> <para>I think widgets is greate. You should buy lots of them forom<company>Spirngy Widgts, Inc.</company> </para> </chapter> </book>
Python code:
fromXml.domImportMinidom, NodeImportRe, TextWrap # # www.jbxue.comclassSamplescanner:""""""def __init__(self, doc):"""Constructor"""assert(Isinstance (Doc, Minidom. Document)) forChildinchDoc.childnodes:ifChild.nodetype = = Node.element_node and\ Child.tagname==" Book": Self.handle_book (Child)defHandle_book (Self, node): forChildinchNode.childnodes:ifChild.nodetype! =Node.element_node:ContinueifChild.tagname = ="title": Print "Book Titile is:", Self.gettext (child.childnodes)ifChild.tagname = ="author": Self.handle_author (Child)ifChild.tagname = ="Chapter": Self.handle_chapter (Child)defHandle_chapter (Self, node): number= Node.getattribute (" Number") Print "Number :", number Title_node= Node.getelementsbytagname ("title") Print "Title:", Self.gettext (Title_node) forChildinchNode.childnodes:ifChild.nodetype! =Node.element_node:ContinueifChild.tagname = ="para": Self.handle_chapter_para (Child)defHandle_chapter_para (Self, node): Company="" Company= Self.gettext (Node.getelementsbytagname (" Company")) Print "Chapter:para:company", CompanydefHandle_author (Self, node): forChildinchNode.childnodes:ifChild.nodetype! =Node.element_node:ContinueifChild.tagname = ="name": Self.handle_author_name (Child)ifChild.tagname = ="Affiliation": Print "Affiliation:", Self.gettext (child.childnodes)defHandle_author_name (Self, node): First="" Last="" forChildinchNode.childnodes:ifChild.nodetype! =Node.element_node:ContinueifChild.tagname = =" First": First=Self.gettext (child.childnodes)ifChild.tagname = =' Last': Last=Self.gettext (child.childnodes)Print "firstname:%s,lastname:%s"%(First, last)defGetText (Self, nodelist): Retlist= [] forNodeinchnodelist:ifNode.nodetype = =Node.TEXT_NODE:retlist.append (Node.wholetext)elifnode.hasChildNodes:retlist.append (Self.gettext (node.childnodes))returnRe.sub ('\s+'," ","'. Join (retlist))if __name__=="__main__": Doc= Minidom.parse ("Simple.xml") Sample= Samplescanner (DOC)
Examples of Python parsing XML file operations