Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
| 8 | |
| 9 | .. versionadded:: 2.5 |
| 10 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 11 | **Source code:** :source:`Lib/xml/etree/ElementTree.py` |
| 12 | |
| 13 | -------------- |
| 14 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 15 | The :class:`Element` type is a flexible container object, designed to store |
| 16 | hierarchical data structures in memory. The type can be described as a cross |
| 17 | between a list and a dictionary. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | |
Christian Heimes | 23790b4 | 2013-03-26 17:53:05 +0100 | [diff] [blame] | 19 | |
| 20 | .. warning:: |
| 21 | |
| 22 | The :mod:`xml.etree.ElementTree` module is not secure against |
| 23 | maliciously constructed data. If you need to parse untrusted or |
| 24 | unauthenticated data see :ref:`xml-vulnerabilities`. |
| 25 | |
| 26 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 27 | Each element has a number of properties associated with it: |
| 28 | |
| 29 | * a tag which is a string identifying what kind of data this element represents |
| 30 | (the element type, in other words). |
| 31 | |
| 32 | * a number of attributes, stored in a Python dictionary. |
| 33 | |
| 34 | * a text string. |
| 35 | |
| 36 | * an optional tail string. |
| 37 | |
| 38 | * a number of child elements, stored in a Python sequence |
| 39 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 40 | To create an element instance, use the :class:`Element` constructor or the |
| 41 | :func:`SubElement` factory function. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 42 | |
| 43 | The :class:`ElementTree` class can be used to wrap an element structure, and |
| 44 | convert it from and to XML. |
| 45 | |
| 46 | A C implementation of this API is available as :mod:`xml.etree.cElementTree`. |
| 47 | |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 48 | See http://effbot.org/zone/element-index.htm for tutorials and links to other |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 49 | docs. Fredrik Lundh's page is also the location of the development version of |
| 50 | the xml.etree.ElementTree. |
| 51 | |
| 52 | .. versionchanged:: 2.7 |
| 53 | The ElementTree API is updated to 1.3. For more information, see |
| 54 | `Introducing ElementTree 1.3 |
| 55 | <http://effbot.org/zone/elementtree-13-intro.htm>`_. |
| 56 | |
Eli Bendersky | 6ee2187 | 2012-08-18 05:40:38 +0300 | [diff] [blame] | 57 | Tutorial |
| 58 | -------- |
| 59 | |
| 60 | This is a short tutorial for using :mod:`xml.etree.ElementTree` (``ET`` in |
| 61 | short). The goal is to demonstrate some of the building blocks and basic |
| 62 | concepts of the module. |
| 63 | |
| 64 | XML tree and elements |
| 65 | ^^^^^^^^^^^^^^^^^^^^^ |
| 66 | |
| 67 | XML is an inherently hierarchical data format, and the most natural way to |
| 68 | represent it is with a tree. ``ET`` has two classes for this purpose - |
| 69 | :class:`ElementTree` represents the whole XML document as a tree, and |
| 70 | :class:`Element` represents a single node in this tree. Interactions with |
| 71 | the whole document (reading and writing to/from files) are usually done |
| 72 | on the :class:`ElementTree` level. Interactions with a single XML element |
| 73 | and its sub-elements are done on the :class:`Element` level. |
| 74 | |
| 75 | .. _elementtree-parsing-xml: |
| 76 | |
| 77 | Parsing XML |
| 78 | ^^^^^^^^^^^ |
| 79 | |
| 80 | We'll be using the following XML document as the sample data for this section: |
| 81 | |
| 82 | .. code-block:: xml |
| 83 | |
| 84 | <?xml version="1.0"?> |
| 85 | <data> |
| 86 | <country name="Liechtenstein"> |
| 87 | <rank>1</rank> |
| 88 | <year>2008</year> |
| 89 | <gdppc>141100</gdppc> |
| 90 | <neighbor name="Austria" direction="E"/> |
| 91 | <neighbor name="Switzerland" direction="W"/> |
| 92 | </country> |
| 93 | <country name="Singapore"> |
| 94 | <rank>4</rank> |
| 95 | <year>2011</year> |
| 96 | <gdppc>59900</gdppc> |
| 97 | <neighbor name="Malaysia" direction="N"/> |
| 98 | </country> |
| 99 | <country name="Panama"> |
| 100 | <rank>68</rank> |
| 101 | <year>2011</year> |
| 102 | <gdppc>13600</gdppc> |
| 103 | <neighbor name="Costa Rica" direction="W"/> |
| 104 | <neighbor name="Colombia" direction="E"/> |
| 105 | </country> |
| 106 | </data> |
| 107 | |
| 108 | We have a number of ways to import the data. Reading the file from disk:: |
| 109 | |
| 110 | import xml.etree.ElementTree as ET |
| 111 | tree = ET.parse('country_data.xml') |
| 112 | root = tree.getroot() |
| 113 | |
| 114 | Reading the data from a string:: |
| 115 | |
| 116 | root = ET.fromstring(country_data_as_string) |
| 117 | |
| 118 | :func:`fromstring` parses XML from a string directly into an :class:`Element`, |
| 119 | which is the root element of the parsed tree. Other parsing functions may |
| 120 | create an :class:`ElementTree`. Check the documentation to be sure. |
| 121 | |
| 122 | As an :class:`Element`, ``root`` has a tag and a dictionary of attributes:: |
| 123 | |
| 124 | >>> root.tag |
| 125 | 'data' |
| 126 | >>> root.attrib |
| 127 | {} |
| 128 | |
| 129 | It also has children nodes over which we can iterate:: |
| 130 | |
| 131 | >>> for child in root: |
| 132 | ... print child.tag, child.attrib |
| 133 | ... |
| 134 | country {'name': 'Liechtenstein'} |
| 135 | country {'name': 'Singapore'} |
| 136 | country {'name': 'Panama'} |
| 137 | |
| 138 | Children are nested, and we can access specific child nodes by index:: |
| 139 | |
| 140 | >>> root[0][1].text |
| 141 | '2008' |
| 142 | |
| 143 | Finding interesting elements |
| 144 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 145 | |
| 146 | :class:`Element` has some useful methods that help iterate recursively over all |
| 147 | the sub-tree below it (its children, their children, and so on). For example, |
| 148 | :meth:`Element.iter`:: |
| 149 | |
| 150 | >>> for neighbor in root.iter('neighbor'): |
| 151 | ... print neighbor.attrib |
| 152 | ... |
| 153 | {'name': 'Austria', 'direction': 'E'} |
| 154 | {'name': 'Switzerland', 'direction': 'W'} |
| 155 | {'name': 'Malaysia', 'direction': 'N'} |
| 156 | {'name': 'Costa Rica', 'direction': 'W'} |
| 157 | {'name': 'Colombia', 'direction': 'E'} |
| 158 | |
| 159 | :meth:`Element.findall` finds only elements with a tag which are direct |
| 160 | children of the current element. :meth:`Element.find` finds the *first* child |
| 161 | with a particular tag, and :meth:`Element.text` accesses the element's text |
| 162 | content. :meth:`Element.get` accesses the element's attributes:: |
| 163 | |
| 164 | >>> for country in root.findall('country'): |
| 165 | ... rank = country.find('rank').text |
| 166 | ... name = country.get('name') |
| 167 | ... print name, rank |
| 168 | ... |
| 169 | Liechtenstein 1 |
| 170 | Singapore 4 |
| 171 | Panama 68 |
| 172 | |
| 173 | More sophisticated specification of which elements to look for is possible by |
| 174 | using :ref:`XPath <elementtree-xpath>`. |
| 175 | |
| 176 | Modifying an XML File |
| 177 | ^^^^^^^^^^^^^^^^^^^^^ |
| 178 | |
| 179 | :class:`ElementTree` provides a simple way to build XML documents and write them to files. |
| 180 | The :meth:`ElementTree.write` method serves this purpose. |
| 181 | |
| 182 | Once created, an :class:`Element` object may be manipulated by directly changing |
| 183 | its fields (such as :attr:`Element.text`), adding and modifying attributes |
| 184 | (:meth:`Element.set` method), as well as adding new children (for example |
| 185 | with :meth:`Element.append`). |
| 186 | |
| 187 | Let's say we want to add one to each country's rank, and add an ``updated`` |
| 188 | attribute to the rank element:: |
| 189 | |
| 190 | >>> for rank in root.iter('rank'): |
| 191 | ... new_rank = int(rank.text) + 1 |
| 192 | ... rank.text = str(new_rank) |
| 193 | ... rank.set('updated', 'yes') |
| 194 | ... |
| 195 | >>> tree.write('output.xml') |
| 196 | |
| 197 | Our XML now looks like this: |
| 198 | |
| 199 | .. code-block:: xml |
| 200 | |
| 201 | <?xml version="1.0"?> |
| 202 | <data> |
| 203 | <country name="Liechtenstein"> |
| 204 | <rank updated="yes">2</rank> |
| 205 | <year>2008</year> |
| 206 | <gdppc>141100</gdppc> |
| 207 | <neighbor name="Austria" direction="E"/> |
| 208 | <neighbor name="Switzerland" direction="W"/> |
| 209 | </country> |
| 210 | <country name="Singapore"> |
| 211 | <rank updated="yes">5</rank> |
| 212 | <year>2011</year> |
| 213 | <gdppc>59900</gdppc> |
| 214 | <neighbor name="Malaysia" direction="N"/> |
| 215 | </country> |
| 216 | <country name="Panama"> |
| 217 | <rank updated="yes">69</rank> |
| 218 | <year>2011</year> |
| 219 | <gdppc>13600</gdppc> |
| 220 | <neighbor name="Costa Rica" direction="W"/> |
| 221 | <neighbor name="Colombia" direction="E"/> |
| 222 | </country> |
| 223 | </data> |
| 224 | |
| 225 | We can remove elements using :meth:`Element.remove`. Let's say we want to |
| 226 | remove all countries with a rank higher than 50:: |
| 227 | |
| 228 | >>> for country in root.findall('country'): |
| 229 | ... rank = int(country.find('rank').text) |
| 230 | ... if rank > 50: |
| 231 | ... root.remove(country) |
| 232 | ... |
| 233 | >>> tree.write('output.xml') |
| 234 | |
| 235 | Our XML now looks like this: |
| 236 | |
| 237 | .. code-block:: xml |
| 238 | |
| 239 | <?xml version="1.0"?> |
| 240 | <data> |
| 241 | <country name="Liechtenstein"> |
| 242 | <rank updated="yes">2</rank> |
| 243 | <year>2008</year> |
| 244 | <gdppc>141100</gdppc> |
| 245 | <neighbor name="Austria" direction="E"/> |
| 246 | <neighbor name="Switzerland" direction="W"/> |
| 247 | </country> |
| 248 | <country name="Singapore"> |
| 249 | <rank updated="yes">5</rank> |
| 250 | <year>2011</year> |
| 251 | <gdppc>59900</gdppc> |
| 252 | <neighbor name="Malaysia" direction="N"/> |
| 253 | </country> |
| 254 | </data> |
| 255 | |
| 256 | Building XML documents |
| 257 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 258 | |
| 259 | The :func:`SubElement` function also provides a convenient way to create new |
| 260 | sub-elements for a given element:: |
| 261 | |
| 262 | >>> a = ET.Element('a') |
| 263 | >>> b = ET.SubElement(a, 'b') |
| 264 | >>> c = ET.SubElement(a, 'c') |
| 265 | >>> d = ET.SubElement(c, 'd') |
| 266 | >>> ET.dump(a) |
| 267 | <a><b /><c><d /></c></a> |
| 268 | |
| 269 | Additional resources |
| 270 | ^^^^^^^^^^^^^^^^^^^^ |
| 271 | |
| 272 | See http://effbot.org/zone/element-index.htm for tutorials and links to other |
| 273 | docs. |
| 274 | |
| 275 | .. _elementtree-xpath: |
| 276 | |
| 277 | XPath support |
| 278 | ------------- |
| 279 | |
| 280 | This module provides limited support for |
| 281 | `XPath expressions <http://www.w3.org/TR/xpath>`_ for locating elements in a |
| 282 | tree. The goal is to support a small subset of the abbreviated syntax; a full |
| 283 | XPath engine is outside the scope of the module. |
| 284 | |
| 285 | Example |
| 286 | ^^^^^^^ |
| 287 | |
| 288 | Here's an example that demonstrates some of the XPath capabilities of the |
| 289 | module. We'll be using the ``countrydata`` XML document from the |
| 290 | :ref:`Parsing XML <elementtree-parsing-xml>` section:: |
| 291 | |
| 292 | import xml.etree.ElementTree as ET |
| 293 | |
| 294 | root = ET.fromstring(countrydata) |
| 295 | |
| 296 | # Top-level elements |
| 297 | root.findall(".") |
| 298 | |
| 299 | # All 'neighbor' grand-children of 'country' children of the top-level |
| 300 | # elements |
| 301 | root.findall("./country/neighbor") |
| 302 | |
| 303 | # Nodes with name='Singapore' that have a 'year' child |
| 304 | root.findall(".//year/..[@name='Singapore']") |
| 305 | |
| 306 | # 'year' nodes that are children of nodes with name='Singapore' |
| 307 | root.findall(".//*[@name='Singapore']/year") |
| 308 | |
| 309 | # All 'neighbor' nodes that are the second child of their parent |
| 310 | root.findall(".//neighbor[2]") |
| 311 | |
| 312 | Supported XPath syntax |
| 313 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 314 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 315 | .. tabularcolumns:: |l|L| |
| 316 | |
Eli Bendersky | 6ee2187 | 2012-08-18 05:40:38 +0300 | [diff] [blame] | 317 | +-----------------------+------------------------------------------------------+ |
| 318 | | Syntax | Meaning | |
| 319 | +=======================+======================================================+ |
| 320 | | ``tag`` | Selects all child elements with the given tag. | |
| 321 | | | For example, ``spam`` selects all child elements | |
| 322 | | | named ``spam``, ``spam/egg`` selects all | |
| 323 | | | grandchildren named ``egg`` in all children named | |
| 324 | | | ``spam``. | |
| 325 | +-----------------------+------------------------------------------------------+ |
| 326 | | ``*`` | Selects all child elements. For example, ``*/egg`` | |
| 327 | | | selects all grandchildren named ``egg``. | |
| 328 | +-----------------------+------------------------------------------------------+ |
| 329 | | ``.`` | Selects the current node. This is mostly useful | |
| 330 | | | at the beginning of the path, to indicate that it's | |
| 331 | | | a relative path. | |
| 332 | +-----------------------+------------------------------------------------------+ |
| 333 | | ``//`` | Selects all subelements, on all levels beneath the | |
| 334 | | | current element. For example, ``.//egg`` selects | |
| 335 | | | all ``egg`` elements in the entire tree. | |
| 336 | +-----------------------+------------------------------------------------------+ |
| 337 | | ``..`` | Selects the parent element. | |
| 338 | +-----------------------+------------------------------------------------------+ |
| 339 | | ``[@attrib]`` | Selects all elements that have the given attribute. | |
| 340 | +-----------------------+------------------------------------------------------+ |
| 341 | | ``[@attrib='value']`` | Selects all elements for which the given attribute | |
| 342 | | | has the given value. The value cannot contain | |
| 343 | | | quotes. | |
| 344 | +-----------------------+------------------------------------------------------+ |
| 345 | | ``[tag]`` | Selects all elements that have a child named | |
| 346 | | | ``tag``. Only immediate children are supported. | |
| 347 | +-----------------------+------------------------------------------------------+ |
| 348 | | ``[position]`` | Selects all elements that are located at the given | |
| 349 | | | position. The position can be either an integer | |
| 350 | | | (1 is the first position), the expression ``last()`` | |
| 351 | | | (for the last position), or a position relative to | |
| 352 | | | the last position (e.g. ``last()-1``). | |
| 353 | +-----------------------+------------------------------------------------------+ |
| 354 | |
| 355 | Predicates (expressions within square brackets) must be preceded by a tag |
| 356 | name, an asterisk, or another predicate. ``position`` predicates must be |
| 357 | preceded by a tag name. |
| 358 | |
| 359 | Reference |
| 360 | --------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 361 | |
| 362 | .. _elementtree-functions: |
| 363 | |
| 364 | Functions |
Eli Bendersky | 6ee2187 | 2012-08-18 05:40:38 +0300 | [diff] [blame] | 365 | ^^^^^^^^^ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 366 | |
| 367 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 368 | .. function:: Comment(text=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 369 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 370 | Comment element factory. This factory function creates a special element |
| 371 | that will be serialized as an XML comment by the standard serializer. The |
| 372 | comment string can be either a bytestring or a Unicode string. *text* is a |
| 373 | string containing the comment string. Returns an element instance |
| 374 | representing a comment. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 375 | |
| 376 | |
| 377 | .. function:: dump(elem) |
| 378 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 379 | Writes an element tree or element structure to sys.stdout. This function |
| 380 | should be used for debugging only. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 381 | |
| 382 | The exact output format is implementation dependent. In this version, it's |
| 383 | written as an ordinary XML file. |
| 384 | |
| 385 | *elem* is an element tree or an individual element. |
| 386 | |
| 387 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 388 | .. function:: fromstring(text) |
| 389 | |
Florent Xicluna | 88db6f4 | 2010-03-14 01:22:09 +0000 | [diff] [blame] | 390 | Parses an XML section from a string constant. Same as :func:`XML`. *text* |
| 391 | is a string containing XML data. Returns an :class:`Element` instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 392 | |
| 393 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 394 | .. function:: fromstringlist(sequence, parser=None) |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 395 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 396 | Parses an XML document from a sequence of string fragments. *sequence* is a |
| 397 | list or other sequence containing XML data fragments. *parser* is an |
| 398 | optional parser instance. If not given, the standard :class:`XMLParser` |
| 399 | parser is used. Returns an :class:`Element` instance. |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 400 | |
| 401 | .. versionadded:: 2.7 |
| 402 | |
| 403 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 404 | .. function:: iselement(element) |
| 405 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 406 | Checks if an object appears to be a valid element object. *element* is an |
| 407 | element instance. Returns a true value if this is an element object. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 408 | |
| 409 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 410 | .. function:: iterparse(source, events=None, parser=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 411 | |
| 412 | Parses an XML section into an element tree incrementally, and reports what's |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 413 | going on to the user. *source* is a filename or file object containing XML |
| 414 | data. *events* is a list of events to report back. If omitted, only "end" |
| 415 | events are reported. *parser* is an optional parser instance. If not |
Eli Bendersky | f4fbf24 | 2013-01-24 07:28:33 -0800 | [diff] [blame] | 416 | given, the standard :class:`XMLParser` parser is used. *parser* is not |
| 417 | supported by ``cElementTree``. Returns an :term:`iterator` providing |
| 418 | ``(event, elem)`` pairs. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 419 | |
Georg Brandl | fb22263 | 2009-01-01 11:46:51 +0000 | [diff] [blame] | 420 | .. note:: |
| 421 | |
| 422 | :func:`iterparse` only guarantees that it has seen the ">" |
| 423 | character of a starting tag when it emits a "start" event, so the |
| 424 | attributes are defined, but the contents of the text and tail attributes |
| 425 | are undefined at that point. The same applies to the element children; |
| 426 | they may or may not be present. |
| 427 | |
| 428 | If you need a fully populated element, look for "end" events instead. |
| 429 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 430 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 431 | .. function:: parse(source, parser=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 432 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 433 | Parses an XML section into an element tree. *source* is a filename or file |
| 434 | object containing XML data. *parser* is an optional parser instance. If |
| 435 | not given, the standard :class:`XMLParser` parser is used. Returns an |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 436 | :class:`ElementTree` instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 437 | |
| 438 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 439 | .. function:: ProcessingInstruction(target, text=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 440 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 441 | PI element factory. This factory function creates a special element that |
| 442 | will be serialized as an XML processing instruction. *target* is a string |
| 443 | containing the PI target. *text* is a string containing the PI contents, if |
| 444 | given. Returns an element instance, representing a processing instruction. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 445 | |
| 446 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 447 | .. function:: register_namespace(prefix, uri) |
| 448 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 449 | Registers a namespace prefix. The registry is global, and any existing |
| 450 | mapping for either the given prefix or the namespace URI will be removed. |
| 451 | *prefix* is a namespace prefix. *uri* is a namespace uri. Tags and |
| 452 | attributes in this namespace will be serialized with the given prefix, if at |
| 453 | all possible. |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 454 | |
| 455 | .. versionadded:: 2.7 |
| 456 | |
| 457 | |
Florent Xicluna | 88db6f4 | 2010-03-14 01:22:09 +0000 | [diff] [blame] | 458 | .. function:: SubElement(parent, tag, attrib={}, **extra) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 459 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 460 | Subelement factory. This function creates an element instance, and appends |
| 461 | it to an existing element. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 462 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 463 | The element name, attribute names, and attribute values can be either |
| 464 | bytestrings or Unicode strings. *parent* is the parent element. *tag* is |
| 465 | the subelement name. *attrib* is an optional dictionary, containing element |
| 466 | attributes. *extra* contains additional attributes, given as keyword |
| 467 | arguments. Returns an element instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 468 | |
| 469 | |
Florent Xicluna | 88db6f4 | 2010-03-14 01:22:09 +0000 | [diff] [blame] | 470 | .. function:: tostring(element, encoding="us-ascii", method="xml") |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 471 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 472 | Generates a string representation of an XML element, including all |
Florent Xicluna | 88db6f4 | 2010-03-14 01:22:09 +0000 | [diff] [blame] | 473 | subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is |
| 474 | the output encoding (default is US-ASCII). *method* is either ``"xml"``, |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 475 | ``"html"`` or ``"text"`` (default is ``"xml"``). Returns an encoded string |
| 476 | containing the XML data. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 477 | |
| 478 | |
Florent Xicluna | 88db6f4 | 2010-03-14 01:22:09 +0000 | [diff] [blame] | 479 | .. function:: tostringlist(element, encoding="us-ascii", method="xml") |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 480 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 481 | Generates a string representation of an XML element, including all |
Florent Xicluna | 88db6f4 | 2010-03-14 01:22:09 +0000 | [diff] [blame] | 482 | subelements. *element* is an :class:`Element` instance. *encoding* [1]_ is |
| 483 | the output encoding (default is US-ASCII). *method* is either ``"xml"``, |
| 484 | ``"html"`` or ``"text"`` (default is ``"xml"``). Returns a list of encoded |
| 485 | strings containing the XML data. It does not guarantee any specific |
| 486 | sequence, except that ``"".join(tostringlist(element)) == |
| 487 | tostring(element)``. |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 488 | |
| 489 | .. versionadded:: 2.7 |
| 490 | |
| 491 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 492 | .. function:: XML(text, parser=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 493 | |
| 494 | Parses an XML section from a string constant. This function can be used to |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 495 | embed "XML literals" in Python code. *text* is a string containing XML |
| 496 | data. *parser* is an optional parser instance. If not given, the standard |
| 497 | :class:`XMLParser` parser is used. Returns an :class:`Element` instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 498 | |
| 499 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 500 | .. function:: XMLID(text, parser=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 501 | |
| 502 | Parses an XML section from a string constant, and also returns a dictionary |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 503 | which maps from element id:s to elements. *text* is a string containing XML |
| 504 | data. *parser* is an optional parser instance. If not given, the standard |
| 505 | :class:`XMLParser` parser is used. Returns a tuple containing an |
| 506 | :class:`Element` instance and a dictionary. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 507 | |
| 508 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 509 | .. _elementtree-element-objects: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 510 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 511 | Element Objects |
Eli Bendersky | 6ee2187 | 2012-08-18 05:40:38 +0300 | [diff] [blame] | 512 | ^^^^^^^^^^^^^^^ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 513 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 514 | .. class:: Element(tag, attrib={}, **extra) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 515 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 516 | Element class. This class defines the Element interface, and provides a |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 517 | reference implementation of this interface. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 518 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 519 | The element name, attribute names, and attribute values can be either |
| 520 | bytestrings or Unicode strings. *tag* is the element name. *attrib* is |
| 521 | an optional dictionary, containing element attributes. *extra* contains |
| 522 | additional attributes, given as keyword arguments. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 523 | |
| 524 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 525 | .. attribute:: tag |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 526 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 527 | A string identifying what kind of data this element represents (the |
| 528 | element type, in other words). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 529 | |
| 530 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 531 | .. attribute:: text |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 532 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 533 | The *text* attribute can be used to hold additional data associated with |
| 534 | the element. As the name implies this attribute is usually a string but |
| 535 | may be any application-specific object. If the element is created from |
| 536 | an XML file the attribute will contain any text found between the element |
| 537 | tags. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 538 | |
| 539 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 540 | .. attribute:: tail |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 541 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 542 | The *tail* attribute can be used to hold additional data associated with |
| 543 | the element. This attribute is usually a string but may be any |
| 544 | application-specific object. If the element is created from an XML file |
| 545 | the attribute will contain any text found after the element's end tag and |
| 546 | before the next tag. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 547 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 548 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 549 | .. attribute:: attrib |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 550 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 551 | A dictionary containing the element's attributes. Note that while the |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 552 | *attrib* value is always a real mutable Python dictionary, an ElementTree |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 553 | implementation may choose to use another internal representation, and |
| 554 | create the dictionary only if someone asks for it. To take advantage of |
| 555 | such implementations, use the dictionary methods below whenever possible. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 556 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 557 | The following dictionary-like methods work on the element attributes. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 558 | |
| 559 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 560 | .. method:: clear() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 561 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 562 | Resets an element. This function removes all subelements, clears all |
| 563 | attributes, and sets the text and tail attributes to None. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 564 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 565 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 566 | .. method:: get(key, default=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 567 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 568 | Gets the element attribute named *key*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 569 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 570 | Returns the attribute value, or *default* if the attribute was not found. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 571 | |
| 572 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 573 | .. method:: items() |
| 574 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 575 | Returns the element attributes as a sequence of (name, value) pairs. The |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 576 | attributes are returned in an arbitrary order. |
| 577 | |
| 578 | |
| 579 | .. method:: keys() |
| 580 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 581 | Returns the elements attribute names as a list. The names are returned |
| 582 | in an arbitrary order. |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 583 | |
| 584 | |
| 585 | .. method:: set(key, value) |
| 586 | |
| 587 | Set the attribute *key* on the element to *value*. |
| 588 | |
| 589 | The following methods work on the element's children (subelements). |
| 590 | |
| 591 | |
| 592 | .. method:: append(subelement) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 593 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 594 | Adds the element *subelement* to the end of this elements internal list |
| 595 | of subelements. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 596 | |
| 597 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 598 | .. method:: extend(subelements) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 599 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 600 | Appends *subelements* from a sequence object with zero or more elements. |
| 601 | Raises :exc:`AssertionError` if a subelement is not a valid object. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 602 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 603 | .. versionadded:: 2.7 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 604 | |
| 605 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 606 | .. method:: find(match) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 607 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 608 | Finds the first subelement matching *match*. *match* may be a tag name |
| 609 | or path. Returns an element instance or ``None``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 610 | |
| 611 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 612 | .. method:: findall(match) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 613 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 614 | Finds all matching subelements, by tag name or path. Returns a list |
| 615 | containing all matching elements in document order. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 616 | |
| 617 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 618 | .. method:: findtext(match, default=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 619 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 620 | Finds text for the first subelement matching *match*. *match* may be |
| 621 | a tag name or path. Returns the text content of the first matching |
| 622 | element, or *default* if no element was found. Note that if the matching |
| 623 | element has no text content an empty string is returned. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 624 | |
| 625 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 626 | .. method:: getchildren() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 627 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 628 | .. deprecated:: 2.7 |
| 629 | Use ``list(elem)`` or iteration. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 630 | |
| 631 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 632 | .. method:: getiterator(tag=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 633 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 634 | .. deprecated:: 2.7 |
| 635 | Use method :meth:`Element.iter` instead. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 636 | |
| 637 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 638 | .. method:: insert(index, element) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 639 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 640 | Inserts a subelement at the given position in this element. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 641 | |
| 642 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 643 | .. method:: iter(tag=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 644 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 645 | Creates a tree :term:`iterator` with the current element as the root. |
| 646 | The iterator iterates over this element and all elements below it, in |
| 647 | document (depth first) order. If *tag* is not ``None`` or ``'*'``, only |
| 648 | elements whose tag equals *tag* are returned from the iterator. If the |
| 649 | tree structure is modified during iteration, the result is undefined. |
| 650 | |
Ezio Melotti | c54d97b | 2011-10-09 23:56:51 +0300 | [diff] [blame] | 651 | .. versionadded:: 2.7 |
| 652 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 653 | |
| 654 | .. method:: iterfind(match) |
| 655 | |
| 656 | Finds all matching subelements, by tag name or path. Returns an iterable |
| 657 | yielding all matching elements in document order. |
| 658 | |
| 659 | .. versionadded:: 2.7 |
| 660 | |
| 661 | |
| 662 | .. method:: itertext() |
| 663 | |
| 664 | Creates a text iterator. The iterator loops over this element and all |
| 665 | subelements, in document order, and returns all inner text. |
| 666 | |
| 667 | .. versionadded:: 2.7 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 668 | |
| 669 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 670 | .. method:: makeelement(tag, attrib) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 671 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 672 | Creates a new element object of the same type as this element. Do not |
| 673 | call this method, use the :func:`SubElement` factory function instead. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 674 | |
| 675 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 676 | .. method:: remove(subelement) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 677 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 678 | Removes *subelement* from the element. Unlike the find\* methods this |
| 679 | method compares elements based on the instance identity, not on tag value |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 680 | or contents. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 681 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 682 | :class:`Element` objects also support the following sequence type methods |
Serhiy Storchaka | 7653e26 | 2013-08-29 10:34:23 +0300 | [diff] [blame] | 683 | for working with subelements: :meth:`~object.__delitem__`, |
| 684 | :meth:`~object.__getitem__`, :meth:`~object.__setitem__`, |
| 685 | :meth:`~object.__len__`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 686 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 687 | Caution: Elements with no subelements will test as ``False``. This behavior |
| 688 | will change in future versions. Use specific ``len(elem)`` or ``elem is |
| 689 | None`` test instead. :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 690 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 691 | element = root.find('foo') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 692 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 693 | if not element: # careful! |
| 694 | print "element not found, or element has no subelements" |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 695 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 696 | if element is None: |
| 697 | print "element not found" |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 698 | |
| 699 | |
| 700 | .. _elementtree-elementtree-objects: |
| 701 | |
| 702 | ElementTree Objects |
Eli Bendersky | 6ee2187 | 2012-08-18 05:40:38 +0300 | [diff] [blame] | 703 | ^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 704 | |
| 705 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 706 | .. class:: ElementTree(element=None, file=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 707 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 708 | ElementTree wrapper class. This class represents an entire element |
| 709 | hierarchy, and adds some extra support for serialization to and from |
| 710 | standard XML. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 711 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 712 | *element* is the root element. The tree is initialized with the contents |
| 713 | of the XML *file* if given. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 714 | |
| 715 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 716 | .. method:: _setroot(element) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 717 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 718 | Replaces the root element for this tree. This discards the current |
| 719 | contents of the tree, and replaces it with the given element. Use with |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 720 | care. *element* is an element instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 721 | |
| 722 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 723 | .. method:: find(match) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 724 | |
Eli Bendersky | 981c3bd | 2013-03-12 06:08:04 -0700 | [diff] [blame] | 725 | Same as :meth:`Element.find`, starting at the root of the tree. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 726 | |
| 727 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 728 | .. method:: findall(match) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 729 | |
Eli Bendersky | 981c3bd | 2013-03-12 06:08:04 -0700 | [diff] [blame] | 730 | Same as :meth:`Element.findall`, starting at the root of the tree. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 731 | |
| 732 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 733 | .. method:: findtext(match, default=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 734 | |
Eli Bendersky | 981c3bd | 2013-03-12 06:08:04 -0700 | [diff] [blame] | 735 | Same as :meth:`Element.findtext`, starting at the root of the tree. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 736 | |
| 737 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 738 | .. method:: getiterator(tag=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 739 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 740 | .. deprecated:: 2.7 |
| 741 | Use method :meth:`ElementTree.iter` instead. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 742 | |
| 743 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 744 | .. method:: getroot() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 745 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 746 | Returns the root element for this tree. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 747 | |
| 748 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 749 | .. method:: iter(tag=None) |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 750 | |
| 751 | Creates and returns a tree iterator for the root element. The iterator |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 752 | loops over all elements in this tree, in section order. *tag* is the tag |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 753 | to look for (default is to return all elements) |
| 754 | |
| 755 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 756 | .. method:: iterfind(match) |
| 757 | |
| 758 | Finds all matching subelements, by tag name or path. Same as |
| 759 | getroot().iterfind(match). Returns an iterable yielding all matching |
| 760 | elements in document order. |
| 761 | |
| 762 | .. versionadded:: 2.7 |
| 763 | |
| 764 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 765 | .. method:: parse(source, parser=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 766 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 767 | Loads an external XML section into this element tree. *source* is a file |
| 768 | name or file object. *parser* is an optional parser instance. If not |
| 769 | given, the standard XMLParser parser is used. Returns the section |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 770 | root element. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 771 | |
| 772 | |
Serhiy Storchaka | 3d4a02a | 2013-01-13 21:57:14 +0200 | [diff] [blame] | 773 | .. method:: write(file, encoding="us-ascii", xml_declaration=None, \ |
| 774 | default_namespace=None, method="xml") |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 775 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 776 | Writes the element tree to a file, as XML. *file* is a file name, or a |
| 777 | file object opened for writing. *encoding* [1]_ is the output encoding |
| 778 | (default is US-ASCII). *xml_declaration* controls if an XML declaration |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 779 | should be added to the file. Use False for never, True for always, None |
Serhiy Storchaka | 3d4a02a | 2013-01-13 21:57:14 +0200 | [diff] [blame] | 780 | for only if not US-ASCII or UTF-8 (default is None). *default_namespace* |
| 781 | sets the default XML namespace (for "xmlns"). *method* is either |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 782 | ``"xml"``, ``"html"`` or ``"text"`` (default is ``"xml"``). Returns an |
| 783 | encoded string. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 784 | |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 785 | This is the XML file that is going to be manipulated:: |
| 786 | |
| 787 | <html> |
| 788 | <head> |
| 789 | <title>Example page</title> |
| 790 | </head> |
| 791 | <body> |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 792 | <p>Moved to <a href="http://example.org/">example.org</a> |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 793 | or <a href="http://example.com/">example.com</a>.</p> |
| 794 | </body> |
| 795 | </html> |
| 796 | |
| 797 | Example of changing the attribute "target" of every link in first paragraph:: |
| 798 | |
| 799 | >>> from xml.etree.ElementTree import ElementTree |
| 800 | >>> tree = ElementTree() |
| 801 | >>> tree.parse("index.xhtml") |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 802 | <Element 'html' at 0xb77e6fac> |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 803 | >>> p = tree.find("body/p") # Finds first occurrence of tag p in body |
| 804 | >>> p |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 805 | <Element 'p' at 0xb77ec26c> |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 806 | >>> links = list(p.iter("a")) # Returns list of all links |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 807 | >>> links |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 808 | [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>] |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 809 | >>> for i in links: # Iterates through all found links |
| 810 | ... i.attrib["target"] = "blank" |
| 811 | >>> tree.write("output.xhtml") |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 812 | |
| 813 | .. _elementtree-qname-objects: |
| 814 | |
| 815 | QName Objects |
Eli Bendersky | 6ee2187 | 2012-08-18 05:40:38 +0300 | [diff] [blame] | 816 | ^^^^^^^^^^^^^ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 817 | |
| 818 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 819 | .. class:: QName(text_or_uri, tag=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 820 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 821 | QName wrapper. This can be used to wrap a QName attribute value, in order |
| 822 | to get proper namespace handling on output. *text_or_uri* is a string |
| 823 | containing the QName value, in the form {uri}local, or, if the tag argument |
| 824 | is given, the URI part of a QName. If *tag* is given, the first argument is |
| 825 | interpreted as an URI, and this argument is interpreted as a local name. |
| 826 | :class:`QName` instances are opaque. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 827 | |
| 828 | |
| 829 | .. _elementtree-treebuilder-objects: |
| 830 | |
| 831 | TreeBuilder Objects |
Eli Bendersky | 6ee2187 | 2012-08-18 05:40:38 +0300 | [diff] [blame] | 832 | ^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 833 | |
| 834 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 835 | .. class:: TreeBuilder(element_factory=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 836 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 837 | Generic element structure builder. This builder converts a sequence of |
| 838 | start, data, and end method calls to a well-formed element structure. You |
| 839 | can use this class to build an element structure using a custom XML parser, |
| 840 | or a parser for some other XML-like format. The *element_factory* is called |
| 841 | to create new :class:`Element` instances when given. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 842 | |
| 843 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 844 | .. method:: close() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 845 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 846 | Flushes the builder buffers, and returns the toplevel document |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 847 | element. Returns an :class:`Element` instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 848 | |
| 849 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 850 | .. method:: data(data) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 851 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 852 | Adds text to the current element. *data* is a string. This should be |
| 853 | either a bytestring, or a Unicode string. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 854 | |
| 855 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 856 | .. method:: end(tag) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 857 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 858 | Closes the current element. *tag* is the element name. Returns the |
| 859 | closed element. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 860 | |
| 861 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 862 | .. method:: start(tag, attrs) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 863 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 864 | Opens a new element. *tag* is the element name. *attrs* is a dictionary |
| 865 | containing element attributes. Returns the opened element. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 866 | |
| 867 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 868 | In addition, a custom :class:`TreeBuilder` object can provide the |
| 869 | following method: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 870 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 871 | .. method:: doctype(name, pubid, system) |
| 872 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 873 | Handles a doctype declaration. *name* is the doctype name. *pubid* is |
| 874 | the public identifier. *system* is the system identifier. This method |
| 875 | does not exist on the default :class:`TreeBuilder` class. |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 876 | |
| 877 | .. versionadded:: 2.7 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 878 | |
| 879 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 880 | .. _elementtree-xmlparser-objects: |
| 881 | |
| 882 | XMLParser Objects |
Eli Bendersky | 6ee2187 | 2012-08-18 05:40:38 +0300 | [diff] [blame] | 883 | ^^^^^^^^^^^^^^^^^ |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 884 | |
| 885 | |
Florent Xicluna | a231e45 | 2010-03-13 20:30:15 +0000 | [diff] [blame] | 886 | .. class:: XMLParser(html=0, target=None, encoding=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 887 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 888 | :class:`Element` structure builder for XML source data, based on the expat |
| 889 | parser. *html* are predefined HTML entities. This flag is not supported by |
| 890 | the current implementation. *target* is the target object. If omitted, the |
| 891 | builder uses an instance of the standard TreeBuilder class. *encoding* [1]_ |
| 892 | is optional. If given, the value overrides the encoding specified in the |
| 893 | XML file. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 894 | |
| 895 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 896 | .. method:: close() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 897 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 898 | Finishes feeding data to the parser. Returns an element structure. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 899 | |
| 900 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 901 | .. method:: doctype(name, pubid, system) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 902 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 903 | .. deprecated:: 2.7 |
| 904 | Define the :meth:`TreeBuilder.doctype` method on a custom TreeBuilder |
| 905 | target. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 906 | |
| 907 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 908 | .. method:: feed(data) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 909 | |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 910 | Feeds data to the parser. *data* is encoded data. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 911 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 912 | :meth:`XMLParser.feed` calls *target*\'s :meth:`start` method |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 913 | for each opening tag, its :meth:`end` method for each closing tag, |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 914 | and data is processed by method :meth:`data`. :meth:`XMLParser.close` |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 915 | calls *target*\'s method :meth:`close`. |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 916 | :class:`XMLParser` can be used not only for building a tree structure. |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 917 | This is an example of counting the maximum depth of an XML file:: |
| 918 | |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 919 | >>> from xml.etree.ElementTree import XMLParser |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 920 | >>> class MaxDepth: # The target object of the parser |
| 921 | ... maxDepth = 0 |
| 922 | ... depth = 0 |
| 923 | ... def start(self, tag, attrib): # Called for each opening tag. |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 924 | ... self.depth += 1 |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 925 | ... if self.depth > self.maxDepth: |
| 926 | ... self.maxDepth = self.depth |
| 927 | ... def end(self, tag): # Called for each closing tag. |
| 928 | ... self.depth -= 1 |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 929 | ... def data(self, data): |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 930 | ... pass # We do not need to do anything with data. |
| 931 | ... def close(self): # Called when all data has been parsed. |
| 932 | ... return self.maxDepth |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 933 | ... |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 934 | >>> target = MaxDepth() |
Florent Xicluna | 3e8c189 | 2010-03-11 14:36:19 +0000 | [diff] [blame] | 935 | >>> parser = XMLParser(target=target) |
Georg Brandl | 39bd059 | 2007-12-01 22:42:46 +0000 | [diff] [blame] | 936 | >>> exampleXml = """ |
| 937 | ... <a> |
| 938 | ... <b> |
| 939 | ... </b> |
| 940 | ... <b> |
| 941 | ... <c> |
| 942 | ... <d> |
| 943 | ... </d> |
| 944 | ... </c> |
| 945 | ... </b> |
| 946 | ... </a>""" |
| 947 | >>> parser.feed(exampleXml) |
| 948 | >>> parser.close() |
| 949 | 4 |
Mark Summerfield | 43da35d | 2008-03-17 08:28:15 +0000 | [diff] [blame] | 950 | |
| 951 | |
| 952 | .. rubric:: Footnotes |
| 953 | |
| 954 | .. [#] The encoding string included in XML output should conform to the |
Florent Xicluna | 583302c | 2010-03-13 17:56:19 +0000 | [diff] [blame] | 955 | appropriate standards. For example, "UTF-8" is valid, but "UTF8" is |
| 956 | not. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl |
Georg Brandl | 8b8c2df | 2009-02-20 08:45:47 +0000 | [diff] [blame] | 957 | and http://www.iana.org/assignments/character-sets. |