blob: 51ff1ee58ad26bb38b53e597f804228da38fd99e [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`xml.etree.ElementTree` --- The ElementTree XML API
2========================================================
3
4.. module:: xml.etree.ElementTree
5 :synopsis: Implementation of the ElementTree API.
6.. moduleauthor:: Fredrik Lundh <fredrik@pythonware.com>
7
Eli Benderskyc1d98692012-03-30 11:44:15 +03008The :mod:`xml.etree.ElementTree` module implements a simple and efficient API
9for parsing and creating XML data.
Florent Xiclunaf15351d2010-03-13 23:24:31 +000010
Florent Xiclunaa72a98f2012-02-13 11:03:30 +010011.. versionchanged:: 3.3
12 This module will use a fast implementation whenever available.
13 The :mod:`xml.etree.cElementTree` module is deprecated.
14
Eli Benderskyc1d98692012-03-30 11:44:15 +030015Tutorial
16--------
Georg Brandl116aa622007-08-15 14:28:22 +000017
Eli Benderskyc1d98692012-03-30 11:44:15 +030018This is a short tutorial for using :mod:`xml.etree.ElementTree` (``ET`` in
19short). The goal is to demonstrate some of the building blocks and basic
20concepts of the module.
Eli Bendersky3a4875e2012-03-26 20:43:32 +020021
Eli Benderskyc1d98692012-03-30 11:44:15 +030022XML tree and elements
23^^^^^^^^^^^^^^^^^^^^^
Eli Bendersky3a4875e2012-03-26 20:43:32 +020024
Eli Benderskyc1d98692012-03-30 11:44:15 +030025XML is an inherently hierarchical data format, and the most natural way to
26represent it is with a tree. ``ET`` has two classes for this purpose -
27:class:`ElementTree` represents the whole XML document as a tree, and
28:class:`Element` represents a single node in this tree. Interactions with
29the whole document (reading and writing to/from files) are usually done
30on the :class:`ElementTree` level. Interactions with a single XML element
31and its sub-elements are done on the :class:`Element` level.
Eli Bendersky3a4875e2012-03-26 20:43:32 +020032
Eli Benderskyc1d98692012-03-30 11:44:15 +030033.. _elementtree-parsing-xml:
Eli Bendersky3a4875e2012-03-26 20:43:32 +020034
Eli Benderskyc1d98692012-03-30 11:44:15 +030035Parsing XML
36^^^^^^^^^^^
Eli Bendersky3a4875e2012-03-26 20:43:32 +020037
Eli Benderskyc1d98692012-03-30 11:44:15 +030038We'll be using the following XML document contained in a Python string as the
39sample data for this section::
Eli Bendersky3a4875e2012-03-26 20:43:32 +020040
Eli Benderskyc1d98692012-03-30 11:44:15 +030041 countrydata = r'''<?xml version="1.0"?>
Eli Bendersky3a4875e2012-03-26 20:43:32 +020042 <data>
43 <country name="Liechtenshtein">
44 <rank>1</rank>
45 <year>2008</year>
46 <gdppc>141100</gdppc>
47 <neighbor name="Austria" direction="E"/>
48 <neighbor name="Switzerland" direction="W"/>
49 </country>
50 <country name="Singapore">
51 <rank>4</rank>
52 <year>2011</year>
53 <gdppc>59900</gdppc>
54 <neighbor name="Malaysia" direction="N"/>
55 </country>
56 <country name="Panama">
57 <rank>68</rank>
58 <year>2011</year>
59 <gdppc>13600</gdppc>
60 <neighbor name="Costa Rica" direction="W"/>
61 <neighbor name="Colombia" direction="E"/>
62 </country>
63 </data>
64 '''
65
Eli Benderskyc1d98692012-03-30 11:44:15 +030066First, import the module and parse the data::
67
68 import xml.etree.ElementTree as ET
69
70 root = ET.fromstring(countrydata)
71
72:func:`fromstring` parses XML from a string directly into an :class:`Element`,
73which is the root element of the parsed tree. Other parsing functions may
74create an :class:`ElementTree`. Make sure to check the documentation to be
75sure.
76
77As an :class:`Element`, ``root`` has a tag and a dictionary of attributes::
78
79 >>> root.tag
80 'data'
81 >>> root.attrib
82 {}
83
84It also has children nodes over which we can iterate::
85
86 >>> for child in root:
87 ... print(child.tag, child.attrib)
88 ...
89 country {'name': 'Liechtenshtein'}
90 country {'name': 'Singapore'}
91 country {'name': 'Panama'}
92
93Children are nested, and we can access specific child nodes by index::
94
95 >>> root[0][1].text
96 '2008'
97
98Finding interesting elements
99^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100
101:class:`Element` has some useful methods that help iterate recursively over all
102the sub-tree below it (its children, their children, and so on). For example,
103:meth:`Element.iter`::
104
105 >>> for neighbor in root.iter('neighbor'):
106 ... print(neighbor.attrib)
107 ...
108 {'name': 'Austria', 'direction': 'E'}
109 {'name': 'Switzerland', 'direction': 'W'}
110 {'name': 'Malaysia', 'direction': 'N'}
111 {'name': 'Costa Rica', 'direction': 'W'}
112 {'name': 'Colombia', 'direction': 'E'}
113
114More sophisticated specification of which elements to look for is possible by
115using :ref:`XPath <elementtree-xpath>`.
116
117Building XML documents
118^^^^^^^^^^^^^^^^^^^^^^
119
120``ET`` provides a simple way to build XML documents and write them to files.
121The :meth:`ElementTree.write` method serves this purpose.
122
123Once created, an :class:`Element` object may be manipulated by directly changing
124its fields (such as :attr:`Element.text`), adding and modifying attributes
125(:meth:`Element.set` method), as well as adding new children (for example
126with :meth:`Element.append`).
127
128The :func:`SubElement` function also provides a convenient way to create new
129sub-elements for a given element::
130
131 >>> a = ET.Element('a')
132 >>> b = ET.SubElement(a, 'b')
133 >>> c = ET.SubElement(a, 'c')
134 >>> d = ET.SubElement(c, 'd')
135 >>> ET.dump(a)
136 <a><b /><c><d /></c></a>
137
138Additional resources
139^^^^^^^^^^^^^^^^^^^^
140
141See http://effbot.org/zone/element-index.htm for tutorials and links to other
142docs.
143
144
145.. _elementtree-xpath:
146
147XPath support
148-------------
149
150This module provides limited support for
151`XPath expressions <http://www.w3.org/TR/xpath>`_ for locating elements in a
152tree. The goal is to support a small subset of the abbreviated syntax; a full
153XPath engine is outside the scope of the module.
154
155Example
156^^^^^^^
157
158Here's an example that demonstrates some of the XPath capabilities of the
159module. We'll be using the ``countrydata`` XML document from the
160:ref:`Parsing XML <elementtree-parsing-xml>` section::
161
162 import xml.etree.ElementTree as ET
163
164 root = ET.fromstring(countrydata)
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200165
166 # Top-level elements
Eli Benderskyc1d98692012-03-30 11:44:15 +0300167 root.findall(".")
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200168
169 # All 'neighbor' grand-children of 'country' children of the top-level
170 # elements
Eli Benderskyc1d98692012-03-30 11:44:15 +0300171 root.findall("./country/neighbor")
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200172
173 # Nodes with name='Singapore' that have a 'year' child
Eli Benderskyc1d98692012-03-30 11:44:15 +0300174 root.findall(".//year/..[@name='Singapore']")
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200175
176 # 'year' nodes that are children of nodes with name='Singapore'
Eli Benderskyc1d98692012-03-30 11:44:15 +0300177 root.findall(".//*[@name='Singapore']/year")
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200178
179 # All 'neighbor' nodes that are the second child of their parent
Eli Benderskyc1d98692012-03-30 11:44:15 +0300180 root.findall(".//neighbor[2]")
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200181
182Supported XPath syntax
183^^^^^^^^^^^^^^^^^^^^^^
184
185+-----------------------+------------------------------------------------------+
186| Syntax | Meaning |
187+=======================+======================================================+
188| ``tag`` | Selects all child elements with the given tag. |
189| | For example, ``spam`` selects all child elements |
190| | named ``spam``, ``spam/egg`` selects all |
191| | grandchildren named ``egg`` in all children named |
192| | ``spam``. |
193+-----------------------+------------------------------------------------------+
194| ``*`` | Selects all child elements. For example, ``*/egg`` |
195| | selects all grandchildren named ``egg``. |
196+-----------------------+------------------------------------------------------+
197| ``.`` | Selects the current node. This is mostly useful |
198| | at the beginning of the path, to indicate that it's |
199| | a relative path. |
200+-----------------------+------------------------------------------------------+
201| ``//`` | Selects all subelements, on all levels beneath the |
Eli Benderskyede001a2012-03-27 04:57:23 +0200202| | current element. For example, ``.//egg`` selects |
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200203| | all ``egg`` elements in the entire tree. |
204+-----------------------+------------------------------------------------------+
205| ``..`` | Selects the parent element. |
206+-----------------------+------------------------------------------------------+
207| ``[@attrib]`` | Selects all elements that have the given attribute. |
208+-----------------------+------------------------------------------------------+
209| ``[@attrib='value']`` | Selects all elements for which the given attribute |
210| | has the given value. The value cannot contain |
211| | quotes. |
212+-----------------------+------------------------------------------------------+
213| ``[tag]`` | Selects all elements that have a child named |
214| | ``tag``. Only immediate children are supported. |
215+-----------------------+------------------------------------------------------+
216| ``[position]`` | Selects all elements that are located at the given |
217| | position. The position can be either an integer |
218| | (1 is the first position), the expression ``last()`` |
219| | (for the last position), or a position relative to |
220| | the last position (e.g. ``last()-1``). |
221+-----------------------+------------------------------------------------------+
222
223Predicates (expressions within square brackets) must be preceded by a tag
224name, an asterisk, or another predicate. ``position`` predicates must be
225preceded by a tag name.
226
227Reference
228---------
229
Georg Brandl116aa622007-08-15 14:28:22 +0000230.. _elementtree-functions:
231
232Functions
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200233^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235
Georg Brandl7f01a132009-09-16 15:58:14 +0000236.. function:: Comment(text=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Georg Brandlf6945182008-02-01 11:56:49 +0000238 Comment element factory. This factory function creates a special element
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000239 that will be serialized as an XML comment by the standard serializer. The
240 comment string can be either a bytestring or a Unicode string. *text* is a
241 string containing the comment string. Returns an element instance
Georg Brandlf6945182008-02-01 11:56:49 +0000242 representing a comment.
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244
245.. function:: dump(elem)
246
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000247 Writes an element tree or element structure to sys.stdout. This function
248 should be used for debugging only.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250 The exact output format is implementation dependent. In this version, it's
251 written as an ordinary XML file.
252
253 *elem* is an element tree or an individual element.
254
255
Georg Brandl116aa622007-08-15 14:28:22 +0000256.. function:: fromstring(text)
257
Florent Xiclunadddd5e92010-03-14 01:28:07 +0000258 Parses an XML section from a string constant. Same as :func:`XML`. *text*
259 is a string containing XML data. Returns an :class:`Element` instance.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000260
261
262.. function:: fromstringlist(sequence, parser=None)
263
264 Parses an XML document from a sequence of string fragments. *sequence* is a
265 list or other sequence containing XML data fragments. *parser* is an
266 optional parser instance. If not given, the standard :class:`XMLParser`
267 parser is used. Returns an :class:`Element` instance.
268
Ezio Melottif8754a62010-03-21 07:16:43 +0000269 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
272.. function:: iselement(element)
273
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000274 Checks if an object appears to be a valid element object. *element* is an
275 element instance. Returns a true value if this is an element object.
Georg Brandl116aa622007-08-15 14:28:22 +0000276
277
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000278.. function:: iterparse(source, events=None, parser=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000279
280 Parses an XML section into an element tree incrementally, and reports what's
Eli Bendersky604c4ff2012-03-16 08:41:30 +0200281 going on to the user. *source* is a filename or :term:`file object`
282 containing XML data. *events* is a list of events to report back. The
283 supported events are the strings ``"start"``, ``"end"``, ``"start-ns"``
284 and ``"end-ns"`` (the "ns" events are used to get detailed namespace
285 information). If *events* is omitted, only ``"end"`` events are reported.
286 *parser* is an optional parser instance. If not given, the standard
287 :class:`XMLParser` parser is used. Returns an :term:`iterator` providing
288 ``(event, elem)`` pairs.
Georg Brandl116aa622007-08-15 14:28:22 +0000289
Benjamin Peterson75edad02009-01-01 15:05:06 +0000290 .. note::
291
292 :func:`iterparse` only guarantees that it has seen the ">"
293 character of a starting tag when it emits a "start" event, so the
294 attributes are defined, but the contents of the text and tail attributes
295 are undefined at that point. The same applies to the element children;
296 they may or may not be present.
297
298 If you need a fully populated element, look for "end" events instead.
299
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Georg Brandl7f01a132009-09-16 15:58:14 +0000301.. function:: parse(source, parser=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000302
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000303 Parses an XML section into an element tree. *source* is a filename or file
304 object containing XML data. *parser* is an optional parser instance. If
305 not given, the standard :class:`XMLParser` parser is used. Returns an
306 :class:`ElementTree` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
308
Georg Brandl7f01a132009-09-16 15:58:14 +0000309.. function:: ProcessingInstruction(target, text=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000310
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000311 PI element factory. This factory function creates a special element that
312 will be serialized as an XML processing instruction. *target* is a string
313 containing the PI target. *text* is a string containing the PI contents, if
314 given. Returns an element instance, representing a processing instruction.
315
316
317.. function:: register_namespace(prefix, uri)
318
319 Registers a namespace prefix. The registry is global, and any existing
320 mapping for either the given prefix or the namespace URI will be removed.
321 *prefix* is a namespace prefix. *uri* is a namespace uri. Tags and
322 attributes in this namespace will be serialized with the given prefix, if at
323 all possible.
324
Ezio Melottif8754a62010-03-21 07:16:43 +0000325 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000326
327
Georg Brandl7f01a132009-09-16 15:58:14 +0000328.. function:: SubElement(parent, tag, attrib={}, **extra)
Georg Brandl116aa622007-08-15 14:28:22 +0000329
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000330 Subelement factory. This function creates an element instance, and appends
331 it to an existing element.
Georg Brandl116aa622007-08-15 14:28:22 +0000332
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000333 The element name, attribute names, and attribute values can be either
334 bytestrings or Unicode strings. *parent* is the parent element. *tag* is
335 the subelement name. *attrib* is an optional dictionary, containing element
336 attributes. *extra* contains additional attributes, given as keyword
337 arguments. Returns an element instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000338
339
Florent Xiclunac17f1722010-08-08 19:48:29 +0000340.. function:: tostring(element, encoding="us-ascii", method="xml")
Georg Brandl116aa622007-08-15 14:28:22 +0000341
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000342 Generates a string representation of an XML element, including all
Florent Xiclunadddd5e92010-03-14 01:28:07 +0000343 subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is
Florent Xiclunac17f1722010-08-08 19:48:29 +0000344 the output encoding (default is US-ASCII). Use ``encoding="unicode"`` to
345 generate a Unicode string. *method* is either ``"xml"``,
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000346 ``"html"`` or ``"text"`` (default is ``"xml"``). Returns an (optionally)
347 encoded string containing the XML data.
Georg Brandl116aa622007-08-15 14:28:22 +0000348
349
Florent Xiclunac17f1722010-08-08 19:48:29 +0000350.. function:: tostringlist(element, encoding="us-ascii", method="xml")
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000351
352 Generates a string representation of an XML element, including all
Florent Xiclunadddd5e92010-03-14 01:28:07 +0000353 subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is
Florent Xiclunac17f1722010-08-08 19:48:29 +0000354 the output encoding (default is US-ASCII). Use ``encoding="unicode"`` to
355 generate a Unicode string. *method* is either ``"xml"``,
Florent Xiclunadddd5e92010-03-14 01:28:07 +0000356 ``"html"`` or ``"text"`` (default is ``"xml"``). Returns a list of
357 (optionally) encoded strings containing the XML data. It does not guarantee
358 any specific sequence, except that ``"".join(tostringlist(element)) ==
359 tostring(element)``.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000360
Ezio Melottif8754a62010-03-21 07:16:43 +0000361 .. versionadded:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000362
363
364.. function:: XML(text, parser=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366 Parses an XML section from a string constant. This function can be used to
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000367 embed "XML literals" in Python code. *text* is a string containing XML
368 data. *parser* is an optional parser instance. If not given, the standard
369 :class:`XMLParser` parser is used. Returns an :class:`Element` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000372.. function:: XMLID(text, parser=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374 Parses an XML section from a string constant, and also returns a dictionary
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000375 which maps from element id:s to elements. *text* is a string containing XML
376 data. *parser* is an optional parser instance. If not given, the standard
377 :class:`XMLParser` parser is used. Returns a tuple containing an
378 :class:`Element` instance and a dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +0000379
380
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000381.. _elementtree-element-objects:
Georg Brandl116aa622007-08-15 14:28:22 +0000382
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000383Element Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200384^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000386.. class:: Element(tag, attrib={}, **extra)
Georg Brandl116aa622007-08-15 14:28:22 +0000387
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000388 Element class. This class defines the Element interface, and provides a
389 reference implementation of this interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000391 The element name, attribute names, and attribute values can be either
392 bytestrings or Unicode strings. *tag* is the element name. *attrib* is
393 an optional dictionary, containing element attributes. *extra* contains
394 additional attributes, given as keyword arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
396
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000397 .. attribute:: tag
Georg Brandl116aa622007-08-15 14:28:22 +0000398
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000399 A string identifying what kind of data this element represents (the
400 element type, in other words).
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000403 .. attribute:: text
Georg Brandl116aa622007-08-15 14:28:22 +0000404
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000405 The *text* attribute can be used to hold additional data associated with
406 the element. As the name implies this attribute is usually a string but
407 may be any application-specific object. If the element is created from
408 an XML file the attribute will contain any text found between the element
409 tags.
Georg Brandl116aa622007-08-15 14:28:22 +0000410
411
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000412 .. attribute:: tail
Georg Brandl116aa622007-08-15 14:28:22 +0000413
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000414 The *tail* attribute can be used to hold additional data associated with
415 the element. This attribute is usually a string but may be any
416 application-specific object. If the element is created from an XML file
417 the attribute will contain any text found after the element's end tag and
418 before the next tag.
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000421 .. attribute:: attrib
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000423 A dictionary containing the element's attributes. Note that while the
424 *attrib* value is always a real mutable Python dictionary, an ElementTree
425 implementation may choose to use another internal representation, and
426 create the dictionary only if someone asks for it. To take advantage of
427 such implementations, use the dictionary methods below whenever possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000428
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000429 The following dictionary-like methods work on the element attributes.
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000432 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +0000433
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000434 Resets an element. This function removes all subelements, clears all
435 attributes, and sets the text and tail attributes to None.
Georg Brandl116aa622007-08-15 14:28:22 +0000436
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000437
438 .. method:: get(key, default=None)
439
440 Gets the element attribute named *key*.
441
442 Returns the attribute value, or *default* if the attribute was not found.
443
444
445 .. method:: items()
446
447 Returns the element attributes as a sequence of (name, value) pairs. The
448 attributes are returned in an arbitrary order.
449
450
451 .. method:: keys()
452
453 Returns the elements attribute names as a list. The names are returned
454 in an arbitrary order.
455
456
457 .. method:: set(key, value)
458
459 Set the attribute *key* on the element to *value*.
460
461 The following methods work on the element's children (subelements).
462
463
464 .. method:: append(subelement)
465
Eli Bendersky396e8fc2012-03-23 14:24:20 +0200466 Adds the element *subelement* to the end of this element's internal list
467 of subelements. Raises :exc:`TypeError` if *subelement* is not an
468 :class:`Element`.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000469
470
471 .. method:: extend(subelements)
Georg Brandl116aa622007-08-15 14:28:22 +0000472
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000473 Appends *subelements* from a sequence object with zero or more elements.
Eli Bendersky396e8fc2012-03-23 14:24:20 +0200474 Raises :exc:`TypeError` if a subelement is not an :class:`Element`.
Georg Brandl116aa622007-08-15 14:28:22 +0000475
Ezio Melottif8754a62010-03-21 07:16:43 +0000476 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000477
Georg Brandl116aa622007-08-15 14:28:22 +0000478
Eli Bendersky737b1732012-05-29 06:02:56 +0300479 .. method:: find(match, namespaces=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000480
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000481 Finds the first subelement matching *match*. *match* may be a tag name
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200482 or a :ref:`path <elementtree-xpath>`. Returns an element instance
Eli Bendersky737b1732012-05-29 06:02:56 +0300483 or ``None``. *namespaces* is an optional mapping from namespace prefix
484 to full name.
Georg Brandl116aa622007-08-15 14:28:22 +0000485
Georg Brandl116aa622007-08-15 14:28:22 +0000486
Eli Bendersky737b1732012-05-29 06:02:56 +0300487 .. method:: findall(match, namespaces=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000488
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200489 Finds all matching subelements, by tag name or
490 :ref:`path <elementtree-xpath>`. Returns a list containing all matching
Eli Bendersky737b1732012-05-29 06:02:56 +0300491 elements in document order. *namespaces* is an optional mapping from
492 namespace prefix to full name.
Georg Brandl116aa622007-08-15 14:28:22 +0000493
Georg Brandl116aa622007-08-15 14:28:22 +0000494
Eli Bendersky737b1732012-05-29 06:02:56 +0300495 .. method:: findtext(match, default=None, namespaces=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000496
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000497 Finds text for the first subelement matching *match*. *match* may be
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200498 a tag name or a :ref:`path <elementtree-xpath>`. Returns the text content
499 of the first matching element, or *default* if no element was found.
500 Note that if the matching element has no text content an empty string
Eli Bendersky737b1732012-05-29 06:02:56 +0300501 is returned. *namespaces* is an optional mapping from namespace prefix
502 to full name.
Georg Brandl116aa622007-08-15 14:28:22 +0000503
Georg Brandl116aa622007-08-15 14:28:22 +0000504
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000505 .. method:: getchildren()
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Georg Brandl67b21b72010-08-17 15:07:14 +0000507 .. deprecated:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000508 Use ``list(elem)`` or iteration.
Georg Brandl116aa622007-08-15 14:28:22 +0000509
Georg Brandl116aa622007-08-15 14:28:22 +0000510
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000511 .. method:: getiterator(tag=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000512
Georg Brandl67b21b72010-08-17 15:07:14 +0000513 .. deprecated:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000514 Use method :meth:`Element.iter` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Georg Brandl116aa622007-08-15 14:28:22 +0000516
Eli Bendersky396e8fc2012-03-23 14:24:20 +0200517 .. method:: insert(index, subelement)
Georg Brandl116aa622007-08-15 14:28:22 +0000518
Eli Bendersky396e8fc2012-03-23 14:24:20 +0200519 Inserts *subelement* at the given position in this element. Raises
520 :exc:`TypeError` if *subelement* is not an :class:`Element`.
Georg Brandl116aa622007-08-15 14:28:22 +0000521
Georg Brandl116aa622007-08-15 14:28:22 +0000522
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000523 .. method:: iter(tag=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000524
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000525 Creates a tree :term:`iterator` with the current element as the root.
526 The iterator iterates over this element and all elements below it, in
527 document (depth first) order. If *tag* is not ``None`` or ``'*'``, only
528 elements whose tag equals *tag* are returned from the iterator. If the
529 tree structure is modified during iteration, the result is undefined.
Georg Brandl116aa622007-08-15 14:28:22 +0000530
Ezio Melotti138fc892011-10-10 00:02:03 +0300531 .. versionadded:: 3.2
532
Georg Brandl116aa622007-08-15 14:28:22 +0000533
Eli Bendersky737b1732012-05-29 06:02:56 +0300534 .. method:: iterfind(match, namespaces=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000535
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200536 Finds all matching subelements, by tag name or
537 :ref:`path <elementtree-xpath>`. Returns an iterable yielding all
Eli Bendersky737b1732012-05-29 06:02:56 +0300538 matching elements in document order. *namespaces* is an optional mapping
539 from namespace prefix to full name.
540
Georg Brandl116aa622007-08-15 14:28:22 +0000541
Ezio Melottif8754a62010-03-21 07:16:43 +0000542 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000543
Georg Brandl116aa622007-08-15 14:28:22 +0000544
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000545 .. method:: itertext()
Georg Brandl116aa622007-08-15 14:28:22 +0000546
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000547 Creates a text iterator. The iterator loops over this element and all
548 subelements, in document order, and returns all inner text.
Georg Brandl116aa622007-08-15 14:28:22 +0000549
Ezio Melottif8754a62010-03-21 07:16:43 +0000550 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000551
552
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000553 .. method:: makeelement(tag, attrib)
Georg Brandl116aa622007-08-15 14:28:22 +0000554
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000555 Creates a new element object of the same type as this element. Do not
556 call this method, use the :func:`SubElement` factory function instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000557
558
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000559 .. method:: remove(subelement)
Georg Brandl116aa622007-08-15 14:28:22 +0000560
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000561 Removes *subelement* from the element. Unlike the find\* methods this
562 method compares elements based on the instance identity, not on tag value
563 or contents.
Georg Brandl116aa622007-08-15 14:28:22 +0000564
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000565 :class:`Element` objects also support the following sequence type methods
566 for working with subelements: :meth:`__delitem__`, :meth:`__getitem__`,
567 :meth:`__setitem__`, :meth:`__len__`.
Georg Brandl116aa622007-08-15 14:28:22 +0000568
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000569 Caution: Elements with no subelements will test as ``False``. This behavior
570 will change in future versions. Use specific ``len(elem)`` or ``elem is
571 None`` test instead. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000572
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000573 element = root.find('foo')
Georg Brandl116aa622007-08-15 14:28:22 +0000574
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000575 if not element: # careful!
576 print("element not found, or element has no subelements")
Georg Brandl116aa622007-08-15 14:28:22 +0000577
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000578 if element is None:
579 print("element not found")
Georg Brandl116aa622007-08-15 14:28:22 +0000580
581
582.. _elementtree-elementtree-objects:
583
584ElementTree Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200585^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000586
587
Georg Brandl7f01a132009-09-16 15:58:14 +0000588.. class:: ElementTree(element=None, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000589
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000590 ElementTree wrapper class. This class represents an entire element
591 hierarchy, and adds some extra support for serialization to and from
592 standard XML.
Georg Brandl116aa622007-08-15 14:28:22 +0000593
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000594 *element* is the root element. The tree is initialized with the contents
595 of the XML *file* if given.
Georg Brandl116aa622007-08-15 14:28:22 +0000596
597
Benjamin Petersone41251e2008-04-25 01:59:09 +0000598 .. method:: _setroot(element)
Georg Brandl116aa622007-08-15 14:28:22 +0000599
Benjamin Petersone41251e2008-04-25 01:59:09 +0000600 Replaces the root element for this tree. This discards the current
601 contents of the tree, and replaces it with the given element. Use with
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000602 care. *element* is an element instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604
Eli Bendersky737b1732012-05-29 06:02:56 +0300605 .. method:: find(match, namespaces=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000606
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200607 Same as :meth:`Element.find`, starting at the root of the tree.
Georg Brandl116aa622007-08-15 14:28:22 +0000608
609
Eli Bendersky737b1732012-05-29 06:02:56 +0300610 .. method:: findall(match, namespaces=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000611
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200612 Same as :meth:`Element.findall`, starting at the root of the tree.
Georg Brandl116aa622007-08-15 14:28:22 +0000613
614
Eli Bendersky737b1732012-05-29 06:02:56 +0300615 .. method:: findtext(match, default=None, namespaces=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000616
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200617 Same as :meth:`Element.findtext`, starting at the root of the tree.
Georg Brandl116aa622007-08-15 14:28:22 +0000618
619
Georg Brandl7f01a132009-09-16 15:58:14 +0000620 .. method:: getiterator(tag=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000621
Georg Brandl67b21b72010-08-17 15:07:14 +0000622 .. deprecated:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000623 Use method :meth:`ElementTree.iter` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000624
625
Benjamin Petersone41251e2008-04-25 01:59:09 +0000626 .. method:: getroot()
Florent Xiclunac17f1722010-08-08 19:48:29 +0000627
Benjamin Petersone41251e2008-04-25 01:59:09 +0000628 Returns the root element for this tree.
Georg Brandl116aa622007-08-15 14:28:22 +0000629
630
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000631 .. method:: iter(tag=None)
632
633 Creates and returns a tree iterator for the root element. The iterator
634 loops over all elements in this tree, in section order. *tag* is the tag
635 to look for (default is to return all elements)
636
637
Eli Bendersky737b1732012-05-29 06:02:56 +0300638 .. method:: iterfind(match, namespaces=None)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000639
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200640 Same as :meth:`Element.iterfind`, starting at the root of the tree.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000641
Ezio Melottif8754a62010-03-21 07:16:43 +0000642 .. versionadded:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000643
644
Georg Brandl7f01a132009-09-16 15:58:14 +0000645 .. method:: parse(source, parser=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000646
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000647 Loads an external XML section into this element tree. *source* is a file
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000648 name or :term:`file object`. *parser* is an optional parser instance.
Eli Bendersky52467b12012-06-01 07:13:08 +0300649 If not given, the standard :class:`XMLParser` parser is used. Returns the
650 section root element.
Georg Brandl116aa622007-08-15 14:28:22 +0000651
652
Florent Xiclunac17f1722010-08-08 19:48:29 +0000653 .. method:: write(file, encoding="us-ascii", xml_declaration=None, method="xml")
Georg Brandl116aa622007-08-15 14:28:22 +0000654
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000655 Writes the element tree to a file, as XML. *file* is a file name, or a
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000656 :term:`file object` opened for writing. *encoding* [1]_ is the output encoding
Florent Xiclunac17f1722010-08-08 19:48:29 +0000657 (default is US-ASCII). Use ``encoding="unicode"`` to write a Unicode string.
658 *xml_declaration* controls if an XML declaration
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000659 should be added to the file. Use False for never, True for always, None
Florent Xiclunac17f1722010-08-08 19:48:29 +0000660 for only if not US-ASCII or UTF-8 or Unicode (default is None). *method* is
661 either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``).
662 Returns an (optionally) encoded string.
Georg Brandl116aa622007-08-15 14:28:22 +0000663
Christian Heimesd8654cf2007-12-02 15:22:16 +0000664This is the XML file that is going to be manipulated::
665
666 <html>
667 <head>
668 <title>Example page</title>
669 </head>
670 <body>
Georg Brandl48310cd2009-01-03 21:18:54 +0000671 <p>Moved to <a href="http://example.org/">example.org</a>
Christian Heimesd8654cf2007-12-02 15:22:16 +0000672 or <a href="http://example.com/">example.com</a>.</p>
673 </body>
674 </html>
675
676Example of changing the attribute "target" of every link in first paragraph::
677
678 >>> from xml.etree.ElementTree import ElementTree
679 >>> tree = ElementTree()
680 >>> tree.parse("index.xhtml")
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000681 <Element 'html' at 0xb77e6fac>
Christian Heimesd8654cf2007-12-02 15:22:16 +0000682 >>> p = tree.find("body/p") # Finds first occurrence of tag p in body
683 >>> p
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000684 <Element 'p' at 0xb77ec26c>
685 >>> links = list(p.iter("a")) # Returns list of all links
Christian Heimesd8654cf2007-12-02 15:22:16 +0000686 >>> links
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000687 [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
Christian Heimesd8654cf2007-12-02 15:22:16 +0000688 >>> for i in links: # Iterates through all found links
689 ... i.attrib["target"] = "blank"
690 >>> tree.write("output.xhtml")
Georg Brandl116aa622007-08-15 14:28:22 +0000691
692.. _elementtree-qname-objects:
693
694QName Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200695^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000696
697
Georg Brandl7f01a132009-09-16 15:58:14 +0000698.. class:: QName(text_or_uri, tag=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000699
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000700 QName wrapper. This can be used to wrap a QName attribute value, in order
701 to get proper namespace handling on output. *text_or_uri* is a string
702 containing the QName value, in the form {uri}local, or, if the tag argument
703 is given, the URI part of a QName. If *tag* is given, the first argument is
704 interpreted as an URI, and this argument is interpreted as a local name.
705 :class:`QName` instances are opaque.
Georg Brandl116aa622007-08-15 14:28:22 +0000706
707
708.. _elementtree-treebuilder-objects:
709
710TreeBuilder Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200711^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000712
713
Georg Brandl7f01a132009-09-16 15:58:14 +0000714.. class:: TreeBuilder(element_factory=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000715
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000716 Generic element structure builder. This builder converts a sequence of
717 start, data, and end method calls to a well-formed element structure. You
718 can use this class to build an element structure using a custom XML parser,
Eli Bendersky48d358b2012-05-30 17:57:50 +0300719 or a parser for some other XML-like format. *element_factory*, when given,
720 must be a callable accepting two positional arguments: a tag and
721 a dict of attributes. It is expected to return a new element instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000722
Benjamin Petersone41251e2008-04-25 01:59:09 +0000723 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000724
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000725 Flushes the builder buffers, and returns the toplevel document
726 element. Returns an :class:`Element` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000727
728
Benjamin Petersone41251e2008-04-25 01:59:09 +0000729 .. method:: data(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000730
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000731 Adds text to the current element. *data* is a string. This should be
732 either a bytestring, or a Unicode string.
Georg Brandl116aa622007-08-15 14:28:22 +0000733
734
Benjamin Petersone41251e2008-04-25 01:59:09 +0000735 .. method:: end(tag)
Georg Brandl116aa622007-08-15 14:28:22 +0000736
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000737 Closes the current element. *tag* is the element name. Returns the
738 closed element.
Georg Brandl116aa622007-08-15 14:28:22 +0000739
740
Benjamin Petersone41251e2008-04-25 01:59:09 +0000741 .. method:: start(tag, attrs)
Georg Brandl116aa622007-08-15 14:28:22 +0000742
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000743 Opens a new element. *tag* is the element name. *attrs* is a dictionary
744 containing element attributes. Returns the opened element.
Georg Brandl116aa622007-08-15 14:28:22 +0000745
746
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000747 In addition, a custom :class:`TreeBuilder` object can provide the
748 following method:
Georg Brandl116aa622007-08-15 14:28:22 +0000749
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000750 .. method:: doctype(name, pubid, system)
751
752 Handles a doctype declaration. *name* is the doctype name. *pubid* is
753 the public identifier. *system* is the system identifier. This method
754 does not exist on the default :class:`TreeBuilder` class.
755
Ezio Melottif8754a62010-03-21 07:16:43 +0000756 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000757
758
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000759.. _elementtree-xmlparser-objects:
Georg Brandl116aa622007-08-15 14:28:22 +0000760
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000761XMLParser Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200762^^^^^^^^^^^^^^^^^
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000763
764
765.. class:: XMLParser(html=0, target=None, encoding=None)
766
767 :class:`Element` structure builder for XML source data, based on the expat
768 parser. *html* are predefined HTML entities. This flag is not supported by
769 the current implementation. *target* is the target object. If omitted, the
Eli Bendersky52467b12012-06-01 07:13:08 +0300770 builder uses an instance of the standard :class:`TreeBuilder` class.
771 *encoding* [1]_ is optional. If given, the value overrides the encoding
772 specified in the XML file.
Georg Brandl116aa622007-08-15 14:28:22 +0000773
774
Benjamin Petersone41251e2008-04-25 01:59:09 +0000775 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000776
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000777 Finishes feeding data to the parser. Returns an element structure.
Georg Brandl116aa622007-08-15 14:28:22 +0000778
779
Benjamin Petersone41251e2008-04-25 01:59:09 +0000780 .. method:: doctype(name, pubid, system)
Georg Brandl116aa622007-08-15 14:28:22 +0000781
Georg Brandl67b21b72010-08-17 15:07:14 +0000782 .. deprecated:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000783 Define the :meth:`TreeBuilder.doctype` method on a custom TreeBuilder
784 target.
Georg Brandl116aa622007-08-15 14:28:22 +0000785
786
Benjamin Petersone41251e2008-04-25 01:59:09 +0000787 .. method:: feed(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000788
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000789 Feeds data to the parser. *data* is encoded data.
Georg Brandl116aa622007-08-15 14:28:22 +0000790
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000791:meth:`XMLParser.feed` calls *target*\'s :meth:`start` method
Christian Heimesd8654cf2007-12-02 15:22:16 +0000792for each opening tag, its :meth:`end` method for each closing tag,
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000793and data is processed by method :meth:`data`. :meth:`XMLParser.close`
Georg Brandl48310cd2009-01-03 21:18:54 +0000794calls *target*\'s method :meth:`close`.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000795:class:`XMLParser` can be used not only for building a tree structure.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000796This is an example of counting the maximum depth of an XML file::
797
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000798 >>> from xml.etree.ElementTree import XMLParser
Christian Heimesd8654cf2007-12-02 15:22:16 +0000799 >>> class MaxDepth: # The target object of the parser
800 ... maxDepth = 0
801 ... depth = 0
802 ... def start(self, tag, attrib): # Called for each opening tag.
Georg Brandl48310cd2009-01-03 21:18:54 +0000803 ... self.depth += 1
Christian Heimesd8654cf2007-12-02 15:22:16 +0000804 ... if self.depth > self.maxDepth:
805 ... self.maxDepth = self.depth
806 ... def end(self, tag): # Called for each closing tag.
807 ... self.depth -= 1
Georg Brandl48310cd2009-01-03 21:18:54 +0000808 ... def data(self, data):
Christian Heimesd8654cf2007-12-02 15:22:16 +0000809 ... pass # We do not need to do anything with data.
810 ... def close(self): # Called when all data has been parsed.
811 ... return self.maxDepth
Georg Brandl48310cd2009-01-03 21:18:54 +0000812 ...
Christian Heimesd8654cf2007-12-02 15:22:16 +0000813 >>> target = MaxDepth()
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000814 >>> parser = XMLParser(target=target)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000815 >>> exampleXml = """
816 ... <a>
817 ... <b>
818 ... </b>
819 ... <b>
820 ... <c>
821 ... <d>
822 ... </d>
823 ... </c>
824 ... </b>
825 ... </a>"""
826 >>> parser.feed(exampleXml)
827 >>> parser.close()
828 4
Christian Heimesb186d002008-03-18 15:15:01 +0000829
Eli Bendersky5b77d812012-03-16 08:20:05 +0200830Exceptions
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200831^^^^^^^^^^
Eli Bendersky5b77d812012-03-16 08:20:05 +0200832
833.. class:: ParseError
834
835 XML parse error, raised by the various parsing methods in this module when
836 parsing fails. The string representation of an instance of this exception
837 will contain a user-friendly error message. In addition, it will have
838 the following attributes available:
839
840 .. attribute:: code
841
842 A numeric error code from the expat parser. See the documentation of
843 :mod:`xml.parsers.expat` for the list of error codes and their meanings.
844
845 .. attribute:: position
846
847 A tuple of *line*, *column* numbers, specifying where the error occurred.
Christian Heimesb186d002008-03-18 15:15:01 +0000848
849.. rubric:: Footnotes
850
851.. [#] The encoding string included in XML output should conform to the
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000852 appropriate standards. For example, "UTF-8" is valid, but "UTF8" is
853 not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl
Benjamin Petersonad3d5c22009-02-26 03:38:59 +0000854 and http://www.iana.org/assignments/character-sets.