Use Python to generate an XML Method Instance. Use python to generate an xml instance.

Source: Internet
Author: User

Use Python to generate an XML Method Instance. Use python to generate an xml instance.

This example describes how to generate XML using Python. We will share this with you for your reference. The details are 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 () # create a DOM Document Object bookstore = doc. createElement ('bookstore') # create the root element bookstore. setAttribute ('xmlns: xsi', "http://www.w3.org/2001/XMLSchema-instance") # Set the namespace bookstore. setAttribute ('xsi: noNamespaceSchemaLocation ', 'bookstore. xsd ') # reference the local XML Schemadoc. appendChild (bookstore) ########### book: python processes XML Minidom ################# book = doc. createElement ('book') book. setAttribute ('genre', 'xml') bookstore. appendChild (book) title = doc. createElement ('title') title_text = doc. createTextNode ('python processes XML Minidom ') # Write the element content to the 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: django ####################### book1 = doc. createElement ('book') book1.setAttribute ('genre', 'web') bookstore. appendChild (book1) title1 = doc. createElement ('title') title_text1 = doc. createTextNode ('django of 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 ('lil') author_last_name_text1 = doc. createTextNode ('4') author1.appendChild (author_first_name1) author1.appendChild (author_last_name1) Principal (author_first_name_text1) Principal (principal) 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 the 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> Use Python to process XML Minidom </title> <author> <first-name> </first-name> <last -name> 3 </last-name> </author> <price> 28 </price> </book> <book genre = "Web"> <title> write a website using Python django </title> <author> <first-name> LI </first-name> <last-name> 4 </last-name> </author> <price> 40 </price> </book> </bookstore>

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat

XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat

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.