Pythonxml reading and writing

Source: Internet
Author: User
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'

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.