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