Let's talk about how to create an XML file.
#-*-Coding:utf-8-*-from xml.dom Import Minidomimpl = minidom.getdomimplementation () dom = Impl.createdocument (None, Non E, None) #namespaceURI, QualifiedName, doctype#write to Domroot = dom.createelement ("skills") for Skillid in range:
skill= dom.createelement (' skill ') skill.setattribute (' id ', str (skillid)) Root.appendchild (skill) Dom.appendchild (Root) #end write to Dom#writexml (writer, indent, addindent, NEWL, encoding) #writer是文件对象 # Indent is a character that is filled in front of each tag, such as: ' , it means that each tag has two spaces before it #addindent is the indented character of each child node #newl is the character that is filled after each tag, such as: ' \ n ', which means that there is a carriage return after each tag. Encoding is the encoding property value in the generated XML header, # Minidom is not really encoded at the time of the output, and if you have characters in your saved text, # You need to encode your own. F=file (' C:\\skills.xml ', ' W ') Dom.writexml (f, ', ', ' \ n ', ' Utf-8 ') f.close ()
The resulting XM is as follows:
#######################
Read XML
>>> dom = xml.dom.minidom.parse (' C:/skills.xml ')
>>> root = dom.documentelement
>>> Root.nodename
>>> R2.nodename
U ' skills '