Python module--xml

Source: Internet
Author: User

XML module

XML is a protocol that implements data exchange between different languages or programs, similar to JSON, but JSON is simpler to use.

However, in ancient times, in the dark years when JSON was not yet born, you can only choose to use XML, so far many traditional companies such as the financial industry, many systems interface is mainly XML.

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

<?XML version= "1.0"?><Data>    <Countryname= "Liechtenstein">        <RankUpdated= "Yes">2</Rank>        < Year>2008</ Year>        <GDPPC>141100</GDPPC>        <Neighborname= "Austria"direction= "E"/>        <Neighborname= "Switzerland"direction= "W"/>    </Country>    <Countryname= "Singapore">        <RankUpdated= "Yes">5</Rank>        < Year>2011</ Year>        <GDPPC>59900</GDPPC>        <Neighborname= "Malaysia"direction= "N"/>    </Country>    <Countryname= "Panama">        <RankUpdated= "Yes">69</Rank>        < Year>2011</ Year>        <GDPPC>13600</GDPPC>        <Neighborname= "Costa Rica"direction= "W"/>        <Neighborname= "Colombia"direction= "E"/>    </Country></Data>

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

Traverse
ImportXml.etree.ElementTree as Ettree= Et.parse ("XML Test")#Open XML Fileroot = Tree.getroot ()#Get root node#Print (dir (root))Print(Root.tag)#traversing an XML document forChildinchRoot:Print('----------', Child.tag, Child.attrib)#Print Country Node     forIinchChild :Print(I.tag,i.text)
modifying and deleting XML document content
ImportXml.etree.ElementTree as Ettree= Et.parse ("xml_test") Root= Tree.getroot ()#f.seek (0)#Modify forNodeinchRoot.iter (' Year'): New_year= Int (node.text) + 5Node.text= str (new_year)#Modify ContentNode.set ("attr_test","false") Tree.write ('Output.xml')#Write File##删除node forCountryinchRoot.findall ('Country'): Rank= Int (Country.find ('Rank'). Text)ifRank > 50: Root.remove (country) tree.write ('Output.xml')
New XML
ImportXml.etree.ElementTree as Etroot= ET. Element ("NameList")#Create Rootname= ET. subelement (Root,"name", attrib={"enrolled":"Yes"})#Create Child--nameAge = ET. subelement (Name," Age", attrib={"checked":"No"})#Create name Child--age,sex,nameSex = ET. subelement (Name,"Sex") n= ET. subelement (Name,"name") N.text="Alex Li"Sex.text='male'name2= ET. subelement (Root,"name", attrib={"enrolled":"No"}) age= ET. Subelement (Name2," Age") Age.text=' +'et= ET. ElementTree (Root)#Generating Document ObjectsEt.write ("Build_out.xml", encoding="Utf-8", xml_declaration=true)

Because the native saved XML is not indented by default, if you want to set indentation, you need to modify the Save method

ImportXml.etree.ElementTree as ET fromXml.domImportMinidomdefsubelement (root, tag, text): Ele=ET. subelement (root, tag) Ele.text=textdefSaveXML (root, filename, indent="\ t", newl="\ n", encoding="Utf-8"): Rawtext=et.tostring (Root) Dom=minidom.parsestring (Rawtext) with open (filename,'W') as F:dom.writexml (F,"", indent, newl, encoding) root= ET. Element ("NameList") to= Root.makeelement (" to", {}) To.text="Peter"Root.append (to) name= ET. subelement (Root,"name", attrib={"enrolled":"Yes"})#Create Child--nameAge = ET. subelement (Name," Age", attrib={"checked":"No"})#Create name Child--age,sex,nameSex = ET. subelement (Name,"Sex") n= ET. subelement (Name,"name") N.text="Alex Li"Sex.text='male'name2= ET. subelement (Root,"name", attrib={"enrolled":"No"}) age= ET. Subelement (Name2," Age") Age.text=' +'#et = et. ElementTree (root) # Generate Document Object#Save the XML fileSaveXML (Root,"Note.xml")

Python module--xml

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.