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