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.
Copy CodeThe code is as follows:
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
Copy CodeThe code is as follows:
Dikatour
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 = doc.createelement ("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就是你的目标文件对象, the second parameter appears to be a And the indented arrangement of 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):
Copy CodeThe code is as follows:
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"
Summarize:
1. Target (writes a string of XML strings to a file) = = Gets a string of XML strings =>dom tree (there are toxml methods in Minidom to output the XML information of the DOM tree to a string)
2. In the 8th chapter of the Library Reference (structrued Markup processing Tools), using the Python 2.5 documentation (the Python manual installed with Python), It's important to consult the manual, plus some concise python books.
3. Thinking, logic clear, just like me on how to use Python to manipulate XML is ignorant, a little bit of information can be completed function
4. It just proves the powerful functionality of the Python language:) gets job done.