Fred Drake | a58f123 | 2006-09-15 15:18:04 +0000 | [diff] [blame] | 1 | \section{\module{xml.etree.ElementTree} --- The ElementTree XML API} |
| 2 | \declaremodule{standard}{xml.etree.ElementTree} |
Neal Norwitz | 56640df | 2006-07-10 02:36:41 +0000 | [diff] [blame] | 3 | \moduleauthor{Fredrik Lundh}{fredrik@pythonware.com} |
Fred Drake | a58f123 | 2006-09-15 15:18:04 +0000 | [diff] [blame] | 4 | \modulesynopsis{Implementation of the ElementTree API.} |
Neal Norwitz | 56640df | 2006-07-10 02:36:41 +0000 | [diff] [blame] | 5 | |
Neal Norwitz | 56640df | 2006-07-10 02:36:41 +0000 | [diff] [blame] | 6 | \versionadded{2.5} |
| 7 | |
Neal Norwitz | 56640df | 2006-07-10 02:36:41 +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: |
Fred Drake | a58f123 | 2006-09-15 15:18:04 +0000 | [diff] [blame] | 13 | |
Neal Norwitz | 56640df | 2006-07-10 02:36:41 +0000 | [diff] [blame] | 14 | \begin{itemize} |
Fred Drake | a58f123 | 2006-09-15 15:18:04 +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 |
Neal Norwitz | 56640df | 2006-07-10 02:36:41 +0000 | [diff] [blame] | 21 | \end{itemize} |
| 22 | |
| 23 | To create an element instance, use the Element or SubElement factory |
| 24 | functions. |
| 25 | |
Fred Drake | a58f123 | 2006-09-15 15:18:04 +0000 | [diff] [blame] | 26 | The \class{ElementTree} class can be used to wrap an element |
Neal Norwitz | 56640df | 2006-07-10 02:36:41 +0000 | [diff] [blame] | 27 | structure, and convert it from and to XML. |
| 28 | |
Fred Drake | a58f123 | 2006-09-15 15:18:04 +0000 | [diff] [blame] | 29 | A C implementation of this API is available as |
| 30 | \module{xml.etree.cElementTree}. |
| 31 | |
Neal Norwitz | 56640df | 2006-07-10 02:36:41 +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. |
| 41 | |
| 42 | \begin{datadescni}{Returns:} |
| 43 | An element instance, representing a comment. |
| 44 | \end{datadescni} |
| 45 | \end{funcdesc} |
| 46 | |
| 47 | \begin{funcdesc}{dump}{elem} |
| 48 | Writes an element tree or element structure to sys.stdout. This |
| 49 | function should be used for debugging only. |
| 50 | |
| 51 | The exact output format is implementation dependent. In this |
| 52 | version, it's written as an ordinary XML file. |
| 53 | |
| 54 | \var{elem} is an element tree or an individual element. |
| 55 | \end{funcdesc} |
| 56 | |
| 57 | \begin{funcdesc}{Element}{tag\optional{, attrib}\optional{, **extra}} |
| 58 | Element factory. This function returns an object implementing the |
| 59 | standard Element interface. The exact class or type of that object |
| 60 | is implementation dependent, but it will always be compatible with |
| 61 | the {\_}ElementInterface class in this module. |
| 62 | |
| 63 | The element name, attribute names, and attribute values can be |
| 64 | either 8-bit ASCII strings or Unicode strings. |
| 65 | \var{tag} is the element name. |
| 66 | \var{attrib} is an optional dictionary, containing element attributes. |
| 67 | \var{extra} contains additional attributes, given as keyword arguments. |
| 68 | |
| 69 | \begin{datadescni}{Returns:} |
| 70 | An element instance. |
| 71 | \end{datadescni} |
| 72 | \end{funcdesc} |
| 73 | |
| 74 | \begin{funcdesc}{fromstring}{text} |
| 75 | Parses an XML section from a string constant. Same as XML. |
| 76 | \var{text} is a string containing XML data. |
| 77 | |
| 78 | \begin{datadescni}{Returns:} |
| 79 | An Element instance. |
| 80 | \end{datadescni} |
| 81 | \end{funcdesc} |
| 82 | |
| 83 | \begin{funcdesc}{iselement}{element} |
| 84 | Checks if an object appears to be a valid element object. |
| 85 | \var{element} is an element instance. |
| 86 | |
| 87 | \begin{datadescni}{Returns:} |
| 88 | A true value if this is an element object. |
| 89 | \end{datadescni} |
| 90 | \end{funcdesc} |
| 91 | |
| 92 | \begin{funcdesc}{iterparse}{source\optional{, events}} |
| 93 | Parses an XML section into an element tree incrementally, and reports |
| 94 | what's going on to the user. |
| 95 | \var{source} is a filename or file object containing XML data. |
| 96 | \var{events} is a list of events to report back. If omitted, only ``end'' |
| 97 | events are reported. |
| 98 | |
| 99 | \begin{datadescni}{Returns:} |
| 100 | A (event, elem) iterator. |
| 101 | \end{datadescni} |
| 102 | \end{funcdesc} |
| 103 | |
| 104 | \begin{funcdesc}{parse}{source\optional{, parser}} |
| 105 | Parses an XML section into an element tree. |
| 106 | \var{source} is a filename or file object containing XML data. |
| 107 | \var{parser} is an optional parser instance. If not given, the |
| 108 | standard XMLTreeBuilder parser is used. |
| 109 | |
| 110 | \begin{datadescni}{Returns:} |
| 111 | An ElementTree instance |
| 112 | \end{datadescni} |
| 113 | \end{funcdesc} |
| 114 | |
| 115 | \begin{funcdesc}{ProcessingInstruction}{target\optional{, text}} |
| 116 | PI element factory. This factory function creates a special element |
| 117 | that will be serialized as an XML processing instruction. |
| 118 | \var{target} is a string containing the PI target. |
| 119 | \var{text} is a string containing the PI contents, if given. |
| 120 | |
| 121 | \begin{datadescni}{Returns:} |
| 122 | An element instance, representing a PI. |
| 123 | \end{datadescni} |
| 124 | \end{funcdesc} |
| 125 | |
| 126 | \begin{funcdesc}{SubElement}{parent, tag\optional{, attrib} \optional{, **extra}} |
| 127 | Subelement factory. This function creates an element instance, and |
| 128 | appends it to an existing element. |
| 129 | |
| 130 | The element name, attribute names, and attribute values can be |
| 131 | either 8-bit ASCII strings or Unicode strings. |
| 132 | \var{parent} is the parent element. |
| 133 | \var{tag} is the subelement name. |
| 134 | \var{attrib} is an optional dictionary, containing element attributes. |
| 135 | \var{extra} contains additional attributes, given as keyword arguments. |
| 136 | |
| 137 | \begin{datadescni}{Returns:} |
| 138 | An element instance. |
| 139 | \end{datadescni} |
| 140 | \end{funcdesc} |
| 141 | |
| 142 | \begin{funcdesc}{tostring}{element\optional{, encoding}} |
| 143 | Generates a string representation of an XML element, including all |
| 144 | subelements. |
| 145 | \var{element} is an Element instance. |
| 146 | \var{encoding} is the output encoding (default is US-ASCII). |
| 147 | |
| 148 | \begin{datadescni}{Returns:} |
| 149 | An encoded string containing the XML data. |
| 150 | \end{datadescni} |
| 151 | \end{funcdesc} |
| 152 | |
| 153 | \begin{funcdesc}{XML}{text} |
| 154 | Parses an XML section from a string constant. This function can |
| 155 | be used to embed ``XML literals'' in Python code. |
| 156 | \var{text} is a string containing XML data. |
| 157 | |
| 158 | \begin{datadescni}{Returns:} |
| 159 | An Element instance. |
| 160 | \end{datadescni} |
| 161 | \end{funcdesc} |
| 162 | |
| 163 | \begin{funcdesc}{XMLID}{text} |
| 164 | Parses an XML section from a string constant, and also returns |
| 165 | a dictionary which maps from element id:s to elements. |
| 166 | \var{text} is a string containing XML data. |
| 167 | |
| 168 | \begin{datadescni}{Returns:} |
| 169 | A tuple containing an Element instance and a dictionary. |
| 170 | \end{datadescni} |
| 171 | \end{funcdesc} |
| 172 | |
| 173 | |
| 174 | \subsection{ElementTree Objects\label{elementtree-elementtree-objects}} |
| 175 | |
| 176 | \begin{classdesc}{ElementTree}{\optional{element,} \optional{file}} |
| 177 | ElementTree wrapper class. This class represents an entire element |
| 178 | hierarchy, and adds some extra support for serialization to and from |
| 179 | standard XML. |
| 180 | |
| 181 | \var{element} is the root element. |
| 182 | The tree is initialized with the contents of the XML \var{file} if given. |
| 183 | \end{classdesc} |
| 184 | |
| 185 | \begin{methoddesc}{_setroot}{element} |
| 186 | Replaces the root element for this tree. This discards the |
| 187 | current contents of the tree, and replaces it with the given |
| 188 | element. Use with care. |
| 189 | \var{element} is an element instance. |
| 190 | \end{methoddesc} |
| 191 | |
| 192 | \begin{methoddesc}{find}{path} |
| 193 | Finds the first toplevel element with given tag. |
| 194 | Same as getroot().find(path). |
| 195 | \var{path} is the element to look for. |
| 196 | |
| 197 | \begin{datadescni}{Returns:} |
| 198 | The first matching element, or None if no element was found. |
| 199 | \end{datadescni} |
| 200 | \end{methoddesc} |
| 201 | |
| 202 | \begin{methoddesc}{findall}{path} |
| 203 | Finds all toplevel elements with the given tag. |
| 204 | Same as getroot().findall(path). |
| 205 | \var{path} is the element to look for. |
| 206 | |
| 207 | \begin{datadescni}{Returns:} |
| 208 | A list or iterator containing all matching elements, |
| 209 | in section order. |
| 210 | \end{datadescni} |
| 211 | \end{methoddesc} |
| 212 | |
| 213 | \begin{methoddesc}{findtext}{path\optional{, default}} |
| 214 | Finds the element text for the first toplevel element with given |
| 215 | tag. Same as getroot().findtext(path). |
| 216 | \var{path} is the toplevel element to look for. |
| 217 | \var{default} is the value to return if the element was not found. |
| 218 | |
| 219 | \begin{datadescni}{Returns:} |
| 220 | The text content of the first matching element, or the |
| 221 | default value no element was found. Note that if the element |
| 222 | has is found, but has no text content, this method returns an |
| 223 | empty string. |
| 224 | \end{datadescni} |
| 225 | \end{methoddesc} |
| 226 | |
| 227 | \begin{methoddesc}{getiterator}{\optional{tag}} |
| 228 | Creates a tree iterator for the root element. The iterator loops |
| 229 | over all elements in this tree, in section order. |
| 230 | \var{tag} is the tag to look for (default is to return all elements) |
| 231 | |
| 232 | \begin{datadescni}{Returns:} |
| 233 | An iterator. |
| 234 | \end{datadescni} |
| 235 | \end{methoddesc} |
| 236 | |
| 237 | \begin{methoddesc}{getroot}{} |
| 238 | Gets the root element for this tree. |
| 239 | |
| 240 | \begin{datadescni}{Returns:} |
| 241 | An element instance. |
| 242 | \end{datadescni} |
| 243 | \end{methoddesc} |
| 244 | |
| 245 | \begin{methoddesc}{parse}{source\optional{, parser}} |
| 246 | Loads an external XML section into this element tree. |
| 247 | \var{source} is a file name or file object. |
| 248 | \var{parser} is an optional parser instance. If not given, the |
| 249 | standard XMLTreeBuilder parser is used. |
| 250 | |
| 251 | \begin{datadescni}{Returns:} |
| 252 | The section root element. |
| 253 | \end{datadescni} |
| 254 | \end{methoddesc} |
| 255 | |
| 256 | \begin{methoddesc}{write}{file\optional{, encoding}} |
| 257 | Writes the element tree to a file, as XML. |
| 258 | \var{file} is a file name, or a file object opened for writing. |
| 259 | \var{encoding} is the output encoding (default is US-ASCII). |
| 260 | \end{methoddesc} |
| 261 | |
| 262 | |
| 263 | \subsection{QName Objects\label{elementtree-qname-objects}} |
| 264 | |
| 265 | \begin{classdesc}{QName}{text_or_uri\optional{, tag}} |
| 266 | QName wrapper. This can be used to wrap a QName attribute value, in |
| 267 | order to get proper namespace handling on output. |
| 268 | \var{text_or_uri} is a string containing the QName value, |
| 269 | in the form {\{}uri{\}}local, or, if the tag argument is given, |
| 270 | the URI part of a QName. |
| 271 | If \var{tag} is given, the first argument is interpreted as |
| 272 | an URI, and this argument is interpreted as a local name. |
| 273 | |
| 274 | \begin{datadescni}{Returns:} |
| 275 | An opaque object, representing the QName. |
| 276 | \end{datadescni} |
| 277 | \end{classdesc} |
| 278 | |
| 279 | |
| 280 | \subsection{TreeBuilder Objects\label{elementtree-treebuilder-objects}} |
| 281 | |
| 282 | \begin{classdesc}{TreeBuilder}{\optional{element_factory}} |
| 283 | Generic element structure builder. This builder converts a sequence |
| 284 | of start, data, and end method calls to a well-formed element structure. |
| 285 | You can use this class to build an element structure using a custom XML |
| 286 | parser, or a parser for some other XML-like format. |
| 287 | The \var{element_factory} is called to create new Element instances when |
| 288 | given. |
| 289 | \end{classdesc} |
| 290 | |
| 291 | \begin{methoddesc}{close}{} |
| 292 | Flushes the parser buffers, and returns the toplevel documen |
| 293 | element. |
| 294 | |
| 295 | \begin{datadescni}{Returns:} |
| 296 | An Element instance. |
| 297 | \end{datadescni} |
| 298 | \end{methoddesc} |
| 299 | |
| 300 | \begin{methoddesc}{data}{data} |
| 301 | Adds text to the current element. |
| 302 | \var{data} is a string. This should be either an 8-bit string |
| 303 | containing ASCII text, or a Unicode string. |
| 304 | \end{methoddesc} |
| 305 | |
| 306 | \begin{methoddesc}{end}{tag} |
| 307 | Closes the current element. |
| 308 | \var{tag} is the element name. |
| 309 | |
| 310 | \begin{datadescni}{Returns:} |
| 311 | The closed element. |
| 312 | \end{datadescni} |
| 313 | \end{methoddesc} |
| 314 | |
| 315 | \begin{methoddesc}{start}{tag, attrs} |
| 316 | Opens a new element. |
| 317 | \var{tag} is the element name. |
| 318 | \var{attrs} is a dictionary containing element attributes. |
| 319 | |
| 320 | \begin{datadescni}{Returns:} |
| 321 | The opened element. |
| 322 | \end{datadescni} |
| 323 | \end{methoddesc} |
| 324 | |
| 325 | |
| 326 | \subsection{XMLTreeBuilder Objects\label{elementtree-xmltreebuilder-objects}} |
| 327 | |
| 328 | \begin{classdesc}{XMLTreeBuilder}{\optional{html,} \optional{target}} |
| 329 | Element structure builder for XML source data, based on the |
| 330 | expat parser. |
| 331 | \var{html} are predefined HTML entities. This flag is not supported |
| 332 | by the current implementation. |
| 333 | \var{target} is the target object. If omitted, the builder uses an |
| 334 | instance of the standard TreeBuilder class. |
| 335 | \end{classdesc} |
| 336 | |
| 337 | \begin{methoddesc}{close}{} |
| 338 | Finishes feeding data to the parser. |
| 339 | |
| 340 | \begin{datadescni}{Returns:} |
| 341 | An element structure. |
| 342 | \end{datadescni} |
| 343 | \end{methoddesc} |
| 344 | |
| 345 | \begin{methoddesc}{doctype}{name, pubid, system} |
| 346 | Handles a doctype declaration. |
| 347 | \var{name} is the doctype name. |
| 348 | \var{pubid} is the public identifier. |
| 349 | \var{system} is the system identifier. |
| 350 | \end{methoddesc} |
| 351 | |
| 352 | \begin{methoddesc}{feed}{data} |
| 353 | Feeds data to the parser. |
| 354 | |
| 355 | \var{data} is encoded data. |
| 356 | \end{methoddesc} |