Python uses ElementTree to process XML files

Source: Internet
Author: User

In Python, as a general rule, we should use elementtree to process XML files,ElementTree from Python 2.5 as a standard module. In general, the XML file size is moderate and the performance requirements are not very stringent.

The following explains how to manipulate XML through ElementTree;

1. Introduction of libraries
You need to use 3 classes, elementtree,element, and wrapper classes that create subclasses subelement

Try:    import xml.etree.cElementTree as etexcept importerror:    import Xml.etree.ElementTree as ET
Priority is to use more efficient CElementtree,celementtree is the Cython implementation of ElementTree, faster, consumes less memory, performance is more dominant, in the actual use of the process should try to prioritize the use of celementtree.

2. Read in and parse
Tree = ElementTree (file=xmlfile)
root = Tree.getroot ()
After reading, the tree is the type of ElementTree, obtaining the XML root node using the Getroot () method;

XML sample file:
<item sid= ' 1712 ' name = ' Big cc ' >
<a id=1></a>
<a id=2></a>
</item>


3. Get your son's knot point
Find all child nodes of element:
Aarry = Item.findall (' a ')
You can also use GetChildren ():
Childs = Item.getchildren ()
For subitem in Childs:
Print subitem.get (' id ')


4. Insert your son's knot
Method One:
Item = Element ("item", {' Sid ': ' 1713 ', ' name ': ' Ityouhui '})
Root.append (item)
Method Two:
SE (Root, ' item ', {' sid ': ' 1713 ', ' name ': ' Ityouhui '})
The benefit of Fayi is that you can continue the operation on item after you insert it. Law two is simple in writing, in which SE is subelement, making a statement in the introduction;


5. Operation Properties
Get a property value for element (eg: get the name of item)
Print Root.find (' Item/name '). Text
Print item.get (' name ')
Get all attributes of element
Print Item.items () # [(' Sid ', ' 1712 '), (' Name ', ' Big cc ')]
Print Item.attrib # {' sid ': ' 1712 ', ' name ': ' Big cc '}




This article explains how to manipulate XML through ElementTree.

1. Introduction of libraries
You need to use 3 classes, elementtree,element, and wrapper classes that create subclasses subelement
From Xml.etree.ElementTree import ElementTree
From Xml.etree.ElementTree import Element
From Xml.etree.ElementTree import subelement as SE


2. Read in and parse
Tree = ElementTree (file=xmlfile)
root = Tree.getroot ()
After reading, the tree is the type of ElementTree, obtaining the XML root node using the Getroot () method;

XML sample file:
<item sid= ' 1712 ' name = ' Big cc ' >
<a id=1></a>
<a id=2></a>
</item>


3. Get your son's knot point
Find all child nodes of element:
Aarry = Item.findall (' a ')
You can also use GetChildren ():
Childs = Item.getchildren ()
For subitem in Childs:
Print subitem.get (' id ')


4. Insert your son's knot
Method One:
Item = Element ("item", {' Sid ': ' 1713 ', ' name ': ' Ityouhui '})
Root.append (item)
Method Two:
SE (Root, ' item ', {' sid ': ' 1713 ', ' name ': ' Ityouhui '})
The benefit of Fayi is that you can continue the operation on item after you insert it. Law two is simple in writing, in which SE is subelement, making a statement in the introduction;


5. Operation Properties
Get a property value for element (eg: get the name of item)
Print Root.find (' Item/name '). Text
Print item.get (' name ')
Get all attributes of element
Print Item.items () # [(' Sid ', ' 1712 '), (' Name ', ' Big cc ')]
Print Item.attrib # {' sid ': ' 1712 ', ' name ': ' Big cc '}



Python uses ElementTree to process XML files

Related Article

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.