Python method for generating XML files

Source: Internet
Author: User
This article mainly describes the use of Python to generate XML methods, combined with specific examples of detailed analysis of Python generated XML file specific fluency and related considerations, the need for friends can refer to the following

The example in this article describes how to generate XML using Python. Share to everyone for your reference, as follows:

1. bookstore.py

#encoding: Utf-8 "generates an XML from a blank file in the form of a DOM tree, based on a given XML Schema. ' from xml.dom.minidom import Documentdoc = Document () #创建DOM文档对象bookstore = doc.createelement (' bookstore ') # Create root element Bookstore.setattribute (' Xmlns:xsi ', "Http://www.w3.org/2001/XMLSchema-instance") # Set the namespace Bookstore.setattribute (' xsi:nonamespaceschemalocation ', ' bookstore.xsd ') #引用本地XML schemadoc.appendchild ( Bookstore) ########### #book:P ython processing xml minidom############### #book = doc.createelement (' book ') Book.setattribute ( ' Genre ', ' XML ') bookstore.appendchild (book) title = Doc.createelement (' title ') Title_text = Doc.createtextnode (' Python handles XML minidom ') #元素内容写入title. appendchild (Title_text) Book.appendchild (title) Author = doc.createelement (' Author ') Book.appendchild (author) author_first_name = doc.createelement (' first-name ') Author_last_name = Doc.createelement (' last-name ') Author_first_name_text = Doc.createtextnode (' zhang ') author_last_name_text = Doc.createtextnode (' three ') Author.appendchild (author_first_name) author.appendchild (author_last_name) Author_firSt_name.appendchild (Author_first_name_text) author_last_name.appendchild (author_last_name_text) book.appendChild (author) Price = Doc.createelement (' price ') Price_text = Doc.createtextnode (' + ') price.appendchild (Price_text) Book.appendchild (Price) ########### #book1:P Ython Write the django################### of the website #book1 = doc.createelement (' book ') Book1.setattribute (' genre ', ' Web ') bookstore.appendchild (book1) title1 = doc.createelement (' title ') Title_text1 = Doc.createtextnode (' python writes website Django ') title1.appendchild (TITLE_TEXT1) book1.appendchild (title1) Author1 = Doc.createelement (' author ') Book.appendchild (author1) author_first_name1 = doc.createelement (' first-name ') author_ last_name1 = doc.createelement (' last-name ') Author_first_name_text1 = Doc.createtextnode (' Lee ') author_last_name_text1 = Doc.createtextnode (' four ') Author1.appendchild (author_first_name1) author1.appendchild (author_last_name1) author_ First_name1.appendchild (AUTHOR_FIRST_NAME_TEXT1) author_last_name1.appendchild (AUTHOR_LAST_NAME_TEXT1) Book1.appendchild (authOr1) Price1 = doc.createelement (' price ') Price_text1 = Doc.createtextnode (' + ') price1.appendchild (PRICE_TEXT1) Book1.appendchild (Price1) ########### writes DOM object doc to File F = open (' Bookstore.xml ', ' W ') F.write (doc.toprettyxml (indent = ") ) F.close ()

2. bookstore.xsd


<?xml version= "1.0" encoding= "Utf-8"? ><xsd:schema xmlns:xsd= "http://www.w3.org/ 2001/xmlschema "elementformdefault=" qualified "> <xsd:element name=" Bookstore "type=" BookstoreType "/> < Xsd:complextype name= "Bookstoretype" > <xsd:sequence maxoccurs= "unbounded" > <xsd:element name= "book" type = "BookType"/> </xsd:sequence> </xsd:complexType> <xsd:complextype name= "BookType" > <xsd: sequence> <xsd:element name= "title" Type= "xsd:string"/> <xsd:element name= "Author" type= "AuthorName"/ > <xsd:element name= "Price" type= "Xsd:decimal"/> </xsd:sequence> <xsd:attribute name= "Genre" type= " Xsd:string "/> </xsd:complexType> <xsd:complextype name=" AuthorName "> <xsd:sequence> <xsd: Element name= "First-name" type= "xsd:string"/> <xsd:element name= "last-name" type= "xsd:string"/> </xsd: Sequence> </xsd:complextype></xsd:schema> 

3. XML generated from Python minidom based on the XML schema above

Bookstore.xml


<?xml version= "1.0"? ><bookstore xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: nonamespaceschemalocation= "Bookstore.xsd" > <book genre= "xml" >  <title>   python processing XML minidom  </title>  <author>   <first-name>    Zhang   </first-name>   <last-name >    three   </last-name>  </author>  <price>  </book </price> > <book genre= "Web" >  <title>   python writing web site Django  </title>  <author>   <first-name>    Lee   </first-name>   <last-name>    four   </last-name>  </author>  <price>  </price> </book></bookstore>
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.