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