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.