Create an XML file with Python

Source: Internet
Author: User

1 #Coding=utf-82  fromXml.etreeImportElementTree3 4 ImportPDB5 6 defprintnodeinfo (node):7 8     #Node.tag Label name9     #Node.text Text PropertiesTen     Print 'Node.tag:%s'%Node.tag One  A     #Node.attrib Property Dictionary -      forKeyinchNode.attrib: -         Print '%s%s'%(Key,node.attrib[key]) the     returnNone -  - defMain (): -  + pdb.set_trace () -     Try: +         #elementtree.parse (Source,parser=none) A         #loads the XML file and returns the ElementTree object at         #parser is an optional parameter that uses the standard xmlparser if it is empty -         #Step 1: Get the ElementTree object -xmldoc = Elementtree.parse ('D:\server\map\App.xml') -         #Step 2: Get the root node -Root =xmldoc.getroot () -         #Root.getchildren () Gets the list returned by the node inGateservernodelist =Root.getchildren () -  to         #here is the addition of a child node under each gateserver node +          forNodeinchgateservernodelist: -             #elementtree.subelement (parentnode,tagname) the             #Add a child node to a parentnode *             #consistent with parentnode.append (Element) Effect $NewNode = elementtree.subelement (node,'Activetype')Panax Notoginseng             #Setting Properties -newnode.attrib['ID'] ='9' thenewnode.attrib[' Level'] =' -' +newnode.attrib['begin'] ='201401010000' Anewnode.attrib['End'] ='201401010000' the             #Chinese need special decoding +newnode.attrib['Tip'] ='Test'. Decode ('Utf-8') -             #symbol at the end of a node $Newnode.tail ='\ n' $             #node.append (NewNode) -  -         #writes the ElementTree object before the file is called . the         #Write (filename,encoding = None,xml_decleare = None,default_namespace = None,method = None) -         #where Xml_decleare is True, it is written at the top of the documentWuyi         #<?xml version= "1.0" encoding= "Utf-8"?> theXmldoc.write ('D:test.xml','Utf-8', True) -     exceptException as E: Wu         Printe -          About     returnNone $  - if __name__=='__main__': -Main ()

Create an XML file with Python

Related Article

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.