How Python uses minidom to read and write XML

Source: Internet
Author: User

The example in this article describes how Python uses Minidom to read and write XML. Share to everyone for your reference. The specific analysis is as follows:

A python-provided XML support

2 Industrial Standard XML parsing methods-sax and Dom. SAX (Simple API for XML) is event-based, and when an XML document is read sequentially, each encounter of an element triggers the corresponding event handler to handle it. Dom (document Object Model), by building a tree structure to represent the entire XML document, once the tree is built, you can provide an interface through the DOM to traverse the tree and extract the corresponding data.

Python also provides Python's unique XML parsing method, which is elementtree compared to sax and Dom easier to use and faster.

Python's XML modules are:

1) xml.dom.minidom

2) Xml.elementtree

3) Xml.sax + xml.dom

Two XML instances: (employees.xml)

?

1 2 3 4 5 6 7 8 9 10 11 <?xml version= "1.0" encoding= "UTF-8"?> <employees> <employee> <name>l inux </name> < age> </age> </employee> <employee> <name>windows </name> <age> </age > </employee> </employees>

Three uses Xml.dom.minidom to read and write XML

1 use Xml.dom.minidom to parse xml:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Def testminidom (): From xml.dom import minidom doc = Minidom.parse ("em Ployees.xml ") # Get root element: <employees/> root = doc.documentelement # get all children elements: <employee /> <employee/> employees = root.getelementsbytagname ("employee") for employee in Employees:print ("--------- ----------------------------------") # element Name:employee print (employee.nodename) # element XML content: <emp Loyee><name>windows</name><age>20</age></employee> # basically equal to Toprettyxml function Print (Employee.toxml ()) Namenode = Employee.getelementsbytagname ("name") [0] Print ( namenode.childnodes) Print (Namenode.nodename + ":" + namenode.childnodes[0].nodevalue) Agenode = Employee.getelementsbytagname ("Age") [0] Print (agenode.childnodes) print (AgenOde.nodename + ":" + agenode.childnodes[0].nodevalue) print ("-------------------------------------------") for N in E Mployee.childNodes:print (n) testminidom ()

2 use Xml.dom.minidom to generate XML:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Def createxml (): import xml.dom.minidom impl = xml.dom.minidom.getDOMImplementation () dom = Impl.createdocument (None, ' Employees ', None] root = dom.documentelement employee = dom.createelement (' employee ') root.appendchild (employee) Namee = dom.createelement (' name ') Namet = Dom.createtextnode (' Linux ') namee.appendchild (Namet) employee.appendchild (NameE) Agee = dom.createelement (' age ') Aget = Dom.createtextnode (') agee.appendchild (aget) employee.appendchild (AgeE) F = Open (' Employees2.xml ', ' W ') Dom.writexml (f, addindent = ', newl = ' n ', encoding = ' utf-8 ') f.close () Createxml ()

3 The use of xml.dom.minidom need to pay attention to

* Use Parse () or createdocument () to return the DOM object;

* Root Element can be obtained using the DocumentElement property of the DOM;

*dom is a tree structure, contains many nodes, in which element is a node, can contain the child elements,textnode is also a node, is the final child node;

* Each node has a Nodename,nodevalue,nodetype attribute, NodeValue is the value of the node and is valid only for Textnode. For Textnode, the text content to which you want it can be used:. Data property.

The *nodetype is the type of node and now has the following:

' Attribute_node ' cdata_section_node ' comment_node ' Document_fragment_node '

' Document_node ' document_type_node ' element_node ' entity_node ' Entity_reference_node '

' Notation_node ' processing_instruction_node ' Text_node '

*getelementsbytagname () can find the child elements according to the name;

*childnodes returns all the nodes, where all the text is Textnode, containing the ' nr ' and the spaces between the elements are textnode;

*writexml () addindent= ' represents the indentation of a child element, newl= ' n ' represents a newline between elements, and encoding= ' Utf-8 ' represents the encoded format () of the generated XML .

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.