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 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 8 | The :mod:`xml.etree.ElementTree` module implements a simple and efficient API |
| 9 | for parsing and creating XML data. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 10 | |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 11 | .. versionchanged:: 3.3 |
| 12 | This module will use a fast implementation whenever available. |
| 13 | The :mod:`xml.etree.cElementTree` module is deprecated. |
| 14 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 15 | Tutorial |
| 16 | -------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 18 | This is a short tutorial for using :mod:`xml.etree.ElementTree` (``ET`` in |
| 19 | short). The goal is to demonstrate some of the building blocks and basic |
| 20 | concepts of the module. |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 21 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 22 | XML tree and elements |
| 23 | ^^^^^^^^^^^^^^^^^^^^^ |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 24 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 25 | XML is an inherently hierarchical data format, and the most natural way to |
| 26 | represent 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 |
| 29 | the whole document (reading and writing to/from files) are usually done |
| 30 | on the :class:`ElementTree` level. Interactions with a single XML element |
| 31 | and its sub-elements are done on the :class:`Element` level. |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 32 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 33 | .. _elementtree-parsing-xml: |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 34 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 35 | Parsing XML |
| 36 | ^^^^^^^^^^^ |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 37 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 38 | We'll be using the following XML document contained in a Python string as the |
| 39 | sample data for this section:: |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 40 | |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 41 | countrydata = r'''<?xml version="1.0"?> |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 42 | <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 Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 66 | First, 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`, |
| 73 | which is the root element of the parsed tree. Other parsing functions may |
| 74 | create an :class:`ElementTree`. Make sure to check the documentation to be |
| 75 | sure. |
| 76 | |
| 77 | As an :class:`Element`, ``root`` has a tag and a dictionary of attributes:: |
| 78 | |
| 79 | >>> root.tag |
| 80 | 'data' |
| 81 | >>> root.attrib |
| 82 | {} |
| 83 | |
| 84 | It 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 | |
| 93 | Children are nested, and we can access specific child nodes by index:: |
| 94 | |
| 95 | >>> root[0][1].text |
| 96 | '2008' |
| 97 | |
| 98 | Finding interesting elements |
| 99 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 100 | |
| 101 | :class:`Element` has some useful methods that help iterate recursively over all |
| 102 | the 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 | |
| 114 | More sophisticated specification of which elements to look for is possible by |
| 115 | using :ref:`XPath <elementtree-xpath>`. |
| 116 | |
| 117 | Building XML documents |
| 118 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 119 | |
| 120 | ``ET`` provides a simple way to build XML documents and write them to files. |
| 121 | The :meth:`ElementTree.write` method serves this purpose. |
| 122 | |
| 123 | Once created, an :class:`Element` object may be manipulated by directly changing |
| 124 | its fields (such as :attr:`Element.text`), adding and modifying attributes |
| 125 | (:meth:`Element.set` method), as well as adding new children (for example |
| 126 | with :meth:`Element.append`). |
| 127 | |
| 128 | The :func:`SubElement` function also provides a convenient way to create new |
| 129 | sub-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 | |
| 138 | Additional resources |
| 139 | ^^^^^^^^^^^^^^^^^^^^ |
| 140 | |
| 141 | See http://effbot.org/zone/element-index.htm for tutorials and links to other |
| 142 | docs. |
| 143 | |
| 144 | |
| 145 | .. _elementtree-xpath: |
| 146 | |
| 147 | XPath support |
| 148 | ------------- |
| 149 | |
| 150 | This module provides limited support for |
| 151 | `XPath expressions <http://www.w3.org/TR/xpath>`_ for locating elements in a |
| 152 | tree. The goal is to support a small subset of the abbreviated syntax; a full |
| 153 | XPath engine is outside the scope of the module. |
| 154 | |
| 155 | Example |
| 156 | ^^^^^^^ |
| 157 | |
| 158 | Here's an example that demonstrates some of the XPath capabilities of the |
| 159 | module. 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 Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 165 | |
| 166 | # Top-level elements |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 167 | root.findall(".") |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 168 | |
| 169 | # All 'neighbor' grand-children of 'country' children of the top-level |
| 170 | # elements |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 171 | root.findall("./country/neighbor") |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 172 | |
| 173 | # Nodes with name='Singapore' that have a 'year' child |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 174 | root.findall(".//year/..[@name='Singapore']") |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 175 | |
| 176 | # 'year' nodes that are children of nodes with name='Singapore' |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 177 | root.findall(".//*[@name='Singapore']/year") |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 178 | |
| 179 | # All 'neighbor' nodes that are the second child of their parent |
Eli Bendersky | c1d9869 | 2012-03-30 11:44:15 +0300 | [diff] [blame] | 180 | root.findall(".//neighbor[2]") |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 181 | |
| 182 | Supported 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 Bendersky | ede001a | 2012-03-27 04:57:23 +0200 | [diff] [blame] | 202 | | | current element. For example, ``.//egg`` selects | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 203 | | | 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 | |
| 223 | Predicates (expressions within square brackets) must be preceded by a tag |
| 224 | name, an asterisk, or another predicate. ``position`` predicates must be |
| 225 | preceded by a tag name. |
| 226 | |
| 227 | Reference |
| 228 | --------- |
| 229 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | .. _elementtree-functions: |
| 231 | |
| 232 | Functions |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 233 | ^^^^^^^^^ |
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:: Comment(text=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 238 | Comment element factory. This factory function creates a special element |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 239 | 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 Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 242 | representing a comment. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 243 | |
| 244 | |
| 245 | .. function:: dump(elem) |
| 246 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 247 | Writes an element tree or element structure to sys.stdout. This function |
| 248 | should be used for debugging only. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 256 | .. function:: fromstring(text) |
| 257 | |
Florent Xicluna | dddd5e9 | 2010-03-14 01:28:07 +0000 | [diff] [blame] | 258 | 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 Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 260 | |
| 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 Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 269 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | .. function:: iselement(element) |
| 273 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 274 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 276 | |
| 277 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 278 | .. function:: iterparse(source, events=None, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 279 | |
| 280 | 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] | 281 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 290 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 301 | .. function:: parse(source, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 302 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 303 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 307 | |
| 308 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 309 | .. function:: ProcessingInstruction(target, text=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 311 | 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 Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 325 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 326 | |
| 327 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 328 | .. function:: SubElement(parent, tag, attrib={}, **extra) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 329 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 330 | Subelement factory. This function creates an element instance, and appends |
| 331 | it to an existing element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 332 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 333 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | |
| 339 | |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 340 | .. function:: tostring(element, encoding="us-ascii", method="xml") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 341 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 342 | Generates a string representation of an XML element, including all |
Florent Xicluna | dddd5e9 | 2010-03-14 01:28:07 +0000 | [diff] [blame] | 343 | subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 344 | the output encoding (default is US-ASCII). Use ``encoding="unicode"`` to |
| 345 | generate a Unicode string. *method* is either ``"xml"``, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 346 | ``"html"`` or ``"text"`` (default is ``"xml"``). Returns an (optionally) |
| 347 | encoded string containing the XML data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 348 | |
| 349 | |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 350 | .. function:: tostringlist(element, encoding="us-ascii", method="xml") |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 351 | |
| 352 | Generates a string representation of an XML element, including all |
Florent Xicluna | dddd5e9 | 2010-03-14 01:28:07 +0000 | [diff] [blame] | 353 | subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 354 | the output encoding (default is US-ASCII). Use ``encoding="unicode"`` to |
| 355 | generate a Unicode string. *method* is either ``"xml"``, |
Florent Xicluna | dddd5e9 | 2010-03-14 01:28:07 +0000 | [diff] [blame] | 356 | ``"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 Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 360 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 361 | .. versionadded:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 362 | |
| 363 | |
| 364 | .. function:: XML(text, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 365 | |
| 366 | 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] | 367 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 370 | |
| 371 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 372 | .. function:: XMLID(text, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 373 | |
| 374 | 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] | 375 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 379 | |
| 380 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 381 | .. _elementtree-element-objects: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 383 | Element Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 384 | ^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 385 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 386 | .. class:: Element(tag, attrib={}, **extra) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 388 | Element class. This class defines the Element interface, and provides a |
| 389 | reference implementation of this interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 390 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 391 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 395 | |
| 396 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 397 | .. attribute:: tag |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 399 | A string identifying what kind of data this element represents (the |
| 400 | element type, in other words). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | |
| 402 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 403 | .. attribute:: text |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 404 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 405 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 410 | |
| 411 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 412 | .. attribute:: tail |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 413 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 414 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 420 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 421 | .. attribute:: attrib |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 422 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 423 | 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 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 | The following dictionary-like methods work on the element attributes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 430 | |
| 431 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 432 | .. method:: clear() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 434 | Resets an element. This function removes all subelements, clears all |
| 435 | attributes, and sets the text and tail attributes to None. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 436 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 437 | |
| 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 Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 466 | 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 Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 469 | |
| 470 | |
| 471 | .. method:: extend(subelements) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 472 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 473 | Appends *subelements* from a sequence object with zero or more elements. |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 474 | Raises :exc:`TypeError` if a subelement is not an :class:`Element`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 475 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 476 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 477 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 478 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 479 | .. method:: find(match, namespaces=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 480 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 481 | Finds the first subelement matching *match*. *match* may be a tag name |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 482 | or a :ref:`path <elementtree-xpath>`. Returns an element instance |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 483 | or ``None``. *namespaces* is an optional mapping from namespace prefix |
| 484 | to full name. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 485 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 486 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 487 | .. method:: findall(match, namespaces=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 488 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 489 | Finds all matching subelements, by tag name or |
| 490 | :ref:`path <elementtree-xpath>`. Returns a list containing all matching |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 491 | elements in document order. *namespaces* is an optional mapping from |
| 492 | namespace prefix to full name. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 493 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 494 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 495 | .. method:: findtext(match, default=None, namespaces=None) |
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 | Finds text for the first subelement matching *match*. *match* may be |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 498 | a tag name or a :ref:`path <elementtree-xpath>`. Returns the text content |
| 499 | of the first matching element, or *default* if no element was found. |
| 500 | Note that if the matching element has no text content an empty string |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 501 | is returned. *namespaces* is an optional mapping from namespace prefix |
| 502 | to full name. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 504 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 505 | .. method:: getchildren() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 506 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 507 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 508 | Use ``list(elem)`` or iteration. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 510 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 511 | .. method:: getiterator(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 512 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 513 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 514 | Use method :meth:`Element.iter` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 516 | |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 517 | .. method:: insert(index, subelement) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 518 | |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 519 | Inserts *subelement* at the given position in this element. Raises |
| 520 | :exc:`TypeError` if *subelement* is not an :class:`Element`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 521 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 522 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 523 | .. method:: iter(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 524 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 525 | Creates a tree :term:`iterator` with the current element as the root. |
| 526 | The iterator iterates over this element and all elements below it, in |
| 527 | document (depth first) order. If *tag* is not ``None`` or ``'*'``, only |
| 528 | elements whose tag equals *tag* are returned from the iterator. If the |
| 529 | tree structure is modified during iteration, the result is undefined. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 530 | |
Ezio Melotti | 138fc89 | 2011-10-10 00:02:03 +0300 | [diff] [blame] | 531 | .. versionadded:: 3.2 |
| 532 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 533 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 534 | .. method:: iterfind(match, namespaces=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 536 | Finds all matching subelements, by tag name or |
| 537 | :ref:`path <elementtree-xpath>`. Returns an iterable yielding all |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 538 | matching elements in document order. *namespaces* is an optional mapping |
| 539 | from namespace prefix to full name. |
| 540 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 541 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 542 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 543 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 544 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 545 | .. method:: itertext() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 546 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 547 | Creates a text iterator. The iterator loops over this element and all |
| 548 | subelements, in document order, and returns all inner text. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 549 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 550 | .. versionadded:: 3.2 |
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:: makeelement(tag, attrib) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 554 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 555 | Creates a new element object of the same type as this element. Do not |
| 556 | call this method, use the :func:`SubElement` factory function instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 557 | |
| 558 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 559 | .. method:: remove(subelement) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 560 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 561 | Removes *subelement* from the element. Unlike the find\* methods this |
| 562 | method compares elements based on the instance identity, not on tag value |
| 563 | or contents. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 564 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 565 | :class:`Element` objects also support the following sequence type methods |
| 566 | for working with subelements: :meth:`__delitem__`, :meth:`__getitem__`, |
| 567 | :meth:`__setitem__`, :meth:`__len__`. |
Georg 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 | Caution: Elements with no subelements will test as ``False``. This behavior |
| 570 | will change in future versions. Use specific ``len(elem)`` or ``elem is |
| 571 | None`` test instead. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 572 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 573 | element = root.find('foo') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 574 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 575 | if not element: # careful! |
| 576 | print("element not found, or element has no subelements") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 577 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 578 | if element is None: |
| 579 | print("element not found") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 580 | |
| 581 | |
| 582 | .. _elementtree-elementtree-objects: |
| 583 | |
| 584 | ElementTree Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 585 | ^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 586 | |
| 587 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 588 | .. class:: ElementTree(element=None, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 589 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 590 | ElementTree wrapper class. This class represents an entire element |
| 591 | hierarchy, and adds some extra support for serialization to and from |
| 592 | standard XML. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 593 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 594 | *element* is the root element. The tree is initialized with the contents |
| 595 | of the XML *file* if given. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 596 | |
| 597 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 598 | .. method:: _setroot(element) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 599 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 600 | Replaces the root element for this tree. This discards the current |
| 601 | contents of the tree, and replaces it with the given element. Use with |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 602 | care. *element* is an element instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 603 | |
| 604 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 605 | .. method:: find(match, namespaces=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 606 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 607 | Same as :meth:`Element.find`, starting at the root of the tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 608 | |
| 609 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 610 | .. method:: findall(match, namespaces=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 611 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 612 | Same as :meth:`Element.findall`, starting at the root of the tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 613 | |
| 614 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 615 | .. method:: findtext(match, default=None, namespaces=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 616 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 617 | Same as :meth:`Element.findtext`, starting at the root of the tree. |
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 | .. method:: getiterator(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 621 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 622 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 623 | Use method :meth:`ElementTree.iter` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 624 | |
| 625 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 626 | .. method:: getroot() |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 627 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 628 | Returns the root element for this tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 629 | |
| 630 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 631 | .. method:: iter(tag=None) |
| 632 | |
| 633 | Creates and returns a tree iterator for the root element. The iterator |
| 634 | loops over all elements in this tree, in section order. *tag* is the tag |
| 635 | to look for (default is to return all elements) |
| 636 | |
| 637 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 638 | .. method:: iterfind(match, namespaces=None) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 639 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 640 | Same as :meth:`Element.iterfind`, starting at the root of the tree. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 641 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 642 | .. versionadded:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 643 | |
| 644 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 645 | .. method:: parse(source, parser=None) |
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 | 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] | 648 | name or :term:`file object`. *parser* is an optional parser instance. |
| 649 | If not given, the standard XMLParser parser is used. Returns the section |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 650 | root element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 651 | |
| 652 | |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 653 | .. method:: write(file, encoding="us-ascii", xml_declaration=None, method="xml") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 654 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 655 | 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] | 656 | :term:`file object` opened for writing. *encoding* [1]_ is the output encoding |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 657 | (default is US-ASCII). Use ``encoding="unicode"`` to write a Unicode string. |
| 658 | *xml_declaration* controls if an XML declaration |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 659 | 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] | 660 | for only if not US-ASCII or UTF-8 or Unicode (default is None). *method* is |
| 661 | either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``). |
| 662 | Returns an (optionally) encoded string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 663 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 664 | This is the XML file that is going to be manipulated:: |
| 665 | |
| 666 | <html> |
| 667 | <head> |
| 668 | <title>Example page</title> |
| 669 | </head> |
| 670 | <body> |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 671 | <p>Moved to <a href="http://example.org/">example.org</a> |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 672 | or <a href="http://example.com/">example.com</a>.</p> |
| 673 | </body> |
| 674 | </html> |
| 675 | |
| 676 | Example of changing the attribute "target" of every link in first paragraph:: |
| 677 | |
| 678 | >>> from xml.etree.ElementTree import ElementTree |
| 679 | >>> tree = ElementTree() |
| 680 | >>> tree.parse("index.xhtml") |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 681 | <Element 'html' at 0xb77e6fac> |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 682 | >>> p = tree.find("body/p") # Finds first occurrence of tag p in body |
| 683 | >>> p |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 684 | <Element 'p' at 0xb77ec26c> |
| 685 | >>> links = list(p.iter("a")) # Returns list of all links |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 686 | >>> links |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 687 | [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 688 | >>> for i in links: # Iterates through all found links |
| 689 | ... i.attrib["target"] = "blank" |
| 690 | >>> tree.write("output.xhtml") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 691 | |
| 692 | .. _elementtree-qname-objects: |
| 693 | |
| 694 | QName Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 695 | ^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 696 | |
| 697 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 698 | .. class:: QName(text_or_uri, tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 699 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 700 | QName wrapper. This can be used to wrap a QName attribute value, in order |
| 701 | to get proper namespace handling on output. *text_or_uri* is a string |
| 702 | containing the QName value, in the form {uri}local, or, if the tag argument |
| 703 | is given, the URI part of a QName. If *tag* is given, the first argument is |
| 704 | interpreted as an URI, and this argument is interpreted as a local name. |
| 705 | :class:`QName` instances are opaque. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 706 | |
| 707 | |
| 708 | .. _elementtree-treebuilder-objects: |
| 709 | |
| 710 | TreeBuilder Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 711 | ^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 712 | |
| 713 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 714 | .. class:: TreeBuilder(element_factory=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 715 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 716 | Generic element structure builder. This builder converts a sequence of |
| 717 | start, data, and end method calls to a well-formed element structure. You |
| 718 | can use this class to build an element structure using a custom XML parser, |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 719 | or a parser for some other XML-like format. *element_factory*, when given, |
| 720 | must be a callable accepting two positional arguments: a tag and |
| 721 | a dict of attributes. It is expected to return a new element instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 722 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 723 | .. method:: close() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 724 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 725 | Flushes the builder buffers, and returns the toplevel document |
| 726 | element. Returns an :class:`Element` instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 727 | |
| 728 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 729 | .. method:: data(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 730 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 731 | Adds text to the current element. *data* is a string. This should be |
| 732 | either a bytestring, or a Unicode string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 733 | |
| 734 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 735 | .. method:: end(tag) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 736 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 737 | Closes the current element. *tag* is the element name. Returns the |
| 738 | closed element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 739 | |
| 740 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 741 | .. method:: start(tag, attrs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 742 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 743 | Opens a new element. *tag* is the element name. *attrs* is a dictionary |
| 744 | containing element attributes. Returns the opened element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 745 | |
| 746 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 747 | In addition, a custom :class:`TreeBuilder` object can provide the |
| 748 | following method: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 749 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 750 | .. method:: doctype(name, pubid, system) |
| 751 | |
| 752 | Handles a doctype declaration. *name* is the doctype name. *pubid* is |
| 753 | the public identifier. *system* is the system identifier. This method |
| 754 | does not exist on the default :class:`TreeBuilder` class. |
| 755 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 756 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 757 | |
| 758 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 759 | .. _elementtree-xmlparser-objects: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 760 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 761 | XMLParser Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 762 | ^^^^^^^^^^^^^^^^^ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 763 | |
| 764 | |
| 765 | .. class:: XMLParser(html=0, target=None, encoding=None) |
| 766 | |
| 767 | :class:`Element` structure builder for XML source data, based on the expat |
| 768 | parser. *html* are predefined HTML entities. This flag is not supported by |
| 769 | the current implementation. *target* is the target object. If omitted, the |
| 770 | builder uses an instance of the standard TreeBuilder class. *encoding* [1]_ |
| 771 | is optional. If given, the value overrides the encoding specified in the |
| 772 | XML file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 773 | |
| 774 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 775 | .. method:: close() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 776 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 777 | Finishes feeding data to the parser. Returns an element structure. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 778 | |
| 779 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 780 | .. method:: doctype(name, pubid, system) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 781 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 782 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 783 | Define the :meth:`TreeBuilder.doctype` method on a custom TreeBuilder |
| 784 | target. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 785 | |
| 786 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 787 | .. method:: feed(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 788 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 789 | Feeds data to the parser. *data* is encoded data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 790 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 791 | :meth:`XMLParser.feed` calls *target*\'s :meth:`start` method |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 792 | for each opening tag, its :meth:`end` method for each closing tag, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 793 | and data is processed by method :meth:`data`. :meth:`XMLParser.close` |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 794 | calls *target*\'s method :meth:`close`. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 795 | :class:`XMLParser` can be used not only for building a tree structure. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 796 | This is an example of counting the maximum depth of an XML file:: |
| 797 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 798 | >>> from xml.etree.ElementTree import XMLParser |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 799 | >>> class MaxDepth: # The target object of the parser |
| 800 | ... maxDepth = 0 |
| 801 | ... depth = 0 |
| 802 | ... def start(self, tag, attrib): # Called for each opening tag. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 803 | ... self.depth += 1 |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 804 | ... if self.depth > self.maxDepth: |
| 805 | ... self.maxDepth = self.depth |
| 806 | ... def end(self, tag): # Called for each closing tag. |
| 807 | ... self.depth -= 1 |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 808 | ... def data(self, data): |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 809 | ... pass # We do not need to do anything with data. |
| 810 | ... def close(self): # Called when all data has been parsed. |
| 811 | ... return self.maxDepth |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 812 | ... |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 813 | >>> target = MaxDepth() |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 814 | >>> parser = XMLParser(target=target) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 815 | >>> exampleXml = """ |
| 816 | ... <a> |
| 817 | ... <b> |
| 818 | ... </b> |
| 819 | ... <b> |
| 820 | ... <c> |
| 821 | ... <d> |
| 822 | ... </d> |
| 823 | ... </c> |
| 824 | ... </b> |
| 825 | ... </a>""" |
| 826 | >>> parser.feed(exampleXml) |
| 827 | >>> parser.close() |
| 828 | 4 |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 829 | |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 830 | Exceptions |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 831 | ^^^^^^^^^^ |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 832 | |
| 833 | .. class:: ParseError |
| 834 | |
| 835 | XML parse error, raised by the various parsing methods in this module when |
| 836 | parsing fails. The string representation of an instance of this exception |
| 837 | will contain a user-friendly error message. In addition, it will have |
| 838 | the following attributes available: |
| 839 | |
| 840 | .. attribute:: code |
| 841 | |
| 842 | A numeric error code from the expat parser. See the documentation of |
| 843 | :mod:`xml.parsers.expat` for the list of error codes and their meanings. |
| 844 | |
| 845 | .. attribute:: position |
| 846 | |
| 847 | A tuple of *line*, *column* numbers, specifying where the error occurred. |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 848 | |
| 849 | .. rubric:: Footnotes |
| 850 | |
| 851 | .. [#] The encoding string included in XML output should conform to the |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 852 | appropriate standards. For example, "UTF-8" is valid, but "UTF8" is |
| 853 | not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 854 | and http://www.iana.org/assignments/character-sets. |