Python-xml Module

Source: Internet
Author: User

XML is a protocol that implements data exchange between different languages or programs, much like JSON, but JSON is simpler to use, but in ancient times, in the Dark Ages when JSON was not yet born, you could only choose to use XML, and so far many traditional companies, such as the financial industry, are mostly XML-like interfaces.

The format of XML is as follows: by the <> node to distinguish the data structure.

<?xml version= "1.0"?><data>    <country name= "Liechtenstein" >        <rank updated= "yes" >2 </rank>        <year>2008</year>        <gdppc>141100</gdppc>        <neighbor name= " Austria "direction=" E "/>        <neighbor name=" Switzerland "direction=" W "/>    </country>    < Country name= "Singapore" >        <rank updated= "yes" >5</rank>        <year>2011</year>        <gdppc>59900</gdppc>        <neighbor name= "Malaysia" direction= "N"/>    </country >    <country name= "Panama" >        <rank updated= "yes" >69</rank>        <year>2011< /year>        <gdppc>13600</gdppc>        <neighbor name= "Costa Rica" direction= "W"/>        < Neighbor Name= "Colombia" direction= "E"/>    </country></data>

XML protocols are supported in each language, and in Python you can manipulate XML with the following modules:

 import Xml.etree.ElementTree as ET tree = Et.parse ("xmltest.xml") root = Tree.getroot () print (Root.tag) #遍历xml文档for Root:print (Child.tag, Child.attrib) for I in Child:print (i.tag,i.text) #只遍历year nodes for node in root . ITER (' year '): Print (node.tag,node.text) #---------------------------------------import Xml.etree.ElementTree as ET    Tree = Et.parse ("xmltest.xml") root = Tree.getroot () #修改for node in Root.iter (' year '): new_year = Int (node.text) + 1 Node.text = str (new_year) Node.set ("Updated", "yes") tree.write ("Xmltest.xml") #删除nodefor Country in Root.findall (' Coun Try '): rank = Int (country.find (' rank '). Text) if rank > 50:root.remove (Country) tree.write (' Output.xml ') 
Create Build XML Document:
Import xml.etree.ElementTree as et  new_xml = et. Element ("NameList") name = ET. Subelement (New_xml, "name", attrib={"enrolled": "Yes"}) Age = ET. subelement (Name, "Age", attrib={"checked": "No"}) Sex = ET. subelement (name, "sex") Sex.text = ' name2 ' = ET. Subelement (New_xml, "name", attrib={"enrolled": "No"}) Age = ET. Subelement (name2, "age") Age.text = ' + ' et = et. ElementTree (New_xml) #生成文档对象et. Write ("Test.xml", encoding= "Utf-8", Xml_declaration=true) Et.dump (new_xml) #打印生成的格式

Python-xml Module

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.