Create an XML document using Python _python

Source: Internet
Author: User

When Google checks, the content is almost the same. But what you want is not one. For example, I can't find Chinese writing how to use Python to create an XML file. Of course, the same effect can be achieved by writing directly in a file, but it's easy to make mistakes and it doesn't look elegant. Finally, I read a lot of information and finally figured out how to write an XML file using Python. Here is a simple example, this example is already debugging through, you can rest assured that the use.

Import Xml.dom.minidom
From xml.dom.DOMImplementation Import implementation
Import Xml.sax.writer
Import Xml.utils

# Create a new document with no namespace URI, qualified name,
# or Document Type
Document = Implementation.createdocument (None,none,none)
Personnel = Document.createelement ("Personnel")
Personnel.setattribute (' Number ', ' 5 ')
Document.appendchild (Personnel)
Sexnode = document.createelement ("Sex")
Sexnode.appendchild (document.createTextNode ("male"))

Namenode = document.createelement ("name")
Namenode.appendchild (document.createTextNode ("Tianbin"))

Personnel.appendchild (Sexnode)
Personnel.appendchild (Namenode)

out = open ("Tianbin.xml", "W")
Xml.dom.ext.PrettyPrint (Document,out)


Today you want to use Python to create an XML file. Find the following information, found that the data is not many, basically use Python to parse the XML file.
For example, I want to add content to

<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<book isbn= "34909023" >
<author>
Dikatour
</author>
</book>
</root>

Write to the Xmlstuff.xml file.
In fact it is very simple, the basic principle is as follows:
I use the DOM way of XML, first create an empty DOM tree in memory, and then increment the node I want, finally form the DOM I want, and finally output to the file.
1. I use the Xml.dom.minidom module to create an XML file
From Xml.dom import Minidom
2. Each XML file is a Document object that represents the in-memory DOM tree
doc = Minidom. Document ()
3. With the empty DOM tree, we add the root node above
RootNode = www.bsck.org("root")
Doc.appendchild (RootNode) #注意python的library reference said that after createelement did not add the node object to the DOM tree, you need to manually add
4. Create a different node
5. Output to an XML file
Doc.writexml (F, "/t", "/t", "/n", "Utf-8") #第一个参数f就是你的目标文件对象, Bachelor cinema The second argument appears to be the indentation arrangement for <?xml> and the following root node.
The third parameter seems to be the indentation arrangement of the other nodes and the child nodes, and the fourth parameter has the format of the newline (if you fill in "", then the line is not wrapped, all the XML is indented on one line:))
, the fifth parameter formulates the encoding of the XML content. Except that the first parameter is required, the other parameters are selectable.
The final code is as follows (the program is worthless, just to test your own ideas, and you are more likely to define a simple class or function that serializes your data structure into an XML file):

From Xml.dom import Minidom
Import Traceback
Try
f = open ("Xmlstuff.xml", "W")
Try
Doc = Minidom. Document ()
RootNode = doc.createelement ("root")
Doc.appendchild (RootNode)
Booknode = Doc.createelement ("book")
Booknode.setattribute ("ISBN", "34909023")
Rootnode.appendchild (Booknode)
Authornode = Doc.createelement ("author")
Booknode.appendchild (Authornode)
Authortextnode = Doc.createtextnode ("Dikatour")
Authornode.appendchild (Authortextnode)
Doc.writexml (F, "/t", "/t", "/n", "Utf-8")
Except
Trackback.print_exc ()
Finally
F.close ()
Except IOException:
Print "Open file failed"

Create an XML document using Python _python

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.