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