blob: d00781c190623c1b506189ab2f9531e999d16697 [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
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000479 .. method:: find(match)
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
483 or ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +0000484
Georg Brandl116aa622007-08-15 14:28:22 +0000485
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000486 .. method:: findall(match)
Georg Brandl116aa622007-08-15 14:28:22 +0000487
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200488 Finds all matching subelements, by tag name or
489 :ref:`path <elementtree-xpath>`. Returns a list containing all matching
490 elements in document order.
Georg Brandl116aa622007-08-15 14:28:22 +0000491
Georg Brandl116aa622007-08-15 14:28:22 +0000492
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000493 .. method:: findtext(match, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000494
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000495 Finds text for the first subelement matching *match*. *match* may be
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200496 a tag name or a :ref:`path <elementtree-xpath>`. Returns the text content
497 of the first matching element, or *default* if no element was found.
498 Note that if the matching element has no text content an empty string
499 is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000500
Georg Brandl116aa622007-08-15 14:28:22 +0000501
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000502 .. method:: getchildren()
Georg Brandl116aa622007-08-15 14:28:22 +0000503
Georg Brandl67b21b72010-08-17 15:07:14 +0000504 .. deprecated:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000505 Use ``list(elem)`` or iteration.
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Georg Brandl116aa622007-08-15 14:28:22 +0000507
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000508 .. method:: getiterator(tag=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000509
Georg Brandl67b21b72010-08-17 15:07:14 +0000510 .. deprecated:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000511 Use method :meth:`Element.iter` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000512
Georg Brandl116aa622007-08-15 14:28:22 +0000513
Eli Bendersky396e8fc2012-03-23 14:24:20 +0200514 .. method:: insert(index, subelement)
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Eli Bendersky396e8fc2012-03-23 14:24:20 +0200516 Inserts *subelement* at the given position in this element. Raises
517 :exc:`TypeError` if *subelement* is not an :class:`Element`.
Georg Brandl116aa622007-08-15 14:28:22 +0000518
Georg Brandl116aa622007-08-15 14:28:22 +0000519
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000520 .. method:: iter(tag=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000521
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000522 Creates a tree :term:`iterator` with the current element as the root.
523 The iterator iterates over this element and all elements below it, in
524 document (depth first) order. If *tag* is not ``None`` or ``'*'``, only
525 elements whose tag equals *tag* are returned from the iterator. If the
526 tree structure is modified during iteration, the result is undefined.
Georg Brandl116aa622007-08-15 14:28:22 +0000527
Ezio Melotti138fc892011-10-10 00:02:03 +0300528 .. versionadded:: 3.2
529
Georg Brandl116aa622007-08-15 14:28:22 +0000530
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000531 .. method:: iterfind(match)
Georg Brandl116aa622007-08-15 14:28:22 +0000532
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200533 Finds all matching subelements, by tag name or
534 :ref:`path <elementtree-xpath>`. Returns an iterable yielding all
535 matching elements in document order.
Georg Brandl116aa622007-08-15 14:28:22 +0000536
Ezio Melottif8754a62010-03-21 07:16:43 +0000537 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000538
Georg Brandl116aa622007-08-15 14:28:22 +0000539
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000540 .. method:: itertext()
Georg Brandl116aa622007-08-15 14:28:22 +0000541
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000542 Creates a text iterator. The iterator loops over this element and all
543 subelements, in document order, and returns all inner text.
Georg Brandl116aa622007-08-15 14:28:22 +0000544
Ezio Melottif8754a62010-03-21 07:16:43 +0000545 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000546
547
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000548 .. method:: makeelement(tag, attrib)
Georg Brandl116aa622007-08-15 14:28:22 +0000549
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000550 Creates a new element object of the same type as this element. Do not
551 call this method, use the :func:`SubElement` factory function instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000552
553
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000554 .. method:: remove(subelement)
Georg Brandl116aa622007-08-15 14:28:22 +0000555
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000556 Removes *subelement* from the element. Unlike the find\* methods this
557 method compares elements based on the instance identity, not on tag value
558 or contents.
Georg Brandl116aa622007-08-15 14:28:22 +0000559
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000560 :class:`Element` objects also support the following sequence type methods
561 for working with subelements: :meth:`__delitem__`, :meth:`__getitem__`,
562 :meth:`__setitem__`, :meth:`__len__`.
Georg Brandl116aa622007-08-15 14:28:22 +0000563
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000564 Caution: Elements with no subelements will test as ``False``. This behavior
565 will change in future versions. Use specific ``len(elem)`` or ``elem is
566 None`` test instead. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000567
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000568 element = root.find('foo')
Georg Brandl116aa622007-08-15 14:28:22 +0000569
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000570 if not element: # careful!
571 print("element not found, or element has no subelements")
Georg Brandl116aa622007-08-15 14:28:22 +0000572
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000573 if element is None:
574 print("element not found")
Georg Brandl116aa622007-08-15 14:28:22 +0000575
576
577.. _elementtree-elementtree-objects:
578
579ElementTree Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200580^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000581
582
Georg Brandl7f01a132009-09-16 15:58:14 +0000583.. class:: ElementTree(element=None, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000584
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000585 ElementTree wrapper class. This class represents an entire element
586 hierarchy, and adds some extra support for serialization to and from
587 standard XML.
Georg Brandl116aa622007-08-15 14:28:22 +0000588
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000589 *element* is the root element. The tree is initialized with the contents
590 of the XML *file* if given.
Georg Brandl116aa622007-08-15 14:28:22 +0000591
592
Benjamin Petersone41251e2008-04-25 01:59:09 +0000593 .. method:: _setroot(element)
Georg Brandl116aa622007-08-15 14:28:22 +0000594
Benjamin Petersone41251e2008-04-25 01:59:09 +0000595 Replaces the root element for this tree. This discards the current
596 contents of the tree, and replaces it with the given element. Use with
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000597 care. *element* is an element instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000598
599
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000600 .. method:: find(match)
Georg Brandl116aa622007-08-15 14:28:22 +0000601
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200602 Same as :meth:`Element.find`, starting at the root of the tree.
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000605 .. method:: findall(match)
Georg Brandl116aa622007-08-15 14:28:22 +0000606
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200607 Same as :meth:`Element.findall`, starting at the root of the tree.
Georg Brandl116aa622007-08-15 14:28:22 +0000608
609
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000610 .. method:: findtext(match, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000611
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200612 Same as :meth:`Element.findtext`, starting at the root of the tree.
Georg Brandl116aa622007-08-15 14:28:22 +0000613
614
Georg Brandl7f01a132009-09-16 15:58:14 +0000615 .. method:: getiterator(tag=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000616
Georg Brandl67b21b72010-08-17 15:07:14 +0000617 .. deprecated:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000618 Use method :meth:`ElementTree.iter` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000619
620
Benjamin Petersone41251e2008-04-25 01:59:09 +0000621 .. method:: getroot()
Florent Xiclunac17f1722010-08-08 19:48:29 +0000622
Benjamin Petersone41251e2008-04-25 01:59:09 +0000623 Returns the root element for this tree.
Georg Brandl116aa622007-08-15 14:28:22 +0000624
625
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000626 .. method:: iter(tag=None)
627
628 Creates and returns a tree iterator for the root element. The iterator
629 loops over all elements in this tree, in section order. *tag* is the tag
630 to look for (default is to return all elements)
631
632
633 .. method:: iterfind(match)
634
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200635 Same as :meth:`Element.iterfind`, starting at the root of the tree.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000636
Ezio Melottif8754a62010-03-21 07:16:43 +0000637 .. versionadded:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000638
639
Georg Brandl7f01a132009-09-16 15:58:14 +0000640 .. method:: parse(source, parser=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000641
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000642 Loads an external XML section into this element tree. *source* is a file
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000643 name or :term:`file object`. *parser* is an optional parser instance.
644 If not given, the standard XMLParser parser is used. Returns the section
Benjamin Petersone41251e2008-04-25 01:59:09 +0000645 root element.
Georg Brandl116aa622007-08-15 14:28:22 +0000646
647
Florent Xiclunac17f1722010-08-08 19:48:29 +0000648 .. method:: write(file, encoding="us-ascii", xml_declaration=None, method="xml")
Georg Brandl116aa622007-08-15 14:28:22 +0000649
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000650 Writes the element tree to a file, as XML. *file* is a file name, or a
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000651 :term:`file object` opened for writing. *encoding* [1]_ is the output encoding
Florent Xiclunac17f1722010-08-08 19:48:29 +0000652 (default is US-ASCII). Use ``encoding="unicode"`` to write a Unicode string.
653 *xml_declaration* controls if an XML declaration
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000654 should be added to the file. Use False for never, True for always, None
Florent Xiclunac17f1722010-08-08 19:48:29 +0000655 for only if not US-ASCII or UTF-8 or Unicode (default is None). *method* is
656 either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``).
657 Returns an (optionally) encoded string.
Georg Brandl116aa622007-08-15 14:28:22 +0000658
Christian Heimesd8654cf2007-12-02 15:22:16 +0000659This is the XML file that is going to be manipulated::
660
661 <html>
662 <head>
663 <title>Example page</title>
664 </head>
665 <body>
Georg Brandl48310cd2009-01-03 21:18:54 +0000666 <p>Moved to <a href="http://example.org/">example.org</a>
Christian Heimesd8654cf2007-12-02 15:22:16 +0000667 or <a href="http://example.com/">example.com</a>.</p>
668 </body>
669 </html>
670
671Example of changing the attribute "target" of every link in first paragraph::
672
673 >>> from xml.etree.ElementTree import ElementTree
674 >>> tree = ElementTree()
675 >>> tree.parse("index.xhtml")
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000676 <Element 'html' at 0xb77e6fac>
Christian Heimesd8654cf2007-12-02 15:22:16 +0000677 >>> p = tree.find("body/p") # Finds first occurrence of tag p in body
678 >>> p
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000679 <Element 'p' at 0xb77ec26c>
680 >>> links = list(p.iter("a")) # Returns list of all links
Christian Heimesd8654cf2007-12-02 15:22:16 +0000681 >>> links
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000682 [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
Christian Heimesd8654cf2007-12-02 15:22:16 +0000683 >>> for i in links: # Iterates through all found links
684 ... i.attrib["target"] = "blank"
685 >>> tree.write("output.xhtml")
Georg Brandl116aa622007-08-15 14:28:22 +0000686
687.. _elementtree-qname-objects:
688
689QName Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200690^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000691
692
Georg Brandl7f01a132009-09-16 15:58:14 +0000693.. class:: QName(text_or_uri, tag=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000694
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000695 QName wrapper. This can be used to wrap a QName attribute value, in order
696 to get proper namespace handling on output. *text_or_uri* is a string
697 containing the QName value, in the form {uri}local, or, if the tag argument
698 is given, the URI part of a QName. If *tag* is given, the first argument is
699 interpreted as an URI, and this argument is interpreted as a local name.
700 :class:`QName` instances are opaque.
Georg Brandl116aa622007-08-15 14:28:22 +0000701
702
703.. _elementtree-treebuilder-objects:
704
705TreeBuilder Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200706^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000707
708
Georg Brandl7f01a132009-09-16 15:58:14 +0000709.. class:: TreeBuilder(element_factory=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000710
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000711 Generic element structure builder. This builder converts a sequence of
712 start, data, and end method calls to a well-formed element structure. You
713 can use this class to build an element structure using a custom XML parser,
714 or a parser for some other XML-like format. The *element_factory* is called
715 to create new :class:`Element` instances when given.
Georg Brandl116aa622007-08-15 14:28:22 +0000716
717
Benjamin Petersone41251e2008-04-25 01:59:09 +0000718 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000719
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000720 Flushes the builder buffers, and returns the toplevel document
721 element. Returns an :class:`Element` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000722
723
Benjamin Petersone41251e2008-04-25 01:59:09 +0000724 .. method:: data(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000725
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000726 Adds text to the current element. *data* is a string. This should be
727 either a bytestring, or a Unicode string.
Georg Brandl116aa622007-08-15 14:28:22 +0000728
729
Benjamin Petersone41251e2008-04-25 01:59:09 +0000730 .. method:: end(tag)
Georg Brandl116aa622007-08-15 14:28:22 +0000731
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000732 Closes the current element. *tag* is the element name. Returns the
733 closed element.
Georg Brandl116aa622007-08-15 14:28:22 +0000734
735
Benjamin Petersone41251e2008-04-25 01:59:09 +0000736 .. method:: start(tag, attrs)
Georg Brandl116aa622007-08-15 14:28:22 +0000737
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000738 Opens a new element. *tag* is the element name. *attrs* is a dictionary
739 containing element attributes. Returns the opened element.
Georg Brandl116aa622007-08-15 14:28:22 +0000740
741
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000742 In addition, a custom :class:`TreeBuilder` object can provide the
743 following method:
Georg Brandl116aa622007-08-15 14:28:22 +0000744
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000745 .. method:: doctype(name, pubid, system)
746
747 Handles a doctype declaration. *name* is the doctype name. *pubid* is
748 the public identifier. *system* is the system identifier. This method
749 does not exist on the default :class:`TreeBuilder` class.
750
Ezio Melottif8754a62010-03-21 07:16:43 +0000751 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000752
753
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000754.. _elementtree-xmlparser-objects:
Georg Brandl116aa622007-08-15 14:28:22 +0000755
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000756XMLParser Objects
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200757^^^^^^^^^^^^^^^^^
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000758
759
760.. class:: XMLParser(html=0, target=None, encoding=None)
761
762 :class:`Element` structure builder for XML source data, based on the expat
763 parser. *html* are predefined HTML entities. This flag is not supported by
764 the current implementation. *target* is the target object. If omitted, the
765 builder uses an instance of the standard TreeBuilder class. *encoding* [1]_
766 is optional. If given, the value overrides the encoding specified in the
767 XML file.
Georg Brandl116aa622007-08-15 14:28:22 +0000768
769
Benjamin Petersone41251e2008-04-25 01:59:09 +0000770 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000771
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000772 Finishes feeding data to the parser. Returns an element structure.
Georg Brandl116aa622007-08-15 14:28:22 +0000773
774
Benjamin Petersone41251e2008-04-25 01:59:09 +0000775 .. method:: doctype(name, pubid, system)
Georg Brandl116aa622007-08-15 14:28:22 +0000776
Georg Brandl67b21b72010-08-17 15:07:14 +0000777 .. deprecated:: 3.2
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000778 Define the :meth:`TreeBuilder.doctype` method on a custom TreeBuilder
779 target.
Georg Brandl116aa622007-08-15 14:28:22 +0000780
781
Benjamin Petersone41251e2008-04-25 01:59:09 +0000782 .. method:: feed(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000783
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000784 Feeds data to the parser. *data* is encoded data.
Georg Brandl116aa622007-08-15 14:28:22 +0000785
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000786:meth:`XMLParser.feed` calls *target*\'s :meth:`start` method
Christian Heimesd8654cf2007-12-02 15:22:16 +0000787for each opening tag, its :meth:`end` method for each closing tag,
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000788and data is processed by method :meth:`data`. :meth:`XMLParser.close`
Georg Brandl48310cd2009-01-03 21:18:54 +0000789calls *target*\'s method :meth:`close`.
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000790:class:`XMLParser` can be used not only for building a tree structure.
Christian Heimesd8654cf2007-12-02 15:22:16 +0000791This is an example of counting the maximum depth of an XML file::
792
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000793 >>> from xml.etree.ElementTree import XMLParser
Christian Heimesd8654cf2007-12-02 15:22:16 +0000794 >>> class MaxDepth: # The target object of the parser
795 ... maxDepth = 0
796 ... depth = 0
797 ... def start(self, tag, attrib): # Called for each opening tag.
Georg Brandl48310cd2009-01-03 21:18:54 +0000798 ... self.depth += 1
Christian Heimesd8654cf2007-12-02 15:22:16 +0000799 ... if self.depth > self.maxDepth:
800 ... self.maxDepth = self.depth
801 ... def end(self, tag): # Called for each closing tag.
802 ... self.depth -= 1
Georg Brandl48310cd2009-01-03 21:18:54 +0000803 ... def data(self, data):
Christian Heimesd8654cf2007-12-02 15:22:16 +0000804 ... pass # We do not need to do anything with data.
805 ... def close(self): # Called when all data has been parsed.
806 ... return self.maxDepth
Georg Brandl48310cd2009-01-03 21:18:54 +0000807 ...
Christian Heimesd8654cf2007-12-02 15:22:16 +0000808 >>> target = MaxDepth()
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000809 >>> parser = XMLParser(target=target)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000810 >>> exampleXml = """
811 ... <a>
812 ... <b>
813 ... </b>
814 ... <b>
815 ... <c>
816 ... <d>
817 ... </d>
818 ... </c>
819 ... </b>
820 ... </a>"""
821 >>> parser.feed(exampleXml)
822 >>> parser.close()
823 4
Christian Heimesb186d002008-03-18 15:15:01 +0000824
Eli Bendersky5b77d812012-03-16 08:20:05 +0200825Exceptions
Eli Bendersky3a4875e2012-03-26 20:43:32 +0200826^^^^^^^^^^
Eli Bendersky5b77d812012-03-16 08:20:05 +0200827
828.. class:: ParseError
829
830 XML parse error, raised by the various parsing methods in this module when
831 parsing fails. The string representation of an instance of this exception
832 will contain a user-friendly error message. In addition, it will have
833 the following attributes available:
834
835 .. attribute:: code
836
837 A numeric error code from the expat parser. See the documentation of
838 :mod:`xml.parsers.expat` for the list of error codes and their meanings.
839
840 .. attribute:: position
841
842 A tuple of *line*, *column* numbers, specifying where the error occurred.
Christian Heimesb186d002008-03-18 15:15:01 +0000843
844.. rubric:: Footnotes
845
846.. [#] The encoding string included in XML output should conform to the
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000847 appropriate standards. For example, "UTF-8" is valid, but "UTF8" is
848 not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl
Benjamin Petersonad3d5c22009-02-26 03:38:59 +0000849 and http://www.iana.org/assignments/character-sets.