Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 | |
Raymond Hettinger | 3029aff | 2011-02-10 08:09:36 +0000 | [diff] [blame] | 8 | **Source code:** :source:`Lib/xml/etree/ElementTree.py` |
| 9 | |
| 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 12 | The :class:`Element` type is a flexible container object, designed to store |
| 13 | hierarchical data structures in memory. The type can be described as a cross |
| 14 | between a list and a dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | |
| 16 | Each element has a number of properties associated with it: |
| 17 | |
| 18 | * a tag which is a string identifying what kind of data this element represents |
| 19 | (the element type, in other words). |
| 20 | |
| 21 | * a number of attributes, stored in a Python dictionary. |
| 22 | |
| 23 | * a text string. |
| 24 | |
| 25 | * an optional tail string. |
| 26 | |
| 27 | * a number of child elements, stored in a Python sequence |
| 28 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 29 | To create an element instance, use the :class:`Element` constructor or the |
| 30 | :func:`SubElement` factory function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
| 32 | The :class:`ElementTree` class can be used to wrap an element structure, and |
| 33 | convert it from and to XML. |
| 34 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 35 | See http://effbot.org/zone/element-index.htm for tutorials and links to other |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 36 | docs. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 37 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 38 | .. versionchanged:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 39 | The ElementTree API is updated to 1.3. For more information, see |
| 40 | `Introducing ElementTree 1.3 |
| 41 | <http://effbot.org/zone/elementtree-13-intro.htm>`_. |
| 42 | |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 43 | .. versionchanged:: 3.3 |
| 44 | This module will use a fast implementation whenever available. |
| 45 | The :mod:`xml.etree.cElementTree` module is deprecated. |
| 46 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 48 | .. _elementtree-xpath: |
| 49 | |
| 50 | XPath support |
| 51 | ------------- |
| 52 | |
| 53 | This module provides limited support for |
| 54 | `XPath expressions <http://www.w3.org/TR/xpath>`_ for locating elements in a |
| 55 | tree. The goal is to support a small subset of the abbreviated syntax; a full |
| 56 | XPath engine is outside the scope of the module. |
| 57 | |
| 58 | Example |
| 59 | ^^^^^^^ |
| 60 | |
| 61 | Here's an example that demonstrates some of the XPath capabilities of the |
| 62 | module:: |
| 63 | |
| 64 | import xml.etree.ElementTree as ET |
| 65 | |
| 66 | xml = r'''<?xml version="1.0"?> |
| 67 | <data> |
| 68 | <country name="Liechtenshtein"> |
| 69 | <rank>1</rank> |
| 70 | <year>2008</year> |
| 71 | <gdppc>141100</gdppc> |
| 72 | <neighbor name="Austria" direction="E"/> |
| 73 | <neighbor name="Switzerland" direction="W"/> |
| 74 | </country> |
| 75 | <country name="Singapore"> |
| 76 | <rank>4</rank> |
| 77 | <year>2011</year> |
| 78 | <gdppc>59900</gdppc> |
| 79 | <neighbor name="Malaysia" direction="N"/> |
| 80 | </country> |
| 81 | <country name="Panama"> |
| 82 | <rank>68</rank> |
| 83 | <year>2011</year> |
| 84 | <gdppc>13600</gdppc> |
| 85 | <neighbor name="Costa Rica" direction="W"/> |
| 86 | <neighbor name="Colombia" direction="E"/> |
| 87 | </country> |
| 88 | </data> |
| 89 | ''' |
| 90 | |
| 91 | tree = ET.fromstring(xml) |
| 92 | |
| 93 | # Top-level elements |
| 94 | tree.findall(".") |
| 95 | |
| 96 | # All 'neighbor' grand-children of 'country' children of the top-level |
| 97 | # elements |
| 98 | tree.findall("./country/neighbor") |
| 99 | |
| 100 | # Nodes with name='Singapore' that have a 'year' child |
| 101 | tree.findall(".//year/..[@name='Singapore']") |
| 102 | |
| 103 | # 'year' nodes that are children of nodes with name='Singapore' |
| 104 | tree.findall(".//*[@name='Singapore']/year") |
| 105 | |
| 106 | # All 'neighbor' nodes that are the second child of their parent |
| 107 | tree.findall(".//neighbor[2]") |
| 108 | |
| 109 | Supported XPath syntax |
| 110 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 111 | |
| 112 | +-----------------------+------------------------------------------------------+ |
| 113 | | Syntax | Meaning | |
| 114 | +=======================+======================================================+ |
| 115 | | ``tag`` | Selects all child elements with the given tag. | |
| 116 | | | For example, ``spam`` selects all child elements | |
| 117 | | | named ``spam``, ``spam/egg`` selects all | |
| 118 | | | grandchildren named ``egg`` in all children named | |
| 119 | | | ``spam``. | |
| 120 | +-----------------------+------------------------------------------------------+ |
| 121 | | ``*`` | Selects all child elements. For example, ``*/egg`` | |
| 122 | | | selects all grandchildren named ``egg``. | |
| 123 | +-----------------------+------------------------------------------------------+ |
| 124 | | ``.`` | Selects the current node. This is mostly useful | |
| 125 | | | at the beginning of the path, to indicate that it's | |
| 126 | | | a relative path. | |
| 127 | +-----------------------+------------------------------------------------------+ |
| 128 | | ``//`` | Selects all subelements, on all levels beneath the | |
Eli Bendersky | ede001a | 2012-03-27 04:57:23 +0200 | [diff] [blame^] | 129 | | | current element. For example, ``.//egg`` selects | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 130 | | | all ``egg`` elements in the entire tree. | |
| 131 | +-----------------------+------------------------------------------------------+ |
| 132 | | ``..`` | Selects the parent element. | |
| 133 | +-----------------------+------------------------------------------------------+ |
| 134 | | ``[@attrib]`` | Selects all elements that have the given attribute. | |
| 135 | +-----------------------+------------------------------------------------------+ |
| 136 | | ``[@attrib='value']`` | Selects all elements for which the given attribute | |
| 137 | | | has the given value. The value cannot contain | |
| 138 | | | quotes. | |
| 139 | +-----------------------+------------------------------------------------------+ |
| 140 | | ``[tag]`` | Selects all elements that have a child named | |
| 141 | | | ``tag``. Only immediate children are supported. | |
| 142 | +-----------------------+------------------------------------------------------+ |
| 143 | | ``[position]`` | Selects all elements that are located at the given | |
| 144 | | | position. The position can be either an integer | |
| 145 | | | (1 is the first position), the expression ``last()`` | |
| 146 | | | (for the last position), or a position relative to | |
| 147 | | | the last position (e.g. ``last()-1``). | |
| 148 | +-----------------------+------------------------------------------------------+ |
| 149 | |
| 150 | Predicates (expressions within square brackets) must be preceded by a tag |
| 151 | name, an asterisk, or another predicate. ``position`` predicates must be |
| 152 | preceded by a tag name. |
| 153 | |
| 154 | Reference |
| 155 | --------- |
| 156 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | .. _elementtree-functions: |
| 158 | |
| 159 | Functions |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 160 | ^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
| 162 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 163 | .. function:: Comment(text=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 165 | Comment element factory. This factory function creates a special element |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 166 | that will be serialized as an XML comment by the standard serializer. The |
| 167 | comment string can be either a bytestring or a Unicode string. *text* is a |
| 168 | string containing the comment string. Returns an element instance |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 169 | representing a comment. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | .. function:: dump(elem) |
| 173 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 174 | Writes an element tree or element structure to sys.stdout. This function |
| 175 | should be used for debugging only. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | |
| 177 | The exact output format is implementation dependent. In this version, it's |
| 178 | written as an ordinary XML file. |
| 179 | |
| 180 | *elem* is an element tree or an individual element. |
| 181 | |
| 182 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | .. function:: fromstring(text) |
| 184 | |
Florent Xicluna | dddd5e9 | 2010-03-14 01:28:07 +0000 | [diff] [blame] | 185 | Parses an XML section from a string constant. Same as :func:`XML`. *text* |
| 186 | is a string containing XML data. Returns an :class:`Element` instance. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | .. function:: fromstringlist(sequence, parser=None) |
| 190 | |
| 191 | Parses an XML document from a sequence of string fragments. *sequence* is a |
| 192 | list or other sequence containing XML data fragments. *parser* is an |
| 193 | optional parser instance. If not given, the standard :class:`XMLParser` |
| 194 | parser is used. Returns an :class:`Element` instance. |
| 195 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 196 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 197 | |
| 198 | |
| 199 | .. function:: iselement(element) |
| 200 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 201 | Checks if an object appears to be a valid element object. *element* is an |
| 202 | element instance. Returns a true value if this is an element object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | |
| 204 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 205 | .. function:: iterparse(source, events=None, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 | |
| 207 | Parses an XML section into an element tree incrementally, and reports what's |
Eli Bendersky | 604c4ff | 2012-03-16 08:41:30 +0200 | [diff] [blame] | 208 | going on to the user. *source* is a filename or :term:`file object` |
| 209 | containing XML data. *events* is a list of events to report back. The |
| 210 | supported events are the strings ``"start"``, ``"end"``, ``"start-ns"`` |
| 211 | and ``"end-ns"`` (the "ns" events are used to get detailed namespace |
| 212 | information). If *events* is omitted, only ``"end"`` events are reported. |
| 213 | *parser* is an optional parser instance. If not given, the standard |
| 214 | :class:`XMLParser` parser is used. Returns an :term:`iterator` providing |
| 215 | ``(event, elem)`` pairs. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 216 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 217 | .. note:: |
| 218 | |
| 219 | :func:`iterparse` only guarantees that it has seen the ">" |
| 220 | character of a starting tag when it emits a "start" event, so the |
| 221 | attributes are defined, but the contents of the text and tail attributes |
| 222 | are undefined at that point. The same applies to the element children; |
| 223 | they may or may not be present. |
| 224 | |
| 225 | If you need a fully populated element, look for "end" events instead. |
| 226 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 227 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 228 | .. function:: parse(source, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 229 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 230 | Parses an XML section into an element tree. *source* is a filename or file |
| 231 | object containing XML data. *parser* is an optional parser instance. If |
| 232 | not given, the standard :class:`XMLParser` parser is used. Returns an |
| 233 | :class:`ElementTree` instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | |
| 235 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 236 | .. function:: ProcessingInstruction(target, text=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 238 | PI element factory. This factory function creates a special element that |
| 239 | will be serialized as an XML processing instruction. *target* is a string |
| 240 | containing the PI target. *text* is a string containing the PI contents, if |
| 241 | given. Returns an element instance, representing a processing instruction. |
| 242 | |
| 243 | |
| 244 | .. function:: register_namespace(prefix, uri) |
| 245 | |
| 246 | Registers a namespace prefix. The registry is global, and any existing |
| 247 | mapping for either the given prefix or the namespace URI will be removed. |
| 248 | *prefix* is a namespace prefix. *uri* is a namespace uri. Tags and |
| 249 | attributes in this namespace will be serialized with the given prefix, if at |
| 250 | all possible. |
| 251 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 252 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 253 | |
| 254 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 255 | .. function:: SubElement(parent, tag, attrib={}, **extra) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 256 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 257 | Subelement factory. This function creates an element instance, and appends |
| 258 | it to an existing element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 260 | The element name, attribute names, and attribute values can be either |
| 261 | bytestrings or Unicode strings. *parent* is the parent element. *tag* is |
| 262 | the subelement name. *attrib* is an optional dictionary, containing element |
| 263 | attributes. *extra* contains additional attributes, given as keyword |
| 264 | arguments. Returns an element instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 | |
| 266 | |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 267 | .. function:: tostring(element, encoding="us-ascii", method="xml") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 269 | Generates a string representation of an XML element, including all |
Florent Xicluna | dddd5e9 | 2010-03-14 01:28:07 +0000 | [diff] [blame] | 270 | subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 271 | the output encoding (default is US-ASCII). Use ``encoding="unicode"`` to |
| 272 | generate a Unicode string. *method* is either ``"xml"``, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 273 | ``"html"`` or ``"text"`` (default is ``"xml"``). Returns an (optionally) |
| 274 | encoded string containing the XML data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 275 | |
| 276 | |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 277 | .. function:: tostringlist(element, encoding="us-ascii", method="xml") |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 278 | |
| 279 | Generates a string representation of an XML element, including all |
Florent Xicluna | dddd5e9 | 2010-03-14 01:28:07 +0000 | [diff] [blame] | 280 | subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 281 | the output encoding (default is US-ASCII). Use ``encoding="unicode"`` to |
| 282 | generate a Unicode string. *method* is either ``"xml"``, |
Florent Xicluna | dddd5e9 | 2010-03-14 01:28:07 +0000 | [diff] [blame] | 283 | ``"html"`` or ``"text"`` (default is ``"xml"``). Returns a list of |
| 284 | (optionally) encoded strings containing the XML data. It does not guarantee |
| 285 | any specific sequence, except that ``"".join(tostringlist(element)) == |
| 286 | tostring(element)``. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 287 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 288 | .. versionadded:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | .. function:: XML(text, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | |
| 293 | Parses an XML section from a string constant. This function can be used to |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 294 | embed "XML literals" in Python code. *text* is a string containing XML |
| 295 | data. *parser* is an optional parser instance. If not given, the standard |
| 296 | :class:`XMLParser` parser is used. Returns an :class:`Element` instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 297 | |
| 298 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 299 | .. function:: XMLID(text, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | |
| 301 | Parses an XML section from a string constant, and also returns a dictionary |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 302 | which maps from element id:s to elements. *text* is a string containing XML |
| 303 | data. *parser* is an optional parser instance. If not given, the standard |
| 304 | :class:`XMLParser` parser is used. Returns a tuple containing an |
| 305 | :class:`Element` instance and a dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 306 | |
| 307 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 308 | .. _elementtree-element-objects: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 309 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 310 | Element Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 311 | ^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 313 | .. class:: Element(tag, attrib={}, **extra) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 314 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 315 | Element class. This class defines the Element interface, and provides a |
| 316 | reference implementation of this interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 318 | The element name, attribute names, and attribute values can be either |
| 319 | bytestrings or Unicode strings. *tag* is the element name. *attrib* is |
| 320 | an optional dictionary, containing element attributes. *extra* contains |
| 321 | additional attributes, given as keyword arguments. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 322 | |
| 323 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 324 | .. attribute:: tag |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 326 | A string identifying what kind of data this element represents (the |
| 327 | element type, in other words). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 328 | |
| 329 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 330 | .. attribute:: text |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 331 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 332 | The *text* attribute can be used to hold additional data associated with |
| 333 | the element. As the name implies this attribute is usually a string but |
| 334 | may be any application-specific object. If the element is created from |
| 335 | an XML file the attribute will contain any text found between the element |
| 336 | tags. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | |
| 338 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 339 | .. attribute:: tail |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 340 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 341 | The *tail* attribute can be used to hold additional data associated with |
| 342 | the element. This attribute is usually a string but may be any |
| 343 | application-specific object. If the element is created from an XML file |
| 344 | the attribute will contain any text found after the element's end tag and |
| 345 | before the next tag. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 347 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 348 | .. attribute:: attrib |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 349 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 350 | A dictionary containing the element's attributes. Note that while the |
| 351 | *attrib* value is always a real mutable Python dictionary, an ElementTree |
| 352 | implementation may choose to use another internal representation, and |
| 353 | create the dictionary only if someone asks for it. To take advantage of |
| 354 | such implementations, use the dictionary methods below whenever possible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 355 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 356 | The following dictionary-like methods work on the element attributes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 357 | |
| 358 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 359 | .. method:: clear() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 360 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 361 | Resets an element. This function removes all subelements, clears all |
| 362 | attributes, and sets the text and tail attributes to None. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 363 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 364 | |
| 365 | .. method:: get(key, default=None) |
| 366 | |
| 367 | Gets the element attribute named *key*. |
| 368 | |
| 369 | Returns the attribute value, or *default* if the attribute was not found. |
| 370 | |
| 371 | |
| 372 | .. method:: items() |
| 373 | |
| 374 | Returns the element attributes as a sequence of (name, value) pairs. The |
| 375 | attributes are returned in an arbitrary order. |
| 376 | |
| 377 | |
| 378 | .. method:: keys() |
| 379 | |
| 380 | Returns the elements attribute names as a list. The names are returned |
| 381 | in an arbitrary order. |
| 382 | |
| 383 | |
| 384 | .. method:: set(key, value) |
| 385 | |
| 386 | Set the attribute *key* on the element to *value*. |
| 387 | |
| 388 | The following methods work on the element's children (subelements). |
| 389 | |
| 390 | |
| 391 | .. method:: append(subelement) |
| 392 | |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 393 | Adds the element *subelement* to the end of this element's internal list |
| 394 | of subelements. Raises :exc:`TypeError` if *subelement* is not an |
| 395 | :class:`Element`. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 396 | |
| 397 | |
| 398 | .. method:: extend(subelements) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 399 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 400 | Appends *subelements* from a sequence object with zero or more elements. |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 401 | Raises :exc:`TypeError` if a subelement is not an :class:`Element`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 402 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 403 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 404 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 405 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 406 | .. method:: find(match) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 407 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 408 | Finds the first subelement matching *match*. *match* may be a tag name |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 409 | or a :ref:`path <elementtree-xpath>`. Returns an element instance |
| 410 | or ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 412 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 413 | .. method:: findall(match) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 414 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 415 | Finds all matching subelements, by tag name or |
| 416 | :ref:`path <elementtree-xpath>`. Returns a list containing all matching |
| 417 | elements in document order. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 418 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 420 | .. method:: findtext(match, default=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 422 | Finds text for the first subelement matching *match*. *match* may be |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 423 | a tag name or a :ref:`path <elementtree-xpath>`. Returns the text content |
| 424 | of the first matching element, or *default* if no element was found. |
| 425 | Note that if the matching element has no text content an empty string |
| 426 | is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 428 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 429 | .. method:: getchildren() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 430 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 431 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 432 | Use ``list(elem)`` or iteration. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 434 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 435 | .. method:: getiterator(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 436 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 437 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 438 | Use method :meth:`Element.iter` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 439 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 | |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 441 | .. method:: insert(index, subelement) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 442 | |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 443 | Inserts *subelement* at the given position in this element. Raises |
| 444 | :exc:`TypeError` if *subelement* is not an :class:`Element`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 445 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 446 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 447 | .. method:: iter(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 448 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 449 | Creates a tree :term:`iterator` with the current element as the root. |
| 450 | The iterator iterates over this element and all elements below it, in |
| 451 | document (depth first) order. If *tag* is not ``None`` or ``'*'``, only |
| 452 | elements whose tag equals *tag* are returned from the iterator. If the |
| 453 | tree structure is modified during iteration, the result is undefined. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 454 | |
Ezio Melotti | 138fc89 | 2011-10-10 00:02:03 +0300 | [diff] [blame] | 455 | .. versionadded:: 3.2 |
| 456 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 457 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 458 | .. method:: iterfind(match) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 459 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 460 | Finds all matching subelements, by tag name or |
| 461 | :ref:`path <elementtree-xpath>`. Returns an iterable yielding all |
| 462 | matching elements in document order. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 463 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 464 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 465 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 466 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 467 | .. method:: itertext() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 468 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 469 | Creates a text iterator. The iterator loops over this element and all |
| 470 | subelements, in document order, and returns all inner text. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 471 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 472 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 473 | |
| 474 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 475 | .. method:: makeelement(tag, attrib) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 476 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 477 | Creates a new element object of the same type as this element. Do not |
| 478 | call this method, use the :func:`SubElement` factory function instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 479 | |
| 480 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 481 | .. method:: remove(subelement) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 482 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 483 | Removes *subelement* from the element. Unlike the find\* methods this |
| 484 | method compares elements based on the instance identity, not on tag value |
| 485 | or contents. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 486 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 487 | :class:`Element` objects also support the following sequence type methods |
| 488 | for working with subelements: :meth:`__delitem__`, :meth:`__getitem__`, |
| 489 | :meth:`__setitem__`, :meth:`__len__`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 491 | Caution: Elements with no subelements will test as ``False``. This behavior |
| 492 | will change in future versions. Use specific ``len(elem)`` or ``elem is |
| 493 | None`` test instead. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 494 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 495 | element = root.find('foo') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 496 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 497 | if not element: # careful! |
| 498 | print("element not found, or element has no subelements") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 499 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 500 | if element is None: |
| 501 | print("element not found") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 502 | |
| 503 | |
| 504 | .. _elementtree-elementtree-objects: |
| 505 | |
| 506 | ElementTree Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 507 | ^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 508 | |
| 509 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 510 | .. class:: ElementTree(element=None, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 511 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 512 | ElementTree wrapper class. This class represents an entire element |
| 513 | hierarchy, and adds some extra support for serialization to and from |
| 514 | standard XML. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 516 | *element* is the root element. The tree is initialized with the contents |
| 517 | of the XML *file* if given. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 518 | |
| 519 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 520 | .. method:: _setroot(element) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 521 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 522 | Replaces the root element for this tree. This discards the current |
| 523 | contents of the tree, and replaces it with the given element. Use with |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 524 | care. *element* is an element instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 525 | |
| 526 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 527 | .. method:: find(match) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 528 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 529 | Same as :meth:`Element.find`, starting at the root of the tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 530 | |
| 531 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 532 | .. method:: findall(match) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 533 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 534 | Same as :meth:`Element.findall`, starting at the root of the tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | |
| 536 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 537 | .. method:: findtext(match, default=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 538 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 539 | Same as :meth:`Element.findtext`, starting at the root of the tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 540 | |
| 541 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 542 | .. method:: getiterator(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 543 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 544 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 545 | Use method :meth:`ElementTree.iter` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 546 | |
| 547 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 548 | .. method:: getroot() |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 549 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 550 | Returns the root element for this tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 551 | |
| 552 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 553 | .. method:: iter(tag=None) |
| 554 | |
| 555 | Creates and returns a tree iterator for the root element. The iterator |
| 556 | loops over all elements in this tree, in section order. *tag* is the tag |
| 557 | to look for (default is to return all elements) |
| 558 | |
| 559 | |
| 560 | .. method:: iterfind(match) |
| 561 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 562 | Same as :meth:`Element.iterfind`, starting at the root of the tree. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 563 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 564 | .. versionadded:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 565 | |
| 566 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 567 | .. method:: parse(source, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 568 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 569 | Loads an external XML section into this element tree. *source* is a file |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 570 | name or :term:`file object`. *parser* is an optional parser instance. |
| 571 | If not given, the standard XMLParser parser is used. Returns the section |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 572 | root element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 573 | |
| 574 | |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 575 | .. method:: write(file, encoding="us-ascii", xml_declaration=None, method="xml") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 576 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 577 | Writes the element tree to a file, as XML. *file* is a file name, or a |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 578 | :term:`file object` opened for writing. *encoding* [1]_ is the output encoding |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 579 | (default is US-ASCII). Use ``encoding="unicode"`` to write a Unicode string. |
| 580 | *xml_declaration* controls if an XML declaration |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 581 | should be added to the file. Use False for never, True for always, None |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 582 | for only if not US-ASCII or UTF-8 or Unicode (default is None). *method* is |
| 583 | either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``). |
| 584 | Returns an (optionally) encoded string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 585 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 586 | This is the XML file that is going to be manipulated:: |
| 587 | |
| 588 | <html> |
| 589 | <head> |
| 590 | <title>Example page</title> |
| 591 | </head> |
| 592 | <body> |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 593 | <p>Moved to <a href="http://example.org/">example.org</a> |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 594 | or <a href="http://example.com/">example.com</a>.</p> |
| 595 | </body> |
| 596 | </html> |
| 597 | |
| 598 | Example of changing the attribute "target" of every link in first paragraph:: |
| 599 | |
| 600 | >>> from xml.etree.ElementTree import ElementTree |
| 601 | >>> tree = ElementTree() |
| 602 | >>> tree.parse("index.xhtml") |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 603 | <Element 'html' at 0xb77e6fac> |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 604 | >>> p = tree.find("body/p") # Finds first occurrence of tag p in body |
| 605 | >>> p |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 606 | <Element 'p' at 0xb77ec26c> |
| 607 | >>> links = list(p.iter("a")) # Returns list of all links |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 608 | >>> links |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 609 | [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 610 | >>> for i in links: # Iterates through all found links |
| 611 | ... i.attrib["target"] = "blank" |
| 612 | >>> tree.write("output.xhtml") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 613 | |
| 614 | .. _elementtree-qname-objects: |
| 615 | |
| 616 | QName Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 617 | ^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 618 | |
| 619 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 620 | .. class:: QName(text_or_uri, tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 621 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 622 | QName wrapper. This can be used to wrap a QName attribute value, in order |
| 623 | to get proper namespace handling on output. *text_or_uri* is a string |
| 624 | containing the QName value, in the form {uri}local, or, if the tag argument |
| 625 | is given, the URI part of a QName. If *tag* is given, the first argument is |
| 626 | interpreted as an URI, and this argument is interpreted as a local name. |
| 627 | :class:`QName` instances are opaque. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 628 | |
| 629 | |
| 630 | .. _elementtree-treebuilder-objects: |
| 631 | |
| 632 | TreeBuilder Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 633 | ^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 634 | |
| 635 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 636 | .. class:: TreeBuilder(element_factory=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 637 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 638 | Generic element structure builder. This builder converts a sequence of |
| 639 | start, data, and end method calls to a well-formed element structure. You |
| 640 | can use this class to build an element structure using a custom XML parser, |
| 641 | or a parser for some other XML-like format. The *element_factory* is called |
| 642 | to create new :class:`Element` instances when given. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 643 | |
| 644 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 645 | .. method:: close() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 646 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 647 | Flushes the builder buffers, and returns the toplevel document |
| 648 | element. Returns an :class:`Element` instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 649 | |
| 650 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 651 | .. method:: data(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 652 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 653 | Adds text to the current element. *data* is a string. This should be |
| 654 | either a bytestring, or a Unicode string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 655 | |
| 656 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 657 | .. method:: end(tag) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 658 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 659 | Closes the current element. *tag* is the element name. Returns the |
| 660 | closed element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 661 | |
| 662 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 663 | .. method:: start(tag, attrs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 664 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 665 | Opens a new element. *tag* is the element name. *attrs* is a dictionary |
| 666 | containing element attributes. Returns the opened element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 667 | |
| 668 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 669 | In addition, a custom :class:`TreeBuilder` object can provide the |
| 670 | following method: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 671 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 672 | .. method:: doctype(name, pubid, system) |
| 673 | |
| 674 | Handles a doctype declaration. *name* is the doctype name. *pubid* is |
| 675 | the public identifier. *system* is the system identifier. This method |
| 676 | does not exist on the default :class:`TreeBuilder` class. |
| 677 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 678 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 679 | |
| 680 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 681 | .. _elementtree-xmlparser-objects: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 682 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 683 | XMLParser Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 684 | ^^^^^^^^^^^^^^^^^ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 685 | |
| 686 | |
| 687 | .. class:: XMLParser(html=0, target=None, encoding=None) |
| 688 | |
| 689 | :class:`Element` structure builder for XML source data, based on the expat |
| 690 | parser. *html* are predefined HTML entities. This flag is not supported by |
| 691 | the current implementation. *target* is the target object. If omitted, the |
| 692 | builder uses an instance of the standard TreeBuilder class. *encoding* [1]_ |
| 693 | is optional. If given, the value overrides the encoding specified in the |
| 694 | XML file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 695 | |
| 696 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 697 | .. method:: close() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 698 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 699 | Finishes feeding data to the parser. Returns an element structure. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 700 | |
| 701 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 702 | .. method:: doctype(name, pubid, system) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 703 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 704 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 705 | Define the :meth:`TreeBuilder.doctype` method on a custom TreeBuilder |
| 706 | target. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 707 | |
| 708 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 709 | .. method:: feed(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 710 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 711 | Feeds data to the parser. *data* is encoded data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 712 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 713 | :meth:`XMLParser.feed` calls *target*\'s :meth:`start` method |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 714 | for each opening tag, its :meth:`end` method for each closing tag, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 715 | and data is processed by method :meth:`data`. :meth:`XMLParser.close` |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 716 | calls *target*\'s method :meth:`close`. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 717 | :class:`XMLParser` can be used not only for building a tree structure. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 718 | This is an example of counting the maximum depth of an XML file:: |
| 719 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 720 | >>> from xml.etree.ElementTree import XMLParser |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 721 | >>> class MaxDepth: # The target object of the parser |
| 722 | ... maxDepth = 0 |
| 723 | ... depth = 0 |
| 724 | ... def start(self, tag, attrib): # Called for each opening tag. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 725 | ... self.depth += 1 |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 726 | ... if self.depth > self.maxDepth: |
| 727 | ... self.maxDepth = self.depth |
| 728 | ... def end(self, tag): # Called for each closing tag. |
| 729 | ... self.depth -= 1 |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 730 | ... def data(self, data): |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 731 | ... pass # We do not need to do anything with data. |
| 732 | ... def close(self): # Called when all data has been parsed. |
| 733 | ... return self.maxDepth |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 734 | ... |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 735 | >>> target = MaxDepth() |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 736 | >>> parser = XMLParser(target=target) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 737 | >>> exampleXml = """ |
| 738 | ... <a> |
| 739 | ... <b> |
| 740 | ... </b> |
| 741 | ... <b> |
| 742 | ... <c> |
| 743 | ... <d> |
| 744 | ... </d> |
| 745 | ... </c> |
| 746 | ... </b> |
| 747 | ... </a>""" |
| 748 | >>> parser.feed(exampleXml) |
| 749 | >>> parser.close() |
| 750 | 4 |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 751 | |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 752 | Exceptions |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 753 | ^^^^^^^^^^ |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 754 | |
| 755 | .. class:: ParseError |
| 756 | |
| 757 | XML parse error, raised by the various parsing methods in this module when |
| 758 | parsing fails. The string representation of an instance of this exception |
| 759 | will contain a user-friendly error message. In addition, it will have |
| 760 | the following attributes available: |
| 761 | |
| 762 | .. attribute:: code |
| 763 | |
| 764 | A numeric error code from the expat parser. See the documentation of |
| 765 | :mod:`xml.parsers.expat` for the list of error codes and their meanings. |
| 766 | |
| 767 | .. attribute:: position |
| 768 | |
| 769 | A tuple of *line*, *column* numbers, specifying where the error occurred. |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 770 | |
| 771 | .. rubric:: Footnotes |
| 772 | |
| 773 | .. [#] The encoding string included in XML output should conform to the |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 774 | appropriate standards. For example, "UTF-8" is valid, but "UTF8" is |
| 775 | not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 776 | and http://www.iana.org/assignments/character-sets. |