Python and XML

Source: Internet
Author: User

Python and XML operations

First, Introduction

XML is a protocol that implements data exchange between different languages or programs, much like JSON, but JSON is simpler to use, but in the dark ages when JSON is not yet born, you can only choose XML, so many of the systems of traditional companies, such as the financial industry, are mostly XML.

Second, the structure

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

<?XML version= "1.0"?><Data>    <Countryname= "China">        <RankUpdated= "Yes">2</Rank>        < Year>2008</ Year>        <GDPPC>141100</GDPPC>        <Neighborname= "中文版"direction= "E"/>        <Neighborname= "Switzerland"direction= "W"/>    </Country>    <Countryname= "French">        <RankUpdated= "Yes">5</Rank>        < Year>2011</ Year>        <GDPPC>59900</GDPPC>        <Neighborname= "Malaysia"direction= "N"/>    </Country>    <Countryname= "Italy">        <RankUpdated= "Yes">69</Rank>        < Year>2011</ Year>        <GDPPC>13600</GDPPC>        <Neighborname= "Costa Rica"direction= "W"/>        <Neighborname= "Colombia"direction= "E"/>    </Country></Data>

Third, using XML in Python

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

1. Traverse XML

Importxml.etree.ElementTree as ET tree= Et.parse ("Xmltest.xml") Root=tree.getroot ()Print(Root.tag)#traversing an XML document forChildinchRoot:Print(Child.tag, Child.attrib) forIinchChild :Print(I.tag,i.text)#Traverse only the year node forNodeinchRoot.iter (' Year'):    Print(Node.tag,node.text)

2. Modify and delete XML document content

mport Xml.etree.ElementTree as ET tree= Et.parse ("Xmltest.xml") Root=tree.getroot ()#Modify forNodeinchRoot.iter (' Year'): New_year= Int (node.text) + 1Node.text=Str (new_year) Node.set ("Updated","Yes") Tree.write ("Xmltest.xml")  #Delete Node forCountryinchRoot.findall ('Country'): Rank= Int (Country.find ('Rank'). Text)ifRank > 50: Root.remove (country) tree.write ('Output.xml')

3. Create an XML document

ImportXml.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)#Generating Document ObjectsEt.write ("Test.xml", encoding="Utf-8", xml_declaration=True) Et.dump (New_xml)#print the generated format

Python and 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.