python-manipulating XML-formatted files

Source: Internet
Author: User

The format of the XML file is as follows:

<Data>    <Countryname= "Liechtenstein">        <RankUpdated= "Yes">2</Rank>        < YearUpdated= "Yes">2009</ Year>        <GDPPC>141100</GDPPC>        <Neighbordirection= "E"name= "Austria" />        <Neighbordirection= "W"name= "Switzerland" />    </Country>    <Countryname= "Singapore">        <RankUpdated= "Yes">5</Rank>        < YearUpdated= "Yes">2012</ Year>        <GDPPC>59900</GDPPC>        <Neighbordirection= "N"name= "Malaysia" />    </Country>    <Countryname= "Panama">        <RankUpdated= "Yes">69</Rank>        < YearUpdated= "Yes">2012</ Year>        <GDPPC>13600</GDPPC>        <Neighbordirection= "W"name= "Costa Rica" />        <Neighbordirection= "E"name= "Colombia" />    </Country></Data>

#@File: 4.8.1xml module. PY#@Software: PycharmImportXml.etree.ElementTree as Ettree= Et.parse ('Xmltest.xml') Root=tree.getroot ()Print(Root.tag)#Get tag name forChildinchRoot#traverse each layer of labels    Print('----------', Child.tag, Child.attrib) forIinchChild#Traverse Sub-labels and values under each layer label        Print(I.tag, I.text)Print('-------------------------------------------')#Traverse only the year node forNodeinchRoot.iter (' Year'):    Print(Node.tag, Node.text)
#@File: 4.8.2xml file additions and deletions change. PY#@Software: PycharmImportXml.etree.ElementTree as ET#Change value OperationTree = Et.parse ('Xmltest.xml') Root= Tree.getroot ()#f.seek (0) forNodeinchRoot.iter (' Year'): New_year= Int (node.text) + 1Node.text=Str (new_year) Node.set ('Updated','Yes') Tree.write ("Xmltest.xml")#Delete Value ActionTree = Et.parse ('Xmltest.xml') Root= Tree.getroot ()#f.seek (0) forCountryinchRoot.findall ('Country'): Rank= Int (Country.find ('Rank'). Text)ifRank > 50: Root.remove (country)Print('----------------------') forNodeinchRoot.iter ('Rank'):    Print(Node.tag, Node.text) tree.write ('Output.xml')
#@File: 4.8.3 automatically creates an XML file. py#@Software: PycharmImportXml.etree.ElementTree as Etnew_xml= ET. Element ('country_list')#Generate a node name#generate a 1th XML tagName = ET. Subelement (New_xml,'name', attrib={'enrolled':'Yes'}) age= ET. subelement (Name,' Age', attrib={'checked':'No'}) Sex= ET. subelement (Name,'Sex') Sex.text=' -'#generate a 2nd XML tagName2 = ET. Subelement (New_xml,'name', attrib={'enrolled':'Yes'}) age= ET. Subelement (Name2,' Age', attrib={'checked':'No'}) age= ET. Subelement (Name2,'Sex') Age.text=' +'et= ET. ElementTree (New_xml)#Generate ArchivesEt.write ('Test.xml', encoding='Utf-8', xml_declaration=True) Et.dump (New_xml)#print the generated format

python-manipulating XML-formatted files

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.