Use python to generate xml

Source: Internet
Author: User
1. Bookstore. py
 #  Encoding: UTF-8  
'''
Based on a given XML schema, an XML is generated from a blank file in the form of a DOM tree.
'''
From XML. Dom. minidom Import Document

Doc = Document () # Create a DOM Document Object

Bookstore = Doc. createelement (' Bookstore ' ) # Create root element
Bookstore. setattribute ( ' Xmlns: xsi ' , " Http://www.w3.org/2001/XMLSchema-instance " ) # Set namespace
Bookstore. setattribute (' Xsi: nonamespaceschemalocation ' , ' Bookstore. XSD ' ) # Reference local XML Schema
Doc. appendchild (bookstore)
# ########## Book: Using python to process XML mini-dom ################
Book = Doc. createelement ( ' Book ' )
Book. setattribute ( ' Genre ' , ' XML ' )
Bookstore. appendchild (book)

Title = Doc. createelement ( ' Title ' )
Title_text = Doc. createtextnode ( ' Using python to process XML mini-dom ' ) # Write Element Content
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 ( ' 3. ' )
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 ( ' 28 ' )
Price. appendchild (price_text)
Book. appendchild (price)
# ########## Book1: Write a website using Python ####################
Book1 = Doc. createelement ( ' Book ' )
Book1.setattribute ( ' Genre ' , ' Web ' )
Bookstore. appendchild (book1)

Title1 = Doc. createelement ( ' Title ' )
Title_text1 = Doc. createtextnode ( ' Django for python website writing ' )
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 ( ' Li ' )
Author_last_name_text1 = Doc. createtextnode ( ' Thu ' )
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 ( ' 40 ' )
Price1.appendchild (price_text1)
Book1.appendchild (price1)

# ######### Write the DOM object Doc to a 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. Use the XML generated by Python minidom according to the preceding XML Schema
 
Bookstore. xml
 <?  XML version = "1.0"  ?> 
< Bookstore Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance" Xsi: nonamespaceschemalocation = "Bookstore. XSD" >
< Book Genre = "XML" >
< Title >
Using python to process XML mini-dom
</ Title >
< Author >
< First-name >
Zhang
</ First-name >
< Last-name >
3.
</ Last-name >
</ Author >
< Price >
28
</ Price >
</ Book >
< Book Genre = "Web" >
< Title >
Django for python website writing
</ Title >
< Author >
< First-name >
Li
</ First-name >
< Last-name >
Thu
</ Last-name >
</ Author >
< Price >
40
</ Price >
</ Book >
</ Bookstore >

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.