Pythonxml reading and writing: first, let's talk about how to create an XML file.
#-*-Coding: UTF-8-*-from xml. dom import minidomimpl = minidom. getDOMImplementation () dom = impl. createDocument (None, None, None) # namespaceURI, qualifiedName, doctype # write to domroot = dom. createElement ("skills") for skillid in range (10): skill = dom. createElement ('skill') skill. setAttribute ('id', str (skillid) root. appendChild (skill) dom. appendChild (root) # end write to dom # writexml (writer, inden T, addindent, newl, encoding) # writer is a file object # indent is a character filled before each tag, such :'', it indicates that each tag has two spaces before it. # addindent is the character of each subnode. # newl is the character filled after each tag, for example, '\ n ', it indicates that each tag is followed by a carriage return # encoding is the value of the encoding attribute in the generated XML Information header. # The minidom does not actually process the encoding during output, if your saved text contains Chinese characters, # you need to encode and convert it yourself. F = file ('C: \ skills. xml ', 'w') dom. writexml (f, '','', '\ n', 'utf-8') f. close ()
The generated XM is as follows:
#######################
Read XML
>>> Dom = xml. dom. minidom. parse ('C:/skills. XML ')
>>> Root = dom.doc umentElement
>>> Root. nodeName
>>> R2.nodeName
U'skills'