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