XML processing Module
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.
Import Xml.etree.ElementTree as Ettree = Et.parse ("country.xml") root = Tree.getroot () # Get root node 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)
Import Xml.etree.ElementTree as Ettree = Et.parse ("country.xml") root = Tree.getroot () # Get the root node# only get year node for nod E in Root.iter (' year '): print (Node.tag,node.text)
#修改for node in Root.iter (' year '): new_year = Int (node.text) +1 node.text = str (new_year) Node.set ("Updated "," Yes ") tree.write (" Xml_test2.xml ")
#删除for Country in Root.findall ("Country"): rank = int (country.find (' rank '). Text) If rank >: Root.remove (country) tree.write ("Xml_test2.xml")
#createnew_xml = ET. Element ("NameList") #rootname = 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 ("New_test.xml", encoding= "Utf-8", Xml_declaration=true) Et.dump (new_xml) # Print the generated format
XML processing Module