XML processing Module

Source: Internet
Author: User

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

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.