Python writes an example of an XML file operation

Source: Internet
Author: User
The example of this article describes how Python writes XML files, and shares them for everyone's reference. Here's how:

The XML file format to be generated is as follows:

<?xml version= "1.0"?> 
 
   
 
     
  
        Sample XML Thing           
  
          
   
    
     
             ma       
   
           
   
    
     
             xiaoju       
   
    Springs Widgets, Inc.     
  
                          
  
        
   
           First          
   
    
     
           I think Widgets is greate. You should buy lots of them forom       
    
     
      
              spirngy Widgts, Inc.       
    
          
   
       
  
    
 
  

The Python implementation code is as follows:

From xml.dom import Minidom, Node doc = Minidom. Document () Doc.appendchild (Doc.createcomment ("Simple XML Document__chapter 8") #generate the book book = Doc.createelem ENT (' book ') doc.appendchild #the title title = doc.createelement (' title ') Title.appendchild (Doc.createtextnode ( "Sample XML Thing")) Book.appendchild (title) #the Author Section author = doc.createelement ("author") Book.appendchild (A Uthor) name = doc.createelement (' name ') author.appendchild (name) FirstName = doc.createelement (' first ') Firstname.appendchild (Doc.createtextnode ("Ma")) Name.appendchild (firstname) LastName = Doc.createelement (' last ') Name.appendchild (LastName) Lastname.appendchild (Doc.createtextnode ("Xiaoju")) affiliation = Doc.createelement ("  Affiliation ") Affiliation.appendchild (Doc.createtextnode (" Springs Widgets, Inc. ") Author.appendchild (affiliation) #The Chapter Chapter = doc.createelement (' chapter ') Chapter.setattribute (' Number ', ' 1 ') title = Doc.createelement (' Title ') Title.appendchild (Doc.createTextNode ("first")) Chapter.appendchild (title) Book.appendchild (chapter) para = Doc.createelement (' para ') Para.appendchild (Doc.createtextnode ("I think widgets is greate.\ you should buy lots of them Forom")] Company = Doc.creat Eelement (' company ') Company.appendchild (Doc.createtextnode ("Spirngy Widgts, Inc.") Para.appendchild (company)  Chapter.appendchild (para) print doc.toprettyxml ()

Hopefully this article will help you with Python programming.

  • 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.