Python------module definition, import, optimization------->xml module

Source: Internet
Author: User

1. XML modules

Reference reference Source Link: https://www.cnblogs.com/python-gm/p/8032465.html Thank you

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

In the Dark Age when JSON is not yet born, you can only choose to use XML, so far many traditional companies such as the financial industry of many systems interface is also mainly XML.

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

 1 <?xml version="1.0"?>2<data>3<country name="Liechtenstein">4<rank updated="Yes">2</rank>5<year>2008</year>6<gdppc>141100</gdppc>7<neighbor name="Austria"direction="E"/>8<neighbor name="Switzerland"direction="W"/>9</country>Ten<country name="Singapore"> One<rank updated="Yes">5</rank> A<year>2011</year> -<gdppc>59900</gdppc> -<neighbor name="Malaysia"direction="N"/> the</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"/> A</country> at</data>

How do I process xml in Python?

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") #指向需要操作的 . xml File
root = Tree.getroot ()
Print (Root.tag)

# Traverse XML Document
For child in Root:
Print (Child.tag, child.attrib)
For I in the child:
Print (I.tag, i.text)
Print (20* ' = = ')
# Traverse only the year node
For node in Root.iter (' year '):
Print (Node.tag, node.text)


Operation Result:

Data
Country {' name ': ' Liechtenstein '}
Rank 2
Year 2008
GDPPC 141100
Neighbor None
Neighbor None
Country {' name ': ' Singapore '}
Rank 5
Year 2011
GDPPC 59900
Neighbor None
Country {' name ': ' Panama '}
Rank 69
Year 2011
GDPPC 13600
Neighbor None
Neighbor None
========================================
Year 2008
Year 2011
Year 2011

2) Modify and delete XML document content

1 ImportXml.etree.ElementTree as ET2   3Tree = Et.parse ("Xmltest.xml")4Root =tree.getroot ()5   6 #Modify7  forNodeinchRoot.iter (' Year'):8new_year = Int (node.text) + 19Node.text =Str (new_year)TenNode.set ("Updated","Yes") One    ATree.write ("Xmltest.xml") -    -    the #Delete Node -  forCountryinchRoot.findall ('Country'): -rank = Int (Country.find ('Rank'). Text) -    ifRank > 50: + root.remove (country) -    +Tree.write ('Output.xml')    

3) Create your own XML document

1 ImportXml.etree.ElementTree as ET2   3   4New_xml = ET. Element ("NameList")5Name = ET. Subelement (New_xml,"name", attrib={"enrolled":"Yes"})6Age = ET. subelement (Name," Age", attrib={"checked":"No"})7Sex = ET. subelement (Name,"Sex")8Sex.text =' -'9Name2 = ET. Subelement (New_xml,"name", attrib={"enrolled":"No"})TenAge = ET. Subelement (Name2," Age") OneAge.text =' +' A    -ET = et. ElementTree (New_xml)#Generating Document Objects -Et.write ("Test.xml", encoding="Utf-8", xml_declaration=True) the    -Et.dump (New_xml)#print the generated format

Python------module definition, import, optimization------->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.