Python XML Operations

Source: Internet
Author: User
Tags tag name

XML (Extensible Markup Language) is a very common type of file that is used primarily for storing and transmitting data. In programming, the operation of XML is also very common.

This article describes the parsing of XML according to the Xml.etree.ElementTree class in the Python library document: https://docs.python.org/3.5/library/xml.etree.elementtree.html

The Btw,xml.etree.celementtree module has been deprecated since 3.3.

XML format

First, take a look at the types of elements that XML contains

1. Labels <tag>

2. Properties <tag Name= "attribute" >

3. Data <data>1<data>

For example, XML segment:

<?XML version= "1.0"?><Data>    <Countryname= "Liechtenstein">        <Rank>1</Rank>        < Year>2008</ Year>        <GDPPC>141100</GDPPC>        <Neighborname= "Austria"direction= "E"/>        <Neighborname= "Switzerland"direction= "W"/>    </Country>    <Countryname= "Singapore">        <Rank>4</Rank>        < Year>2011</ Year>        <GDPPC>59900</GDPPC>        <Neighborname= "Malaysia"direction= "N"/>    </Country>    <Countryname= "Panama">        <Rank>68</Rank>        < Year>2011</ Year>        <GDPPC>13600</GDPPC>        <Neighborname= "Costa Rica"direction= "W"/>        <Neighborname= "Colombia"direction= "E"/>    </Country></Data>

XML operations
    • Read
#从变量读取, the argument is an XML segment, which returns a root element object , root = et.fromstring (country_data_as_string)#从xml文件中读取, Get root node with Getroot, root node is also element object tree = et.parse ('file.xml'= Tree.getroot ()
    • Access
      • Accessing the tags, properties, and values of the element object
Tag == = Element.text
      • Accessing child nodes
# print the label and properties of the root node, get  for inch Root:     Print (Child.tag, Child.attrib)
    • Find operations
      • Element elements iterate over child elements: Element.iter ("tag"), which can list all other nodes that the node contains (element object)
# prints the Name property of all neighbor objects in the root node  for  in Root.iter ('neighbor'):    Print( neighbor.attrib['name'])
      • Element.findall ("tag"): Finds the immediate child element of the current element as "tag"
#FindAll can only be used to find direct child elements and cannot be used to find element Rank,neighbor etc. forCountryinchRoot.findall ('Country'): Rank= Country.find ('Rank'). Text name= Country.find ('Rank'). Text Neig= Country.find ('Neighbor'). AttribPrint(rank, Name,neig)
      • Element.find ("tag"): Find the first Direct child element for tag
# returns the element with the first tag country, if not, returns noneFirstcountry = Root.find ("country")  Print(firstcountry)
    • Creating an XML file
__author__='Xua'ImportXml.etree.ElementTree as ET#Creating the root nodeA = ET. Element ("Root")#Create child nodes, and add propertiesb = ET. Subelement (A,"sub1") B.attrib= {"name":"Name Attribute"}#Create child nodes, and add datac = ET. Subelement (A,"Sub2") C.text="Test"#create ElementTree object, write fileTree =ET. ElementTree (a) tree.write ("Test.xml")

The new file content created is:<root><sub1 name= "Name attribute"/><sub2>test</sub2></root>

    • Modifying an XML file
      • Elementtree.write ("XMLFile"): Update XML file
      • Element.append (): Adds a child element to the current element object (element)
      • Element.set (key,value): Set value value for the key property of the current element
      • Element.remove (Element): Delete the node as element
#read files to be modifiedUpdatetree = Et.parse ("Test.xml") Root=updatetree.getroot ()#Create a new node and add the child node as rootNewele = ET. Element ("newelement") Newele.attrib= {"name":"newelement"," Age":" -"}newele.text="This is a new element"root.append (Newele)#Modify the Name property of the Sub1Sub1 = Root.find ("sub1") Sub1.set ("name","New Name")#Modifying the data values of a sub2Sub2 = Root.find ("Sub2") Sub2.text="New Value"#write back the original fileUpdatetree.write ("Test.xml")

The updated files are: <root><sub1 name= "new name"/><sub2>new Value</sub2>< Newelement age= "name=" newelement ">this is a new element</newelement></root>

Summarize

XML operation is more common, of course, there are many third-party libraries can be used, the need to do nothing more than the usual read and write XML files, element node additions and deletions, you can also learn more on the official Python document.

Https://docs.python.org/3.5/library/xml.etree.elementtree.html

Python XML Operations

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.