Using DOM modules in Python to generate an XML file sample _python

Source: Internet
Author: User
Tags in python

parsing XML files in Python also has two ways of Dom and sax, and here's how to parse XML using DOM, which is the DOM generates an XML file, and the next article goes on to explain the DOM parsing xml file.

In generating XML files, we use the following methods primarily to accomplish this.

Main methods

1. Generate XML nodes (node)

Copy Code code as follows:

CreateElement ("Node_name")

2. Add attribute value to Node
Copy Code code as follows:

Node.setattribute ("Att_name", "Arr_value")

3, the node label value (data)
Copy Code code as follows:

createTextNode ("Node_value")

Where the 1th and 3 points after creating the node (node value), you need to add the following method to the location of the pointing node:
Copy Code code as follows:

Prev_node.appendchild (Cur_node)

The prev_node here is to add the node's upper-level node, and Cur_node is the node that is currently being added.

Code Demo

The following code demonstrates how to use the DOM to generate XML, which is a simple version, as follows:

Copy Code code as follows:

Created on 2012-8-28

@author: walfred
@module: Domxml.genxml
Import Xml.dom.minidom as Dom

if __name__ = = "__main__":
doc = Dom.document ()
Root_node = Doc.createelement ("Book_store")
Root_node.setattribute ("name", "Newhua")
Root_node.setattribute ("website", "Http://")
Doc.appendchild (Root_node)

Book_node = Doc.createelement ("Book1")

Book_name_node = doc.createelement ("name")
Book_name_value = Doc.createtextnode ("Hamlet")
Book_name_node.appendchild (Book_name_value)
Book_node.appendchild (Book_name_node)

Book_author_node = Doc.createelement ("author")
Book_author_value = Doc.createtextnode ("William Shakespeare")
Book_author_node.appendchild (Book_author_value)
Book_node.appendchild (Book_author_node)

Root_node.appendchild (Book_node)

f = open ("Book_store.xml", "W")
F.write (doc.toprettyxml (indent = "\ T", newl = "\ n", encoding = "Utf-8"))
F.close ()

This code will generate a book_store.xml file in the current directory, and I'll post it below:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<book_store name= "Newhua" website= "Http://" >
<author>william shakespeare</author>

Of course once you have mastered these basic methods, we can use a class for better completion, this class we call Xmlgenerator, the code is as follows:

Copy Code code as follows:

Created on 2012-8-28

@author: walfred
@module: Domxml.wxmlbydom
Import Xml.dom.minidom as Dom

Class Xmlgenerator:
def __init__ (self, xml_name):
Self.doc = Dom.document ()
Self.xml_name = Xml_name

def createnode (self, node_name):
Return Self.doc.createElement (Node_name)

def addNode (self, node, Prev_node = None):
Cur_node = node
If Prev_node is not None:
Prev_node.appendchild (Cur_node)
Self.doc.appendChild (Cur_node)
Return Cur_node

def setnodeattr (self, node, att_name, value):
Cur_node = node
Cur_node.setattribute (Att_name, value)

def setnodevalue (self, Cur_node, value):
Node_data = Self.doc.createTextNode (value)
Cur_node.appendchild (Node_data)

def genxml (self):
f = open (Self.xml_name, "w")
F.write (self.doc.toprettyxml (indent = "\ T", newl = "\ n", encoding = "Utf-8"))
F.close ()

if __name__ = = "__main__":
Myxmlgenerator = Xmlgenerator ("Book_store.xml")

#xml root Node
Node_book_store = Myxmlgenerator.createnode ("Book_store")
Myxmlgenerator.setnodeattr (Node_book_store, "name", "New Hua")
Myxmlgenerator.setnodeattr (Node_book_store, "website", "Http://")
Myxmlgenerator.addnode (node = node_book_store)

node_book_01 = Myxmlgenerator.createnode ("book")

Node_book_01_name = Myxmlgenerator.createnode ("name")
Myxmlgenerator.setnodevalue (Node_book_01_name, "Hamlet")
Myxmlgenerator.addnode (Node_book_01_name, node_book_01)

Node_book_01_author = Myxmlgenerator.createnode ("author")
Myxmlgenerator.setnodevalue (Node_book_01_author, "William Shakespeare")
Myxmlgenerator.addnode (Node_book_01_author, node_book_01)

Node_book_01_price = Myxmlgenerator.createnode ("Price")
Myxmlgenerator.setnodevalue (Node_book_01_price, "$ $")
Myxmlgenerator.addnode (Node_book_01_price, node_book_01)

Node_book_01_grade = Myxmlgenerator.createnode ("Grade")
Myxmlgenerator.setnodevalue (Node_book_01_grade, "good")
Myxmlgenerator.addnode (Node_book_01_grade, node_book_01)

Myxmlgenerator.addnode (node_book_01, Node_book_store)

#book 02
node_book_02 = Myxmlgenerator.createnode ("book")

Node_book_02_name = Myxmlgenerator.createnode ("name")
Myxmlgenerator.setnodevalue (Node_book_02_name, "Shuihu")
Myxmlgenerator.addnode (Node_book_02_name, node_book_02)

Node_book_02_author = Myxmlgenerator.createnode ("author")
Myxmlgenerator.setnodevalue (Node_book_02_author, "Naian shi")
Myxmlgenerator.addnode (Node_book_02_author, node_book_02)

Node_book_02_price = Myxmlgenerator.createnode ("Price")
Myxmlgenerator.setnodevalue (Node_book_02_price, "$)"
Myxmlgenerator.addnode (Node_book_02_price, node_book_02)

Node_book_02_grade = Myxmlgenerator.createnode ("Grade")
Myxmlgenerator.setnodevalue (Node_book_02_grade, "good")
Myxmlgenerator.addnode (Node_book_02_grade, node_book_02)

Myxmlgenerator.addnode (node_book_02, Node_book_store)

Myxmlgenerator.genxml ()

The same method will generate a Book_store.xml file in this directory, as follows:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<book_store name= "New Hua" website= "" >
<author>william shakespeare</author>
<author>naian shi</author>

This version is a slightly advanced version, but because of the time constraints are still very rough, the reader can play better.

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: 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.