Python XML Read and write

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

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