Text of docs.python.org › 3.16 › library › xml.etree.elementtree.html as Styx crawled it on Oct 14, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

xml.etree.ElementTree — The ElementTree XML API — Python 3.16.0a1 documentation

Navigation index modules | next | previous | Python » 3.16.0a1 Documentation » The Python standard library » Structured Markup Processing Tools » xml.etree.ElementTree — The ElementTree XML API | Theme | xml.etree.ElementTree — The ElementTree XML API ¶ Source code: Lib/xml/etree/ElementTree.py The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available.

Deprecated since version 3.3: The xml.etree.cElementTree alias of this module is deprecated. Note If you need to parse untrusted or unauthenticated data, see XML security . Tutorial ¶ This is a short tutorial for using xml.etree.ElementTree ( ET in short). The goal is to demonstrate some of the building blocks and basic concepts of the module. XML tree and elements ¶ XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree.

ET has two classes for this purpose - ElementTree represents the whole XML document as a tree, and Element represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level. Parsing XML ¶ We’ll be using the fictive country_data.xml XML document as the sample data for this section: <?xml version="1.0"?> <data> <country name= "Liechtenstein" > <rank> 1 </rank> <year> 2008 </year> <gdppc> 141100 </gdppc> <neighbor name= "Austria" direction= "E" /> <neighbor name= "Switzerland" direction= "W" /> </country> <country name= "Singapore" > <rank> 4 </rank> <year> 2011 </year> <gdppc> 59900 </gdppc> <neighbor name= "Malaysia" direction= "N" /> </country> <country name= "Panama" > <rank> 68 </rank> <year> 2011 </year> <gdppc> 13600 </gdppc> <neighbor name= "Costa Rica" direction= "W" /> <neighbor name= "Colombia" direction= "E" /> </country> </data> We can import this data by reading from a file: import xml.etree.ElementTree as ET tree = ET . parse ( 'country_data.xml' ) root = tree . getroot () Or directly from a string: root = ET . fromstring ( country_data_as_string ) fromstring() parses XML from a string directly into an Element , which is the root element of the parsed tree.

Other parsing functions may create an ElementTree . Check the documentation to be sure. As an Element , root has a tag and a dictionary of attributes: >>> root . tag 'data' >>> root . attrib {} It also has children nodes over which we can iterate: >>> for child in root : ... print ( child . tag , child . attrib ) ... country {'name': 'Liechtenstein'} country {'name': 'Singapore'} country {'name': 'Panama'} Children are nested, and we can access specific child nodes by index: >>> root [ 0 ][ 1 ] . text '2008' Note Not all elements of the XML input will end up as elements of the parsed tree.

Currently, this module skips over any XML comments, processing instructions, and document type declarations in the input. Nevertheless, trees built using this module’s API rather than parsing from XML text can have comments and processing instructions in them; they will be included when generating XML output. A document type declaration may be accessed by passing a custom TreeBuilder instance to the XMLParser constructor.

Pull API for non-blocking parsing ¶ Most parsing functions provided by this module require the whole document to be read at once before returning any result. It is possible to use an XMLParser and feed data into it incrementally, but it is a push API that calls methods on a callback target, which is too low-level and inconvenient for most needs. Sometimes what the user really wants is to be able to parse XML incrementally, without blocking operations, while enjoying the convenience of fully constructed Element objects.

The most powerful tool for doing this is XMLPullParser . It does not require a blocking read to obtain the XML data, and is instead fed with data incrementally with XMLPullParser.feed() calls. To get the parsed XML elements, call XMLPullParser.read_events() . Here is an example: >>> parser = ET . XMLPullParser ([ 'start' , 'end' ]) >>> parser . feed ( '<mytag>sometext' ) >>> list ( parser . read_events ()) [('start', <Element 'mytag' at 0x7fa66db2be58>)] >>> parser . feed ( ' more text</mytag>' ) >>> for event , elem in parser . read_events (): ... print ( event ) ... print ( elem . tag , 'text=' , elem . text ) ... end mytag text= sometext more text The obvious use case is applications that operate in a non-blocking fashion where the XML data is being received from a socket or read incrementally from some storage device.

In such cases, blocking reads are unacceptable. Because it’s so flexible, XMLPullParser can be inconvenient to use for simpler use-cases. If you don’t mind your application blocking on reading XML data but would still like to have incremental parsing capabilities, take a look at iterparse() . Note that both parsers build the tree incrementally: it is not freed incrementally, so every parsed element is kept until the whole document is read.

To keep the memory usage low, get rid of the data which is not needed any more. If the processed elements are large, it is enough to clear them. This works wherever they are in the tree, but the emptied elements are left in it: for event , elem in ET . iterparse ( source ): if elem . tag == 'record' : process ( elem ) elem . clear () If an element has a large number of children, remove the processed children from it: for event , elem in ET . iterparse ( source , events = ( 'start' , 'end' )): if event == 'start' and elem . tag == 'parent' : parent = elem elif event == 'end' and elem . tag == 'child' : process ( elem ) parent . remove ( elem ) These examples are not universal, they only give an idea for two common cases.

If you do not need a tree at all, parse with XMLParser and a custom target instead; it is not built then, and nothing has to be removed. Where immediate feedback through events is wanted, calling method XMLPullParser.flush() can help reduce delay; please make sure to study the related security notes. Finding interesting elements ¶ Element has some useful methods that help iterate recursively over all the sub-tree below it (its children, their children, and so on).

For example, Element.iter() : >>> for neighbor in root . iter ( 'neighbor' ): ... print ( neighbor . attrib ) ... {'name': 'Austria', 'direction': 'E'} {'name': 'Switzerland', 'direction': 'W'} {'name': 'Malaysia', 'direction': 'N'} {'name': 'Costa Rica', 'direction': 'W'} {'name': 'Colombia', 'direction': 'E'} Element.findall() finds only elements with a tag which are direct children of the current element. Element.find() finds the first child with a particular tag, and Element.text accesses the element’s text content.

Element.get() accesses the element’s attributes: >>> for country in root . findall ( 'country' ): ... rank = country . find ( 'rank' ) . text ... name = country . get ( 'name' ) ... print ( name , rank ) ... Liechtenstein 1 Singapore 4 Panama 68 More sophisticated specification of which elements to look for is possible by using XPath . Modifying an XML File ¶ ElementTree provides a simple way to build XML documents and write them to files.

The ElementTree.write() method serves this purpose. Once created, an Element object may be manipulated by directly changing its fields (such as Element.text ), adding and modifying attributes ( Element.set() method), as well as adding new children (for example with Element.append() ). Let’s say we want to add one to each country’s rank, and add an updated attribute to the rank element: >>> for rank in root . iter ( 'rank' ): ... new_rank = int ( rank . text ) + 1 ... rank . text = str ( new_rank ) ... rank . set ( 'updated' , 'yes' ) ... >>> tree . write ( 'output.xml' ) Our XML now looks like this: <?xml version="1.0"?> <data> <country

The rest of this page is on the live site.