Generate method instances of XML using Python

  • 2020-05-27 06:00:59
  • OfStack

The example in this article shows how to generate XML using Python. I will share it with you for your reference as follows:

1. bookstore.py


#encoding:utf-8
'''
 According to the 1 A given XML Schema , the use of DOM The tree form is generated from a blank file 1 a XML . 
'''
from xml.dom.minidom import Document
doc = Document() # create DOM The document object 
bookstore = doc.createElement('bookstore') # Creating the root element 
bookstore.setAttribute('xmlns:xsi',"http://www.w3.org/2001/XMLSchema-instance")# Set the namespace 
bookstore.setAttribute('xsi:noNamespaceSchemaLocation','bookstore.xsd')# Refer to the local XML Schema
doc.appendChild(bookstore)
############book:Python To deal with XML the Minidom################
book = doc.createElement('book')
book.setAttribute('genre','XML')
bookstore.appendChild(book)
title = doc.createElement('title')
title_text = doc.createTextNode('Python To deal with XML the Minidom') # Element content write 
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:Python Write the site Django####################
book1 = doc.createElement('book')
book1.setAttribute('genre','Web')
bookstore.appendChild(book1)
title1 = doc.createElement('title')
title_text1 = doc.createTextNode('Python Write the site 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(' li ')
author_last_name_text1 = doc.createTextNode('4')
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)
###########  will DOM object doc Written 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. XML is generated from Python minidom according to 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 To deal with XML the Minidom
  </title>
  <author>
   <first-name>
     zhang 
   </first-name>
   <last-name>
    3
   </last-name>
  </author>
  <price>
   28
  </price>
 </book>
 <book genre="Web">
  <title>
   Python Write the site Django
  </title>
  <author>
   <first-name>
     li 
   </first-name>
   <last-name>
    4
   </last-name>
  </author>
  <price>
   40
  </price>
 </book>
</bookstore>

PS: here are some other online tools about xml operation for your reference:

Online XML/JSON interconversion tool:
http://tools.ofstack.com/code/xmljson

Online formatting XML/ online compression XML:
http://tools.ofstack.com/code/xmlformat

XML online compression/formatting tool:
http://tools.ofstack.com/code/xml_format_compress

XML code online formatting beautification tool:
http://tools.ofstack.com/code/xmlcodeformat

More about Python related topics: interested readers to view this site "Python xml data operation skill summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful for you to design Python program.


Related articles: