20.5.xml.etree.ElementTree — The ElementTree XML API

Source code:Lib/xml/etree/ElementTree.py


Thexml.etree.ElementTree module implements a simple and efficient APIfor parsing and creating XML data.

Changed in version 3.3:This module will use a fast implementation whenever available.Thexml.etree.cElementTree module is deprecated.

Warning

Thexml.etree.ElementTree module is not secure againstmaliciously constructed data. If you need to parse untrusted orunauthenticated data seeXML vulnerabilities.

20.5.1.Tutorial

This is a short tutorial for usingxml.etree.ElementTree (ET inshort). The goal is to demonstrate some of the building blocks and basicconcepts of the module.

20.5.1.1.XML tree and elements

XML is an inherently hierarchical data format, and the most natural way torepresent it is with a tree.ET has two classes for this purpose -ElementTree represents the whole XML document as a tree, andElement represents a single node in this tree. Interactions withthe whole document (reading and writing to/from files) are usually doneon theElementTree level. Interactions with a single XML elementand its sub-elements are done on theElement level.

20.5.1.2.Parsing XML

We’ll be using the following XML document as the sample data for this section:

<?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>

We can import this data by reading from a file:

importxml.etree.ElementTreeasETtree=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 anElement,which is the root element of the parsed tree. Other parsing functions maycreate anElementTree. Check the documentation to be sure.

As anElement,root has a tag and a dictionary of attributes:

>>>root.tag'data'>>>root.attrib{}

It also has children nodes over which we can iterate:

>>>forchildinroot:...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 theparsed tree. Currently, this module skips over any XML comments,processing instructions, and document type declarations in theinput. Nevertheless, trees built using this module’s API ratherthan parsing from XML text can have comments and processinginstructions in them; they will be included when generating XMLoutput. A document type declaration may be accessed by passing acustomTreeBuilder instance to theXMLParserconstructor.

20.5.1.3.Pull API for non-blocking parsing

Most parsing functions provided by this module require the whole documentto be read at once before returning any result. It is possible to use anXMLParser and feed data into it incrementally, but it is a push API thatcalls methods on a callback target, which is too low-level and inconvenient formost needs. Sometimes what the user really wants is to be able to parse XMLincrementally, without blocking operations, while enjoying the convenience offully constructedElement objects.

The most powerful tool for doing this isXMLPullParser. It does notrequire a blocking read to obtain the XML data, and is instead fed with dataincrementally withXMLPullParser.feed() calls. To get the parsed XMLelements, callXMLPullParser.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>')>>>forevent,eleminparser.read_events():...print(event)...print(elem.tag,'text=',elem.text)...end

The obvious use case is applications that operate in a non-blocking fashionwhere the XML data is being received from a socket or read incrementally fromsome storage device. In such cases, blocking reads are unacceptable.

Because it’s so flexible,XMLPullParser can be inconvenient to use forsimpler use-cases. If you don’t mind your application blocking on reading XMLdata but would still like to have incremental parsing capabilities, take a lookatiterparse(). It can be useful when you’re reading a large XML documentand don’t want to hold it wholly in memory.

20.5.1.4.Finding interesting elements

Element has some useful methods that help iterate recursively over allthe sub-tree below it (its children, their children, and so on). For example,Element.iter():

>>>forneighborinroot.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 directchildren of the current element.Element.find() finds thefirst childwith a particular tag, andElement.text accesses the element’s textcontent.Element.get() accesses the element’s attributes:

>>>forcountryinroot.findall('country'):...rank=country.find('rank').text...name=country.get('name')...print(name,rank)...Liechtenstein 1Singapore 4Panama 68

More sophisticated specification of which elements to look for is possible byusingXPath.

20.5.1.5.Modifying an XML File

ElementTree provides a simple way to build XML documents and write them to files.TheElementTree.write() method serves this purpose.

Once created, anElement object may be manipulated by directly changingits fields (such asElement.text), adding and modifying attributes(Element.set() method), as well as adding new children (for examplewithElement.append()).

Let’s say we want to add one to each country’s rank, and add anupdatedattribute to the rank element:

>>>forrankinroot.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><countryname="Liechtenstein"><rankupdated="yes">2</rank><year>2008</year><gdppc>141100</gdppc><neighborname="Austria"direction="E"/><neighborname="Switzerland"direction="W"/></country><countryname="Singapore"><rankupdated="yes">5</rank><year>2011</year><gdppc>59900</gdppc><neighborname="Malaysia"direction="N"/></country><countryname="Panama"><rankupdated="yes">69</rank><year>2011</year><gdppc>13600</gdppc><neighborname="Costa Rica"direction="W"/><neighborname="Colombia"direction="E"/></country></data>

We can remove elements usingElement.remove(). Let’s say we want toremove all countries with a rank higher than 50:

>>>forcountryinroot.findall('country'):...rank=int(country.find('rank').text)...ifrank>50:...root.remove(country)...>>>tree.write('output.xml')

Our XML now looks like this:

<?xml version="1.0"?><data><countryname="Liechtenstein"><rankupdated="yes">2</rank><year>2008</year><gdppc>141100</gdppc><neighborname="Austria"direction="E"/><neighborname="Switzerland"direction="W"/></country><countryname="Singapore"><rankupdated="yes">5</rank><year>2011</year><gdppc>59900</gdppc><neighborname="Malaysia"direction="N"/></country></data>

20.5.1.6.Building XML documents

TheSubElement() function also provides a convenient way to create newsub-elements for a given element:

>>>a=ET.Element('a')>>>b=ET.SubElement(a,'b')>>>c=ET.SubElement(a,'c')>>>d=ET.SubElement(c,'d')>>>ET.dump(a)<a><b /><c><d /></c></a>

20.5.1.7.Parsing XML with Namespaces

If the XML input hasnamespaces, tags and attributeswith prefixes in the formprefix:sometag get expanded to{uri}sometag where theprefix is replaced by the fullURI.Also, if there is adefault namespace,that full URI gets prepended to all of the non-prefixed tags.

Here is an XML example that incorporates two namespaces, one with theprefix “fictional” and the other serving as the default namespace:

<?xml version="1.0"?><actorsxmlns:fictional="http://characters.example.com"xmlns="http://people.example.com"><actor><name>JohnCleese</name><fictional:character>Lancelot</fictional:character><fictional:character>ArchieLeach</fictional:character></actor><actor><name>EricIdle</name><fictional:character>SirRobin</fictional:character><fictional:character>Gunther</fictional:character><fictional:character>CommanderClement</fictional:character></actor></actors>

One way to search and explore this XML example is to manually add theURI to every tag or attribute in the xpath of afind() orfindall():

root=fromstring(xml_text)foractorinroot.findall('{http://people.example.com}actor'):name=actor.find('{http://people.example.com}name')print(name.text)forcharinactor.findall('{http://characters.example.com}character'):print(' |-->',char.text)

A better way to search the namespaced XML example is to create adictionary with your own prefixes and use those in the search functions:

ns={'real_person':'http://people.example.com','role':'http://characters.example.com'}foractorinroot.findall('real_person:actor',ns):name=actor.find('real_person:name',ns)print(name.text)forcharinactor.findall('role:character',ns):print(' |-->',char.text)

These two approaches both output:

JohnCleese|-->Lancelot|-->ArchieLeachEricIdle|-->SirRobin|-->Gunther|-->CommanderClement

20.5.1.8.Additional resources

Seehttp://effbot.org/zone/element-index.htm for tutorials and links to otherdocs.

20.5.2.XPath support

This module provides limited support forXPath expressions for locating elements in atree. The goal is to support a small subset of the abbreviated syntax; a fullXPath engine is outside the scope of the module.

20.5.2.1.Example

Here’s an example that demonstrates some of the XPath capabilities of themodule. We’ll be using thecountrydata XML document from theParsing XML section:

importxml.etree.ElementTreeasETroot=ET.fromstring(countrydata)# Top-level elementsroot.findall(".")# All 'neighbor' grand-children of 'country' children of the top-level# elementsroot.findall("./country/neighbor")# Nodes with name='Singapore' that have a 'year' childroot.findall(".//year/..[@name='Singapore']")# 'year' nodes that are children of nodes with name='Singapore'root.findall(".//*[@name='Singapore']/year")# All 'neighbor' nodes that are the second child of their parentroot.findall(".//neighbor[2]")

20.5.2.2.Supported XPath syntax

Syntax

Meaning

tag

Selects all child elements with the given tag.For example,spam selects all child elementsnamedspam, andspam/egg selects allgrandchildren namedegg in all children namedspam.

*

Selects all child elements. For example,*/eggselects all grandchildren namedegg.

.

Selects the current node. This is mostly usefulat the beginning of the path, to indicate that it’sa relative path.

//

Selects all subelements, on all levels beneath thecurrent element. For example,.//egg selectsallegg elements in the entire tree.

..

Selects the parent element. ReturnsNone if thepath attempts to reach the ancestors of the startelement (the elementfind was called on).

[@attrib]

Selects all elements that have the given attribute.

[@attrib='value']

Selects all elements for which the given attributehas the given value. The value cannot containquotes.

[tag]

Selects all elements that have a child namedtag. Only immediate children are supported.

[tag='text']

Selects all elements that have a child namedtag whose complete text content, includingdescendants, equals the giventext.

[position]

Selects all elements that are located at the givenposition. The position can be either an integer(1 is the first position), the expressionlast()(for the last position), or a position relative tothe last position (e.g.last()-1).

Predicates (expressions within square brackets) must be preceded by a tagname, an asterisk, or another predicate.position predicates must bepreceded by a tag name.

20.5.3.Reference

20.5.3.1.Functions

xml.etree.ElementTree.Comment(text=None)

Comment element factory. This factory function creates a special elementthat will be serialized as an XML comment by the standard serializer. Thecomment string can be either a bytestring or a Unicode string.text is astring containing the comment string. Returns an element instancerepresenting a comment.

Note thatXMLParser skips over comments in the inputinstead of creating comment objects for them. AnElementTree willonly contain comment nodes if they have been inserted into tothe tree using one of theElement methods.

xml.etree.ElementTree.dump(elem)

Writes an element tree or element structure to sys.stdout. This functionshould be used for debugging only.

The exact output format is implementation dependent. In this version, it’swritten as an ordinary XML file.

elem is an element tree or an individual element.

xml.etree.ElementTree.fromstring(text)

Parses an XML section from a string constant. Same asXML().textis a string containing XML data. Returns anElement instance.

xml.etree.ElementTree.fromstringlist(sequence,parser=None)

Parses an XML document from a sequence of string fragments.sequence is alist or other sequence containing XML data fragments.parser is anoptional parser instance. If not given, the standardXMLParserparser is used. Returns anElement instance.

New in version 3.2.

xml.etree.ElementTree.iselement(element)

Checks if an object appears to be a valid element object.element is anelement instance. Returns a true value if this is an element object.

xml.etree.ElementTree.iterparse(source,events=None,parser=None)

Parses an XML section into an element tree incrementally, and reports what’sgoing on to the user.source is a filename orfile objectcontaining XML data.events is a sequence of events to report back. Thesupported events are the strings"start","end","start-ns" and"end-ns" (the “ns” events are used to get detailed namespaceinformation). Ifevents is omitted, only"end" events are reported.parser is an optional parser instance. If not given, the standardXMLParser parser is used.parser must be a subclass ofXMLParser and can only use the defaultTreeBuilder as atarget. Returns aniterator providing(event,elem) pairs.

Note that whileiterparse() builds the tree incrementally, it issuesblocking reads onsource (or the file it names). As such, it’s unsuitablefor applications where blocking reads can’t be made. For fully non-blockingparsing, seeXMLPullParser.

Note

iterparse() only guarantees that it has seen the “>” character of astarting tag when it emits a “start” event, so the attributes are defined,but the contents of the text and tail attributes are undefined at thatpoint. The same applies to the element children; they may or may not bepresent.

If you need a fully populated element, look for “end” events instead.

Deprecated since version 3.4:Theparser argument.

xml.etree.ElementTree.parse(source,parser=None)

Parses an XML section into an element tree.source is a filename or fileobject containing XML data.parser is an optional parser instance. Ifnot given, the standardXMLParser parser is used. Returns anElementTree instance.

xml.etree.ElementTree.ProcessingInstruction(target,text=None)

PI element factory. This factory function creates a special element thatwill be serialized as an XML processing instruction.target is a stringcontaining the PI target.text is a string containing the PI contents, ifgiven. Returns an element instance, representing a processing instruction.

Note thatXMLParser skips over processing instructionsin the input instead of creating comment objects for them. AnElementTree will only contain processing instruction nodes ifthey have been inserted into to the tree using one of theElement methods.

xml.etree.ElementTree.register_namespace(prefix,uri)

Registers a namespace prefix. The registry is global, and any existingmapping for either the given prefix or the namespace URI will be removed.prefix is a namespace prefix.uri is a namespace uri. Tags andattributes in this namespace will be serialized with the given prefix, if atall possible.

New in version 3.2.

xml.etree.ElementTree.SubElement(parent,tag,attrib={},**extra)

Subelement factory. This function creates an element instance, and appendsit to an existing element.

The element name, attribute names, and attribute values can be eitherbytestrings or Unicode strings.parent is the parent element.tag isthe subelement name.attrib is an optional dictionary, containing elementattributes.extra contains additional attributes, given as keywordarguments. Returns an element instance.

xml.etree.ElementTree.tostring(element,encoding="us-ascii",method="xml",*,short_empty_elements=True)

Generates a string representation of an XML element, including allsubelements.element is anElement instance.encoding1 isthe output encoding (default is US-ASCII). Useencoding="unicode" togenerate a Unicode string (otherwise, a bytestring is generated).methodis either"xml","html" or"text" (default is"xml").short_empty_elements has the same meaning as inElementTree.write().Returns an (optionally) encoded string containing the XML data.

New in version 3.4:Theshort_empty_elements parameter.

xml.etree.ElementTree.tostringlist(element,encoding="us-ascii",method="xml",*,short_empty_elements=True)

Generates a string representation of an XML element, including allsubelements.element is anElement instance.encoding1 isthe output encoding (default is US-ASCII). Useencoding="unicode" togenerate a Unicode string (otherwise, a bytestring is generated).methodis either"xml","html" or"text" (default is"xml").short_empty_elements has the same meaning as inElementTree.write().Returns a list of (optionally) encoded strings containing the XML data.It does not guarantee any specific sequence, except thatb"".join(tostringlist(element))==tostring(element).

New in version 3.2.

New in version 3.4:Theshort_empty_elements parameter.

xml.etree.ElementTree.XML(text,parser=None)

Parses an XML section from a string constant. This function can be used toembed “XML literals” in Python code.text is a string containing XMLdata.parser is an optional parser instance. If not given, the standardXMLParser parser is used. Returns anElement instance.

xml.etree.ElementTree.XMLID(text,parser=None)

Parses an XML section from a string constant, and also returns a dictionarywhich maps from element id:s to elements.text is a string containing XMLdata.parser is an optional parser instance. If not given, the standardXMLParser parser is used. Returns a tuple containing anElement instance and a dictionary.

20.5.3.2.Element Objects

classxml.etree.ElementTree.Element(tag,attrib={},**extra)

Element class. This class defines the Element interface, and provides areference implementation of this interface.

The element name, attribute names, and attribute values can be eitherbytestrings or Unicode strings.tag is the element name.attrib isan optional dictionary, containing element attributes.extra containsadditional attributes, given as keyword arguments.

tag

A string identifying what kind of data this element represents (theelement type, in other words).

text
tail

These attributes can be used to hold additional data associated withthe element. Their values are usually strings but may be anyapplication-specific object. If the element is created froman XML file, thetext attribute holds either the text betweenthe element’s start tag and its first child or end tag, orNone, andthetail attribute holds either the text between the element’send tag and the next tag, orNone. For the XML data

<a><b>1<c>2<d/>3</c></b>4</a>

thea element hasNone for bothtext andtail attributes,theb element hastext"1" andtail"4",thec element hastext"2" andtailNone,and thed element hastextNone andtail"3".

To collect the inner text of an element, seeitertext(), forexample"".join(element.itertext()).

Applications may store arbitrary objects in these attributes.

attrib

A dictionary containing the element’s attributes. Note that while theattrib value is always a real mutable Python dictionary, an ElementTreeimplementation may choose to use another internal representation, andcreate the dictionary only if someone asks for it. To take advantage ofsuch implementations, use the dictionary methods below whenever possible.

The following dictionary-like methods work on the element attributes.

clear()

Resets an element. This function removes all subelements, clears allattributes, and sets the text and tail attributes toNone.

get(key,default=None)

Gets the element attribute namedkey.

Returns the attribute value, ordefault if the attribute was not found.

items()

Returns the element attributes as a sequence of (name, value) pairs. Theattributes are returned in an arbitrary order.

keys()

Returns the elements attribute names as a list. The names are returnedin an arbitrary order.

set(key,value)

Set the attributekey on the element tovalue.

The following methods work on the element’s children (subelements).

append(subelement)

Adds the elementsubelement to the end of this element’s internal listof subelements. RaisesTypeError ifsubelement is not anElement.

extend(subelements)

Appendssubelements from a sequence object with zero or more elements.RaisesTypeError if a subelement is not anElement.

New in version 3.2.

find(match,namespaces=None)

Finds the first subelement matchingmatch.match may be a tag nameor apath. Returns an element instanceorNone.namespaces is an optional mapping from namespace prefixto full name.

findall(match,namespaces=None)

Finds all matching subelements, by tag name orpath. Returns a list containing all matchingelements in document order.namespaces is an optional mapping fromnamespace prefix to full name.

findtext(match,default=None,namespaces=None)

Finds text for the first subelement matchingmatch.match may bea tag name or apath. Returns the text contentof the first matching element, ordefault if no element was found.Note that if the matching element has no text content an empty stringis returned.namespaces is an optional mapping from namespace prefixto full name.

getchildren()

Deprecated since version 3.2:Uselist(elem) or iteration.

getiterator(tag=None)

Deprecated since version 3.2:Use methodElement.iter() instead.

insert(index,subelement)

Insertssubelement at the given position in this element. RaisesTypeError ifsubelement is not anElement.

iter(tag=None)

Creates a treeiterator with the current element as the root.The iterator iterates over this element and all elements below it, indocument (depth first) order. Iftag is notNone or'*', onlyelements whose tag equalstag are returned from the iterator. If thetree structure is modified during iteration, the result is undefined.

New in version 3.2.

iterfind(match,namespaces=None)

Finds all matching subelements, by tag name orpath. Returns an iterable yielding allmatching elements in document order.namespaces is an optional mappingfrom namespace prefix to full name.

New in version 3.2.

itertext()

Creates a text iterator. The iterator loops over this element and allsubelements, in document order, and returns all inner text.

New in version 3.2.

makeelement(tag,attrib)

Creates a new element object of the same type as this element. Do notcall this method, use theSubElement() factory function instead.

remove(subelement)

Removessubelement from the element. Unlike the find* methods thismethod compares elements based on the instance identity, not on tag valueor contents.

Element objects also support the following sequence type methodsfor working with subelements:__delitem__(),__getitem__(),__setitem__(),__len__().

Caution: Elements with no subelements will test asFalse. This behaviorwill change in future versions. Use specificlen(elem) orelemisNone test instead.

element=root.find('foo')ifnotelement:# careful!print("element not found, or element has no subelements")ifelementisNone:print("element not found")

20.5.3.3.ElementTree Objects

classxml.etree.ElementTree.ElementTree(element=None,file=None)

ElementTree wrapper class. This class represents an entire elementhierarchy, and adds some extra support for serialization to and fromstandard XML.

element is the root element. The tree is initialized with the contentsof the XMLfile if given.

_setroot(element)

Replaces the root element for this tree. This discards the currentcontents of the tree, and replaces it with the given element. Use withcare.element is an element instance.

find(match,namespaces=None)

Same asElement.find(), starting at the root of the tree.

findall(match,namespaces=None)

Same asElement.findall(), starting at the root of the tree.

findtext(match,default=None,namespaces=None)

Same asElement.findtext(), starting at the root of the tree.

getiterator(tag=None)

Deprecated since version 3.2:Use methodElementTree.iter() instead.

getroot()

Returns the root element for this tree.

iter(tag=None)

Creates and returns a tree iterator for the root element. The iteratorloops over all elements in this tree, in section order.tag is the tagto look for (default is to return all elements).

iterfind(match,namespaces=None)

Same asElement.iterfind(), starting at the root of the tree.

New in version 3.2.

parse(source,parser=None)

Loads an external XML section into this element tree.source is a filename orfile object.parser is an optional parser instance.If not given, the standardXMLParser parser is used. Returns thesection root element.

write(file,encoding="us-ascii",xml_declaration=None,default_namespace=None,method="xml",*,short_empty_elements=True)

Writes the element tree to a file, as XML.file is a file name, or afile object opened for writing.encoding1 is the outputencoding (default is US-ASCII).xml_declaration controls if an XML declaration should be added to thefile. UseFalse for never,True for always,Nonefor only if not US-ASCII or UTF-8 or Unicode (default isNone).default_namespace sets the default XML namespace (for “xmlns”).method is either"xml","html" or"text" (default is"xml").The keyword-onlyshort_empty_elements parameter controls the formattingof elements that contain no content. IfTrue (the default), they areemitted as a single self-closed tag, otherwise they are emitted as a pairof start/end tags.

The output is either a string (str) or binary (bytes).This is controlled by theencoding argument. Ifencoding is"unicode", the output is a string; otherwise, it’s binary. Note thatthis may conflict with the type offile if it’s an openfile object; make sure you do not try to write a string to abinary stream and vice versa.

New in version 3.4:Theshort_empty_elements parameter.

This is the XML file that is going to be manipulated:

<html><head><title>Examplepage</title></head><body><p>Movedto<ahref="http://example.org/">example.org</a>or<ahref="http://example.com/">example.com</a>.</p></body></html>

Example of changing the attribute “target” of every link in first paragraph:

>>>fromxml.etree.ElementTreeimportElementTree>>>tree=ElementTree()>>>tree.parse("index.xhtml")<Element 'html' at 0xb77e6fac>>>>p=tree.find("body/p")# Finds first occurrence of tag p in body>>>p<Element 'p' at 0xb77ec26c>>>>links=list(p.iter("a"))# Returns list of all links>>>links[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]>>>foriinlinks:# Iterates through all found links...i.attrib["target"]="blank">>>tree.write("output.xhtml")

20.5.3.4.QName Objects

classxml.etree.ElementTree.QName(text_or_uri,tag=None)

QName wrapper. This can be used to wrap a QName attribute value, in orderto get proper namespace handling on output.text_or_uri is a stringcontaining the QName value, in the form {uri}local, or, if the tag argumentis given, the URI part of a QName. Iftag is given, the first argument isinterpreted as a URI, and this argument is interpreted as a local name.QName instances are opaque.

20.5.3.5.TreeBuilder Objects

classxml.etree.ElementTree.TreeBuilder(element_factory=None)

Generic element structure builder. This builder converts a sequence ofstart, data, and end method calls to a well-formed element structure. Youcan use this class to build an element structure using a custom XML parser,or a parser for some other XML-like format.element_factory, when given,must be a callable accepting two positional arguments: a tag anda dict of attributes. It is expected to return a new element instance.

close()

Flushes the builder buffers, and returns the toplevel documentelement. Returns anElement instance.

data(data)

Adds text to the current element.data is a string. This should beeither a bytestring, or a Unicode string.

end(tag)

Closes the current element.tag is the element name. Returns theclosed element.

start(tag,attrs)

Opens a new element.tag is the element name.attrs is a dictionarycontaining element attributes. Returns the opened element.

In addition, a customTreeBuilder object can provide thefollowing method:

doctype(name,pubid,system)

Handles a doctype declaration.name is the doctype name.pubid isthe public identifier.system is the system identifier. This methoddoes not exist on the defaultTreeBuilder class.

New in version 3.2.

20.5.3.6.XMLParser Objects

classxml.etree.ElementTree.XMLParser(html=0,target=None,encoding=None)

This class is the low-level building block of the module. It usesxml.parsers.expat for efficient, event-based parsing of XML. It canbe fed XML data incrementally with thefeed() method, and parsingevents are translated to a push API - by invoking callbacks on thetargetobject. Iftarget is omitted, the standardTreeBuilder is used.Thehtml argument was historically used for backwards compatibility and isnow deprecated. Ifencoding1 is given, the value overrides theencoding specified in the XML file.

Deprecated since version 3.4:Thehtml argument. The remaining arguments should be passed viakeyword to prepare for the removal of thehtml argument.

close()

Finishes feeding data to the parser. Returns the result of calling theclose() method of thetarget passed during construction; by default,this is the toplevel document element.

doctype(name,pubid,system)

Deprecated since version 3.2:Define theTreeBuilder.doctype() method on a custom TreeBuildertarget.

feed(data)

Feeds data to the parser.data is encoded data.

XMLParser.feed() callstarget’sstart(tag,attrs_dict) methodfor each opening tag, itsend(tag) method for each closing tag, and datais processed by methoddata(data).XMLParser.close() callstarget’s methodclose().XMLParser can be used not only forbuilding a tree structure. This is an example of counting the maximum depthof an XML file:

>>>fromxml.etree.ElementTreeimportXMLParser>>>classMaxDepth:# The target object of the parser...maxDepth=0...depth=0...defstart(self,tag,attrib):# Called for each opening tag....self.depth+=1...ifself.depth>self.maxDepth:...self.maxDepth=self.depth...defend(self,tag):# Called for each closing tag....self.depth-=1...defdata(self,data):...pass# We do not need to do anything with data....defclose(self):# Called when all data has been parsed....returnself.maxDepth...>>>target=MaxDepth()>>>parser=XMLParser(target=target)>>>exampleXml="""...<a>...  <b>...  </b>...  <b>...    <c>...      <d>...      </d>...    </c>...  </b>...</a>""">>>parser.feed(exampleXml)>>>parser.close()4

20.5.3.7.XMLPullParser Objects

classxml.etree.ElementTree.XMLPullParser(events=None)

A pull parser suitable for non-blocking applications. Its input-side API issimilar to that ofXMLParser, but instead of pushing calls to acallback target,XMLPullParser collects an internal list of parsingevents and lets the user read from it.events is a sequence of events toreport back. The supported events are the strings"start","end","start-ns" and"end-ns" (the “ns” events are used to get detailednamespace information). Ifevents is omitted, only"end" events arereported.

feed(data)

Feed the given bytes data to the parser.

close()

Signal the parser that the data stream is terminated. UnlikeXMLParser.close(), this method always returnsNone.Any events not yet retrieved when the parser is closed can still beread withread_events().

read_events()

Return an iterator over the events which have been encountered in thedata fed to theparser. The iterator yields(event,elem) pairs, whereevent is astring representing the type of event (e.g."end") andelem is theencounteredElement object.

Events provided in a previous call toread_events() will not beyielded again. Events are consumed from the internal queue only whenthey are retrieved from the iterator, so multiple readers iterating inparallel over iterators obtained fromread_events() will haveunpredictable results.

Note

XMLPullParser only guarantees that it has seen the “>”character of a starting tag when it emits a “start” event, so theattributes are defined, but the contents of the text and tail attributesare undefined at that point. The same applies to the element children;they may or may not be present.

If you need a fully populated element, look for “end” events instead.

New in version 3.4.

20.5.3.8.Exceptions

classxml.etree.ElementTree.ParseError

XML parse error, raised by the various parsing methods in this module whenparsing fails. The string representation of an instance of this exceptionwill contain a user-friendly error message. In addition, it will havethe following attributes available:

code

A numeric error code from the expat parser. See the documentation ofxml.parsers.expat for the list of error codes and their meanings.

position

A tuple ofline,column numbers, specifying where the error occurred.

Footnotes

1(1,2,3,4)

The encoding string included in XML output should conform to theappropriate standards. For example, “UTF-8” is valid, but “UTF8” isnot. Seehttps://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDeclandhttps://www.iana.org/assignments/character-sets/character-sets.xhtml.