Examples of Python parsing XML file operations

Source: Internet
Author: User
Tags gettext tagname

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

Related Article

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.