How to generate an XML file using Python

Source: Internet
Author: User
This article mainly introduces how to generate XML using Python, and analyzes in detail the smoothness of xml file generation using Python in combination with the specific instance form and related precautions, for more information about how to generate an XML file using Python, see the next article. This article describes how to generate an xml file using Python and how to generate an XML file using a specific instance, for more information, see

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


 
  
   
    
      
      
    
   
    
      
       
       
      
     
    
   
    
      
       
      
    
  
 

3. use the XML generated by Python minidom according to the preceding XML Schema

Bookstore. xml


 
  
    
   Using Python to process XML mini-Dom     
   
    
Zhang
      
   
    
3.
       
   
    
28
    
   
    
   Django for Python website writing     
   
    
Li
      
   
    
Thu
       
   
    
40
    
  
 

The above is a detailed description of how Python generates an XML file. For more information, see other related articles in the first PHP community!

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.