Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1 | \section{\module{xml.etree.ElementTree} --- The ElementTree XML API} |
| 2 | \declaremodule{standard}{xml.etree.ElementTree} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3 | \moduleauthor{Fredrik Lundh}{fredrik@pythonware.com} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4 | \modulesynopsis{Implementation of the ElementTree API.} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6 | \versionadded{2.5} |
| 7 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8 | The Element type is a flexible container object, designed to store |
| 9 | hierarchical data structures in memory. The type can be described as a |
| 10 | cross between a list and a dictionary. |
| 11 | |
| 12 | Each element has a number of properties associated with it: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14 | \begin{itemize} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 15 | \item a tag which is a string identifying what kind of data |
| 16 | this element represents (the element type, in other words). |
| 17 | \item a number of attributes, stored in a Python dictionary. |
| 18 | \item a text string. |
| 19 | \item an optional tail string. |
| 20 | \item a number of child elements, stored in a Python sequence |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | \end{itemize} |
| 22 | |
| 23 | To create an element instance, use the Element or SubElement factory |
| 24 | functions. |
| 25 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 26 | The \class{ElementTree} class can be used to wrap an element |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | structure, and convert it from and to XML. |
| 28 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 29 | A C implementation of this API is available as |
| 30 | \module{xml.etree.cElementTree}. |
| 31 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 32 | |
| 33 | \subsection{Functions\label{elementtree-functions}} |
| 34 | |
| 35 | \begin{funcdesc}{Comment}{\optional{text}} |
| 36 | Comment element factory. This factory function creates a special |
| 37 | element that will be serialized as an XML comment. |
| 38 | The comment string can be either an 8-bit ASCII string or a Unicode |
| 39 | string. |
| 40 | \var{text} is a string containing the comment string. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 41 | Returns an element instance representing a comment. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
| 44 | \begin{funcdesc}{dump}{elem} |
| 45 | Writes an element tree or element structure to sys.stdout. This |
| 46 | function should be used for debugging only. |
| 47 | |
| 48 | The exact output format is implementation dependent. In this |
| 49 | version, it's written as an ordinary XML file. |
| 50 | |
| 51 | \var{elem} is an element tree or an individual element. |
| 52 | \end{funcdesc} |
| 53 | |
| 54 | \begin{funcdesc}{Element}{tag\optional{, attrib}\optional{, **extra}} |
| 55 | Element factory. This function returns an object implementing the |
| 56 | standard Element interface. The exact class or type of that object |
| 57 | is implementation dependent, but it will always be compatible with |
| 58 | the {\_}ElementInterface class in this module. |
| 59 | |
| 60 | The element name, attribute names, and attribute values can be |
| 61 | either 8-bit ASCII strings or Unicode strings. |
| 62 | \var{tag} is the element name. |
| 63 | \var{attrib} is an optional dictionary, containing element attributes. |
| 64 | \var{extra} contains additional attributes, given as keyword arguments. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 65 | Returns an element instance. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | \end{funcdesc} |
| 67 | |
| 68 | \begin{funcdesc}{fromstring}{text} |
| 69 | Parses an XML section from a string constant. Same as XML. |
| 70 | \var{text} is a string containing XML data. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 71 | Returns an Element instance. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | \end{funcdesc} |
| 73 | |
| 74 | \begin{funcdesc}{iselement}{element} |
| 75 | Checks if an object appears to be a valid element object. |
| 76 | \var{element} is an element instance. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 77 | Returns a true value if this is an element object. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | \end{funcdesc} |
| 79 | |
| 80 | \begin{funcdesc}{iterparse}{source\optional{, events}} |
| 81 | Parses an XML section into an element tree incrementally, and reports |
| 82 | what's going on to the user. |
| 83 | \var{source} is a filename or file object containing XML data. |
| 84 | \var{events} is a list of events to report back. If omitted, only ``end'' |
| 85 | events are reported. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 86 | Returns an iterator providing \code{(\var{event}, \var{elem})} pairs. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 87 | \end{funcdesc} |
| 88 | |
| 89 | \begin{funcdesc}{parse}{source\optional{, parser}} |
| 90 | Parses an XML section into an element tree. |
| 91 | \var{source} is a filename or file object containing XML data. |
| 92 | \var{parser} is an optional parser instance. If not given, the |
| 93 | standard XMLTreeBuilder parser is used. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 94 | Returns an ElementTree instance. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 95 | \end{funcdesc} |
| 96 | |
| 97 | \begin{funcdesc}{ProcessingInstruction}{target\optional{, text}} |
| 98 | PI element factory. This factory function creates a special element |
| 99 | that will be serialized as an XML processing instruction. |
| 100 | \var{target} is a string containing the PI target. |
| 101 | \var{text} is a string containing the PI contents, if given. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 102 | Returns an element instance, representing a processing instruction. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 103 | \end{funcdesc} |
| 104 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 105 | \begin{funcdesc}{SubElement}{parent, tag\optional{, |
| 106 | attrib\optional{, **extra}}} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 107 | Subelement factory. This function creates an element instance, and |
| 108 | appends it to an existing element. |
| 109 | |
| 110 | The element name, attribute names, and attribute values can be |
| 111 | either 8-bit ASCII strings or Unicode strings. |
| 112 | \var{parent} is the parent element. |
| 113 | \var{tag} is the subelement name. |
| 114 | \var{attrib} is an optional dictionary, containing element attributes. |
| 115 | \var{extra} contains additional attributes, given as keyword arguments. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 116 | Returns an element instance. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 117 | \end{funcdesc} |
| 118 | |
| 119 | \begin{funcdesc}{tostring}{element\optional{, encoding}} |
| 120 | Generates a string representation of an XML element, including all |
| 121 | subelements. |
| 122 | \var{element} is an Element instance. |
| 123 | \var{encoding} is the output encoding (default is US-ASCII). |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 124 | Returns an encoded string containing the XML data. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 125 | \end{funcdesc} |
| 126 | |
| 127 | \begin{funcdesc}{XML}{text} |
| 128 | Parses an XML section from a string constant. This function can |
| 129 | be used to embed ``XML literals'' in Python code. |
| 130 | \var{text} is a string containing XML data. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 131 | Returns an Element instance. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 132 | \end{funcdesc} |
| 133 | |
| 134 | \begin{funcdesc}{XMLID}{text} |
| 135 | Parses an XML section from a string constant, and also returns |
| 136 | a dictionary which maps from element id:s to elements. |
| 137 | \var{text} is a string containing XML data. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 138 | Returns a tuple containing an Element instance and a dictionary. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 139 | \end{funcdesc} |
| 140 | |
| 141 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 142 | \subsection{The Element Interface\label{elementtree-element-interface}} |
| 143 | |
| 144 | Element objects returned by Element or SubElement have the |
| 145 | following methods and attributes. |
| 146 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 147 | \begin{memberdesc}[Element]{tag} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 148 | A string identifying what kind of data this element represents |
| 149 | (the element type, in other words). |
| 150 | \end{memberdesc} |
| 151 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 152 | \begin{memberdesc}[Element]{text} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 153 | The \var{text} attribute can be used to hold additional data |
| 154 | associated with the element. |
| 155 | As the name implies this attribute is usually a string but may be any |
| 156 | application-specific object. |
| 157 | If the element is created from an XML file the attribute will contain |
| 158 | any text found between the element tags. |
| 159 | \end{memberdesc} |
| 160 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 161 | \begin{memberdesc}[Element]{tail} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 162 | The \var{tail} attribute can be used to hold additional data |
| 163 | associated with the element. |
| 164 | This attribute is usually a string but may be any application-specific object. |
| 165 | If the element is created from an XML file the attribute will contain |
| 166 | any text found after the element's end tag and before the next tag. |
| 167 | \end{memberdesc} |
| 168 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 169 | \begin{memberdesc}[Element]{attrib} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 170 | A dictionary containing the element's attributes. |
| 171 | Note that while the \var{attrib} value is always a real mutable Python |
| 172 | dictionary, an ElementTree implementation may choose to use another |
| 173 | internal representation, and create the dictionary only if someone |
| 174 | asks for it. To take advantage of such implementations, use the |
| 175 | dictionary methods below whenever possible. |
| 176 | \end{memberdesc} |
| 177 | |
| 178 | The following dictionary-like methods work on the element attributes. |
| 179 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 180 | \begin{methoddesc}[Element]{clear}{} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 181 | Resets an element. This function removes all subelements, clears |
| 182 | all attributes, and sets the text and tail attributes to None. |
| 183 | \end{methoddesc} |
| 184 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 185 | \begin{methoddesc}[Element]{get}{key\optional{, default=None}} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 186 | Gets the element attribute named \var{key}. |
| 187 | |
| 188 | Returns the attribute value, or \var{default} if the |
| 189 | attribute was not found. |
| 190 | \end{methoddesc} |
| 191 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 192 | \begin{methoddesc}[Element]{items}{} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 193 | Returns the element attributes as a sequence of (name, value) pairs. |
| 194 | The attributes are returned in an arbitrary order. |
| 195 | \end{methoddesc} |
| 196 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 197 | \begin{methoddesc}[Element]{keys}{} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 198 | Returns the elements attribute names as a list. |
| 199 | The names are returned in an arbitrary order. |
| 200 | \end{methoddesc} |
| 201 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 202 | \begin{methoddesc}[Element]{set}{key, value} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 203 | Set the attribute \var{key} on the element to \var{value}. |
| 204 | \end{methoddesc} |
| 205 | |
| 206 | The following methods work on the element's children (subelements). |
| 207 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 208 | \begin{methoddesc}[Element]{append}{subelement} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 209 | Adds the element \var{subelement} to the end of this elements internal list |
| 210 | of subelements. |
| 211 | \end{methoddesc} |
| 212 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 213 | \begin{methoddesc}[Element]{find}{match} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 214 | Finds the first subelement matching \var{match}. |
| 215 | \var{match} may be a tag name or path. |
| 216 | Returns an element instance or \code{None}. |
| 217 | \end{methoddesc} |
| 218 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 219 | \begin{methoddesc}[Element]{findall}{match} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 220 | Finds all subelements matching \var{match}. |
| 221 | \var{match} may be a tag name or path. |
| 222 | Returns an iterable yielding all matching elements in document order. |
| 223 | \end{methoddesc} |
| 224 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 225 | \begin{methoddesc}[Element]{findtext}{condition\optional{, default=None}} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 226 | Finds text for the first subelement matching \var{condition}. |
| 227 | \var{condition} may be a tag name or path. |
| 228 | Returns the text content of the first matching element, or |
| 229 | \var{default} if no element was found. Note that if the |
| 230 | matching element has no text content an empty string is returned. |
| 231 | \end{methoddesc} |
| 232 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 233 | \begin{methoddesc}[Element]{getchildren}{} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 234 | Returns all subelements. The elements are returned in document order. |
| 235 | \end{methoddesc} |
| 236 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 237 | \begin{methoddesc}[Element]{getiterator}{\optional{tag=None}} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 238 | Creates a tree iterator with the current element as the root. |
| 239 | The iterator iterates over this element and all elements below it |
| 240 | that match the given tag. If tag |
| 241 | is \code{None} or \code{'*'} then all elements are iterated over. |
| 242 | Returns an iterable that provides element objects in document (depth first) |
| 243 | order. |
| 244 | \end{methoddesc} |
| 245 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 246 | \begin{methoddesc}[Element]{insert}{index, element} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 247 | Inserts a subelement at the given position in this element. |
| 248 | \end{methoddesc} |
| 249 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 250 | \begin{methoddesc}[Element]{makeelement}{tag, attrib} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 251 | Creates a new element object of the same type as this element. |
| 252 | Do not call this method, use the SubElement factory function instead. |
| 253 | \end{methoddesc} |
| 254 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 255 | \begin{methoddesc}[Element]{remove}{subelement} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 256 | Removes \var{subelement} from the element. |
| 257 | Unlike the findXXX methods this method compares elements based on |
| 258 | the instance identity, not on tag value or contents. |
| 259 | \end{methoddesc} |
| 260 | |
| 261 | Element objects also support the following sequence type methods for |
| 262 | working with subelements: \method{__delitem__()}, |
| 263 | \method{__getitem__()}, \method{__setitem__()}, \method{__len__()}. |
| 264 | |
| 265 | Caution: Because Element objects do not define a |
| 266 | \method{__nonzero__()} method, elements with no subelements will test |
| 267 | as \code{False}. |
| 268 | |
| 269 | \begin{verbatim} |
| 270 | element = root.find('foo') |
| 271 | |
| 272 | if not element: # careful! |
| 273 | print "element not found, or element has no subelements" |
| 274 | |
| 275 | if element is None: |
| 276 | print "element not found" |
| 277 | \end{verbatim} |
| 278 | |
| 279 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 280 | \subsection{ElementTree Objects\label{elementtree-elementtree-objects}} |
| 281 | |
| 282 | \begin{classdesc}{ElementTree}{\optional{element,} \optional{file}} |
| 283 | ElementTree wrapper class. This class represents an entire element |
| 284 | hierarchy, and adds some extra support for serialization to and from |
| 285 | standard XML. |
| 286 | |
| 287 | \var{element} is the root element. |
| 288 | The tree is initialized with the contents of the XML \var{file} if given. |
| 289 | \end{classdesc} |
| 290 | |
| 291 | \begin{methoddesc}{_setroot}{element} |
| 292 | Replaces the root element for this tree. This discards the |
| 293 | current contents of the tree, and replaces it with the given |
| 294 | element. Use with care. |
| 295 | \var{element} is an element instance. |
| 296 | \end{methoddesc} |
| 297 | |
| 298 | \begin{methoddesc}{find}{path} |
| 299 | Finds the first toplevel element with given tag. |
| 300 | Same as getroot().find(path). |
| 301 | \var{path} is the element to look for. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 302 | Returns the first matching element, or \code{None} if no element was found. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 303 | \end{methoddesc} |
| 304 | |
| 305 | \begin{methoddesc}{findall}{path} |
| 306 | Finds all toplevel elements with the given tag. |
| 307 | Same as getroot().findall(path). |
| 308 | \var{path} is the element to look for. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 309 | Returns a list or iterator containing all matching elements, |
| 310 | in document order. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 311 | \end{methoddesc} |
| 312 | |
| 313 | \begin{methoddesc}{findtext}{path\optional{, default}} |
| 314 | Finds the element text for the first toplevel element with given |
| 315 | tag. Same as getroot().findtext(path). |
| 316 | \var{path} is the toplevel element to look for. |
| 317 | \var{default} is the value to return if the element was not found. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 318 | Returns the text content of the first matching element, or the |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 319 | default value no element was found. Note that if the element |
| 320 | has is found, but has no text content, this method returns an |
| 321 | empty string. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 322 | \end{methoddesc} |
| 323 | |
| 324 | \begin{methoddesc}{getiterator}{\optional{tag}} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 325 | Creates and returns a tree iterator for the root element. The iterator loops |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 326 | over all elements in this tree, in section order. |
| 327 | \var{tag} is the tag to look for (default is to return all elements) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 328 | \end{methoddesc} |
| 329 | |
| 330 | \begin{methoddesc}{getroot}{} |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 331 | Returns the root element for this tree. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 332 | \end{methoddesc} |
| 333 | |
| 334 | \begin{methoddesc}{parse}{source\optional{, parser}} |
| 335 | Loads an external XML section into this element tree. |
| 336 | \var{source} is a file name or file object. |
| 337 | \var{parser} is an optional parser instance. If not given, the |
| 338 | standard XMLTreeBuilder parser is used. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 339 | Returns the section root element. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 340 | \end{methoddesc} |
| 341 | |
| 342 | \begin{methoddesc}{write}{file\optional{, encoding}} |
| 343 | Writes the element tree to a file, as XML. |
| 344 | \var{file} is a file name, or a file object opened for writing. |
| 345 | \var{encoding} is the output encoding (default is US-ASCII). |
| 346 | \end{methoddesc} |
| 347 | |
| 348 | |
| 349 | \subsection{QName Objects\label{elementtree-qname-objects}} |
| 350 | |
| 351 | \begin{classdesc}{QName}{text_or_uri\optional{, tag}} |
| 352 | QName wrapper. This can be used to wrap a QName attribute value, in |
| 353 | order to get proper namespace handling on output. |
| 354 | \var{text_or_uri} is a string containing the QName value, |
| 355 | in the form {\{}uri{\}}local, or, if the tag argument is given, |
| 356 | the URI part of a QName. |
| 357 | If \var{tag} is given, the first argument is interpreted as |
| 358 | an URI, and this argument is interpreted as a local name. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 359 | \class{QName} instances are opaque. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 360 | \end{classdesc} |
| 361 | |
| 362 | |
| 363 | \subsection{TreeBuilder Objects\label{elementtree-treebuilder-objects}} |
| 364 | |
| 365 | \begin{classdesc}{TreeBuilder}{\optional{element_factory}} |
| 366 | Generic element structure builder. This builder converts a sequence |
| 367 | of start, data, and end method calls to a well-formed element structure. |
| 368 | You can use this class to build an element structure using a custom XML |
| 369 | parser, or a parser for some other XML-like format. |
| 370 | The \var{element_factory} is called to create new Element instances when |
| 371 | given. |
| 372 | \end{classdesc} |
| 373 | |
| 374 | \begin{methoddesc}{close}{} |
| 375 | Flushes the parser buffers, and returns the toplevel documen |
| 376 | element. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 377 | Returns an Element instance. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 378 | \end{methoddesc} |
| 379 | |
| 380 | \begin{methoddesc}{data}{data} |
| 381 | Adds text to the current element. |
| 382 | \var{data} is a string. This should be either an 8-bit string |
| 383 | containing ASCII text, or a Unicode string. |
| 384 | \end{methoddesc} |
| 385 | |
| 386 | \begin{methoddesc}{end}{tag} |
| 387 | Closes the current element. |
| 388 | \var{tag} is the element name. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 389 | Returns the closed element. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 390 | \end{methoddesc} |
| 391 | |
| 392 | \begin{methoddesc}{start}{tag, attrs} |
| 393 | Opens a new element. |
| 394 | \var{tag} is the element name. |
| 395 | \var{attrs} is a dictionary containing element attributes. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 396 | Returns the opened element. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 397 | \end{methoddesc} |
| 398 | |
| 399 | |
| 400 | \subsection{XMLTreeBuilder Objects\label{elementtree-xmltreebuilder-objects}} |
| 401 | |
| 402 | \begin{classdesc}{XMLTreeBuilder}{\optional{html,} \optional{target}} |
| 403 | Element structure builder for XML source data, based on the |
| 404 | expat parser. |
| 405 | \var{html} are predefined HTML entities. This flag is not supported |
| 406 | by the current implementation. |
| 407 | \var{target} is the target object. If omitted, the builder uses an |
| 408 | instance of the standard TreeBuilder class. |
| 409 | \end{classdesc} |
| 410 | |
| 411 | \begin{methoddesc}{close}{} |
| 412 | Finishes feeding data to the parser. |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 413 | Returns an element structure. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 414 | \end{methoddesc} |
| 415 | |
| 416 | \begin{methoddesc}{doctype}{name, pubid, system} |
| 417 | Handles a doctype declaration. |
| 418 | \var{name} is the doctype name. |
| 419 | \var{pubid} is the public identifier. |
| 420 | \var{system} is the system identifier. |
| 421 | \end{methoddesc} |
| 422 | |
| 423 | \begin{methoddesc}{feed}{data} |
| 424 | Feeds data to the parser. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 425 | \var{data} is encoded data. |
| 426 | \end{methoddesc} |