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 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 479 | .. method:: find(match) |
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 |
| 483 | or ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 484 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 485 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 486 | .. method:: findall(match) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 487 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 488 | Finds all matching subelements, by tag name or |
| 489 | :ref:`path <elementtree-xpath>`. Returns a list containing all matching |
| 490 | elements in document order. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 491 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 492 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 493 | .. method:: findtext(match, default=None) |
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 | Finds text for the first subelement matching *match*. *match* may be |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 496 | a tag name or a :ref:`path <elementtree-xpath>`. Returns the text content |
| 497 | of the first matching element, or *default* if no element was found. |
| 498 | Note that if the matching element has no text content an empty string |
| 499 | is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 500 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 501 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 502 | .. method:: getchildren() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 504 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 505 | Use ``list(elem)`` or iteration. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 506 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 507 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 508 | .. method:: getiterator(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 510 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 511 | Use method :meth:`Element.iter` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 512 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 513 | |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 514 | .. method:: insert(index, subelement) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 516 | Inserts *subelement* at the given position in this element. Raises |
| 517 | :exc:`TypeError` if *subelement* is not an :class:`Element`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 518 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 519 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 520 | .. method:: iter(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 521 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 522 | Creates a tree :term:`iterator` with the current element as the root. |
| 523 | The iterator iterates over this element and all elements below it, in |
| 524 | document (depth first) order. If *tag* is not ``None`` or ``'*'``, only |
| 525 | elements whose tag equals *tag* are returned from the iterator. If the |
| 526 | tree structure is modified during iteration, the result is undefined. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 527 | |
Ezio Melotti | 138fc89 | 2011-10-10 00:02:03 +0300 | [diff] [blame] | 528 | .. versionadded:: 3.2 |
| 529 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 530 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 531 | .. method:: iterfind(match) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 532 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 533 | Finds all matching subelements, by tag name or |
| 534 | :ref:`path <elementtree-xpath>`. Returns an iterable yielding all |
| 535 | matching elements in document order. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 536 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 537 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 538 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 539 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 540 | .. method:: itertext() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 541 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 542 | Creates a text iterator. The iterator loops over this element and all |
| 543 | subelements, in document order, and returns all inner text. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 544 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 545 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 546 | |
| 547 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 548 | .. method:: makeelement(tag, attrib) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 549 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 550 | Creates a new element object of the same type as this element. Do not |
| 551 | call this method, use the :func:`SubElement` factory function instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 552 | |
| 553 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 554 | .. method:: remove(subelement) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 555 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 556 | Removes *subelement* from the element. Unlike the find\* methods this |
| 557 | method compares elements based on the instance identity, not on tag value |
| 558 | or contents. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 559 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 560 | :class:`Element` objects also support the following sequence type methods |
| 561 | for working with subelements: :meth:`__delitem__`, :meth:`__getitem__`, |
| 562 | :meth:`__setitem__`, :meth:`__len__`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 563 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 564 | Caution: Elements with no subelements will test as ``False``. This behavior |
| 565 | will change in future versions. Use specific ``len(elem)`` or ``elem is |
| 566 | None`` test instead. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 567 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 568 | element = root.find('foo') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 569 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 570 | if not element: # careful! |
| 571 | print("element not found, or element has no subelements") |
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 | if element is None: |
| 574 | print("element not found") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 575 | |
| 576 | |
| 577 | .. _elementtree-elementtree-objects: |
| 578 | |
| 579 | ElementTree Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 580 | ^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 581 | |
| 582 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 583 | .. class:: ElementTree(element=None, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 584 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 585 | ElementTree wrapper class. This class represents an entire element |
| 586 | hierarchy, and adds some extra support for serialization to and from |
| 587 | standard XML. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 588 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 589 | *element* is the root element. The tree is initialized with the contents |
| 590 | of the XML *file* if given. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 591 | |
| 592 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 593 | .. method:: _setroot(element) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 594 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 595 | Replaces the root element for this tree. This discards the current |
| 596 | contents of the tree, and replaces it with the given element. Use with |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 597 | care. *element* is an element instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 598 | |
| 599 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 600 | .. method:: find(match) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 601 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 602 | Same as :meth:`Element.find`, starting at the root of the tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 603 | |
| 604 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 605 | .. method:: findall(match) |
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.findall`, starting at the root of the tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 608 | |
| 609 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 610 | .. method:: findtext(match, default=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.findtext`, starting at the root of the tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 613 | |
| 614 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 615 | .. method:: getiterator(tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 616 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 617 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 618 | Use method :meth:`ElementTree.iter` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 619 | |
| 620 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 621 | .. method:: getroot() |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 622 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 623 | Returns the root element for this tree. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 624 | |
| 625 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 626 | .. method:: iter(tag=None) |
| 627 | |
| 628 | Creates and returns a tree iterator for the root element. The iterator |
| 629 | loops over all elements in this tree, in section order. *tag* is the tag |
| 630 | to look for (default is to return all elements) |
| 631 | |
| 632 | |
| 633 | .. method:: iterfind(match) |
| 634 | |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 635 | Same as :meth:`Element.iterfind`, starting at the root of the tree. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 636 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 637 | .. versionadded:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 638 | |
| 639 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 640 | .. method:: parse(source, parser=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 641 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 642 | 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] | 643 | name or :term:`file object`. *parser* is an optional parser instance. |
| 644 | If not given, the standard XMLParser parser is used. Returns the section |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 645 | root element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 646 | |
| 647 | |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 648 | .. method:: write(file, encoding="us-ascii", xml_declaration=None, method="xml") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 649 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 650 | 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] | 651 | :term:`file object` opened for writing. *encoding* [1]_ is the output encoding |
Florent Xicluna | c17f172 | 2010-08-08 19:48:29 +0000 | [diff] [blame] | 652 | (default is US-ASCII). Use ``encoding="unicode"`` to write a Unicode string. |
| 653 | *xml_declaration* controls if an XML declaration |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 654 | 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] | 655 | for only if not US-ASCII or UTF-8 or Unicode (default is None). *method* is |
| 656 | either ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``). |
| 657 | Returns an (optionally) encoded string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 658 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 659 | This is the XML file that is going to be manipulated:: |
| 660 | |
| 661 | <html> |
| 662 | <head> |
| 663 | <title>Example page</title> |
| 664 | </head> |
| 665 | <body> |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 666 | <p>Moved to <a href="http://example.org/">example.org</a> |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 667 | or <a href="http://example.com/">example.com</a>.</p> |
| 668 | </body> |
| 669 | </html> |
| 670 | |
| 671 | Example of changing the attribute "target" of every link in first paragraph:: |
| 672 | |
| 673 | >>> from xml.etree.ElementTree import ElementTree |
| 674 | >>> tree = ElementTree() |
| 675 | >>> tree.parse("index.xhtml") |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 676 | <Element 'html' at 0xb77e6fac> |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 677 | >>> p = tree.find("body/p") # Finds first occurrence of tag p in body |
| 678 | >>> p |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 679 | <Element 'p' at 0xb77ec26c> |
| 680 | >>> links = list(p.iter("a")) # Returns list of all links |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 681 | >>> links |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 682 | [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 683 | >>> for i in links: # Iterates through all found links |
| 684 | ... i.attrib["target"] = "blank" |
| 685 | >>> tree.write("output.xhtml") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 686 | |
| 687 | .. _elementtree-qname-objects: |
| 688 | |
| 689 | QName Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 690 | ^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 691 | |
| 692 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 693 | .. class:: QName(text_or_uri, tag=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 694 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 695 | QName wrapper. This can be used to wrap a QName attribute value, in order |
| 696 | to get proper namespace handling on output. *text_or_uri* is a string |
| 697 | containing the QName value, in the form {uri}local, or, if the tag argument |
| 698 | is given, the URI part of a QName. If *tag* is given, the first argument is |
| 699 | interpreted as an URI, and this argument is interpreted as a local name. |
| 700 | :class:`QName` instances are opaque. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 701 | |
| 702 | |
| 703 | .. _elementtree-treebuilder-objects: |
| 704 | |
| 705 | TreeBuilder Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 706 | ^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 707 | |
| 708 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 709 | .. class:: TreeBuilder(element_factory=None) |
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 | Generic element structure builder. This builder converts a sequence of |
| 712 | start, data, and end method calls to a well-formed element structure. You |
| 713 | can use this class to build an element structure using a custom XML parser, |
| 714 | or a parser for some other XML-like format. The *element_factory* is called |
| 715 | to create new :class:`Element` instances when given. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 716 | |
| 717 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 718 | .. method:: close() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 719 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 720 | Flushes the builder buffers, and returns the toplevel document |
| 721 | element. Returns an :class:`Element` instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 722 | |
| 723 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 724 | .. method:: data(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 725 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 726 | Adds text to the current element. *data* is a string. This should be |
| 727 | either a bytestring, or a Unicode string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 728 | |
| 729 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 730 | .. method:: end(tag) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 731 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 732 | Closes the current element. *tag* is the element name. Returns the |
| 733 | closed element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 734 | |
| 735 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 736 | .. method:: start(tag, attrs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 737 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 738 | Opens a new element. *tag* is the element name. *attrs* is a dictionary |
| 739 | containing element attributes. Returns the opened element. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 740 | |
| 741 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 742 | In addition, a custom :class:`TreeBuilder` object can provide the |
| 743 | following method: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 744 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 745 | .. method:: doctype(name, pubid, system) |
| 746 | |
| 747 | Handles a doctype declaration. *name* is the doctype name. *pubid* is |
| 748 | the public identifier. *system* is the system identifier. This method |
| 749 | does not exist on the default :class:`TreeBuilder` class. |
| 750 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 751 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 752 | |
| 753 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 754 | .. _elementtree-xmlparser-objects: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 755 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 756 | XMLParser Objects |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 757 | ^^^^^^^^^^^^^^^^^ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 758 | |
| 759 | |
| 760 | .. class:: XMLParser(html=0, target=None, encoding=None) |
| 761 | |
| 762 | :class:`Element` structure builder for XML source data, based on the expat |
| 763 | parser. *html* are predefined HTML entities. This flag is not supported by |
| 764 | the current implementation. *target* is the target object. If omitted, the |
| 765 | builder uses an instance of the standard TreeBuilder class. *encoding* [1]_ |
| 766 | is optional. If given, the value overrides the encoding specified in the |
| 767 | XML file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 768 | |
| 769 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 770 | .. method:: close() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 771 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 772 | Finishes feeding data to the parser. Returns an element structure. |
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:: doctype(name, pubid, system) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 776 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 777 | .. deprecated:: 3.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 778 | Define the :meth:`TreeBuilder.doctype` method on a custom TreeBuilder |
| 779 | target. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 780 | |
| 781 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 782 | .. method:: feed(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 783 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 784 | Feeds data to the parser. *data* is encoded data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 785 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 786 | :meth:`XMLParser.feed` calls *target*\'s :meth:`start` method |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 787 | for each opening tag, its :meth:`end` method for each closing tag, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 788 | and data is processed by method :meth:`data`. :meth:`XMLParser.close` |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 789 | calls *target*\'s method :meth:`close`. |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 790 | :class:`XMLParser` can be used not only for building a tree structure. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 791 | This is an example of counting the maximum depth of an XML file:: |
| 792 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 793 | >>> from xml.etree.ElementTree import XMLParser |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 794 | >>> class MaxDepth: # The target object of the parser |
| 795 | ... maxDepth = 0 |
| 796 | ... depth = 0 |
| 797 | ... def start(self, tag, attrib): # Called for each opening tag. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 798 | ... self.depth += 1 |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 799 | ... if self.depth > self.maxDepth: |
| 800 | ... self.maxDepth = self.depth |
| 801 | ... def end(self, tag): # Called for each closing tag. |
| 802 | ... self.depth -= 1 |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 803 | ... def data(self, data): |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 804 | ... pass # We do not need to do anything with data. |
| 805 | ... def close(self): # Called when all data has been parsed. |
| 806 | ... return self.maxDepth |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 807 | ... |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 808 | >>> target = MaxDepth() |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 809 | >>> parser = XMLParser(target=target) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 810 | >>> exampleXml = """ |
| 811 | ... <a> |
| 812 | ... <b> |
| 813 | ... </b> |
| 814 | ... <b> |
| 815 | ... <c> |
| 816 | ... <d> |
| 817 | ... </d> |
| 818 | ... </c> |
| 819 | ... </b> |
| 820 | ... </a>""" |
| 821 | >>> parser.feed(exampleXml) |
| 822 | >>> parser.close() |
| 823 | 4 |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 824 | |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 825 | Exceptions |
Eli Bendersky | 3a4875e | 2012-03-26 20:43:32 +0200 | [diff] [blame] | 826 | ^^^^^^^^^^ |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 827 | |
| 828 | .. class:: ParseError |
| 829 | |
| 830 | XML parse error, raised by the various parsing methods in this module when |
| 831 | parsing fails. The string representation of an instance of this exception |
| 832 | will contain a user-friendly error message. In addition, it will have |
| 833 | the following attributes available: |
| 834 | |
| 835 | .. attribute:: code |
| 836 | |
| 837 | A numeric error code from the expat parser. See the documentation of |
| 838 | :mod:`xml.parsers.expat` for the list of error codes and their meanings. |
| 839 | |
| 840 | .. attribute:: position |
| 841 | |
| 842 | A tuple of *line*, *column* numbers, specifying where the error occurred. |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 843 | |
| 844 | .. rubric:: Footnotes |
| 845 | |
| 846 | .. [#] The encoding string included in XML output should conform to the |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 847 | appropriate standards. For example, "UTF-8" is valid, but "UTF8" is |
| 848 | not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 849 | and http://www.iana.org/assignments/character-sets. |