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