blob: ffa19435c10cea63886a9c2a00b00b0c315bc8bc [file] [log] [blame]
Fred Drakea58f1232006-09-15 15:18:04 +00001\section{\module{xml.etree.ElementTree} --- The ElementTree XML API}
2\declaremodule{standard}{xml.etree.ElementTree}
Neal Norwitz56640df2006-07-10 02:36:41 +00003\moduleauthor{Fredrik Lundh}{fredrik@pythonware.com}
Fred Drakea58f1232006-09-15 15:18:04 +00004\modulesynopsis{Implementation of the ElementTree API.}
Neal Norwitz56640df2006-07-10 02:36:41 +00005
Neal Norwitz56640df2006-07-10 02:36:41 +00006\versionadded{2.5}
7
Neal Norwitz56640df2006-07-10 02:36:41 +00008The Element type is a flexible container object, designed to store
9hierarchical data structures in memory. The type can be described as a
10cross between a list and a dictionary.
11
12Each element has a number of properties associated with it:
Fred Drakea58f1232006-09-15 15:18:04 +000013
Neal Norwitz56640df2006-07-10 02:36:41 +000014\begin{itemize}
Fred Drakea58f1232006-09-15 15:18:04 +000015 \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 Norwitz56640df2006-07-10 02:36:41 +000021\end{itemize}
22
23To create an element instance, use the Element or SubElement factory
24functions.
25
Fred Drakea58f1232006-09-15 15:18:04 +000026The \class{ElementTree} class can be used to wrap an element
Neal Norwitz56640df2006-07-10 02:36:41 +000027structure, and convert it from and to XML.
28
Fred Drakea58f1232006-09-15 15:18:04 +000029A C implementation of this API is available as
30\module{xml.etree.cElementTree}.
31
Neal Norwitz56640df2006-07-10 02:36:41 +000032
33\subsection{Functions\label{elementtree-functions}}
34
35\begin{funcdesc}{Comment}{\optional{text}}
36Comment element factory. This factory function creates a special
37element that will be serialized as an XML comment.
38The comment string can be either an 8-bit ASCII string or a Unicode
39string.
40\var{text} is a string containing the comment string.
41
42\begin{datadescni}{Returns:}
43An element instance, representing a comment.
44\end{datadescni}
45\end{funcdesc}
46
47\begin{funcdesc}{dump}{elem}
48Writes an element tree or element structure to sys.stdout. This
49function should be used for debugging only.
50
51The exact output format is implementation dependent. In this
52version, 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}}
58Element factory. This function returns an object implementing the
59standard Element interface. The exact class or type of that object
60is implementation dependent, but it will always be compatible with
61the {\_}ElementInterface class in this module.
62
63The element name, attribute names, and attribute values can be
64either 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:}
70An element instance.
71\end{datadescni}
72\end{funcdesc}
73
74\begin{funcdesc}{fromstring}{text}
75Parses an XML section from a string constant. Same as XML.
76\var{text} is a string containing XML data.
77
78\begin{datadescni}{Returns:}
79An Element instance.
80\end{datadescni}
81\end{funcdesc}
82
83\begin{funcdesc}{iselement}{element}
84Checks if an object appears to be a valid element object.
85\var{element} is an element instance.
86
87\begin{datadescni}{Returns:}
88A true value if this is an element object.
89\end{datadescni}
90\end{funcdesc}
91
92\begin{funcdesc}{iterparse}{source\optional{, events}}
93Parses an XML section into an element tree incrementally, and reports
94what'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''
97events are reported.
98
99\begin{datadescni}{Returns:}
100A (event, elem) iterator.
101\end{datadescni}
102\end{funcdesc}
103
104\begin{funcdesc}{parse}{source\optional{, parser}}
105Parses 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
108standard XMLTreeBuilder parser is used.
109
110\begin{datadescni}{Returns:}
111An ElementTree instance
112\end{datadescni}
113\end{funcdesc}
114
115\begin{funcdesc}{ProcessingInstruction}{target\optional{, text}}
116PI element factory. This factory function creates a special element
117that 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:}
122An element instance, representing a PI.
123\end{datadescni}
124\end{funcdesc}
125
126\begin{funcdesc}{SubElement}{parent, tag\optional{, attrib} \optional{, **extra}}
127Subelement factory. This function creates an element instance, and
128appends it to an existing element.
129
130The element name, attribute names, and attribute values can be
131either 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:}
138An element instance.
139\end{datadescni}
140\end{funcdesc}
141
142\begin{funcdesc}{tostring}{element\optional{, encoding}}
143Generates a string representation of an XML element, including all
144subelements.
145\var{element} is an Element instance.
146\var{encoding} is the output encoding (default is US-ASCII).
147
148\begin{datadescni}{Returns:}
149An encoded string containing the XML data.
150\end{datadescni}
151\end{funcdesc}
152
153\begin{funcdesc}{XML}{text}
154Parses an XML section from a string constant. This function can
155be used to embed ``XML literals'' in Python code.
156\var{text} is a string containing XML data.
157
158\begin{datadescni}{Returns:}
159An Element instance.
160\end{datadescni}
161\end{funcdesc}
162
163\begin{funcdesc}{XMLID}{text}
164Parses an XML section from a string constant, and also returns
165a dictionary which maps from element id:s to elements.
166\var{text} is a string containing XML data.
167
168\begin{datadescni}{Returns:}
169A 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}}
177ElementTree wrapper class. This class represents an entire element
178hierarchy, and adds some extra support for serialization to and from
179standard XML.
180
181\var{element} is the root element.
182The tree is initialized with the contents of the XML \var{file} if given.
183\end{classdesc}
184
185\begin{methoddesc}{_setroot}{element}
186Replaces the root element for this tree. This discards the
187current contents of the tree, and replaces it with the given
188element. Use with care.
189\var{element} is an element instance.
190\end{methoddesc}
191
192\begin{methoddesc}{find}{path}
193Finds the first toplevel element with given tag.
194Same as getroot().find(path).
195\var{path} is the element to look for.
196
197\begin{datadescni}{Returns:}
198The first matching element, or None if no element was found.
199\end{datadescni}
200\end{methoddesc}
201
202\begin{methoddesc}{findall}{path}
203Finds all toplevel elements with the given tag.
204Same as getroot().findall(path).
205\var{path} is the element to look for.
206
207\begin{datadescni}{Returns:}
208A list or iterator containing all matching elements,
209in section order.
210\end{datadescni}
211\end{methoddesc}
212
213\begin{methoddesc}{findtext}{path\optional{, default}}
214Finds the element text for the first toplevel element with given
215tag. 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:}
220The text content of the first matching element, or the
221default value no element was found. Note that if the element
222has is found, but has no text content, this method returns an
223empty string.
224\end{datadescni}
225\end{methoddesc}
226
227\begin{methoddesc}{getiterator}{\optional{tag}}
228Creates a tree iterator for the root element. The iterator loops
229over 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:}
233An iterator.
234\end{datadescni}
235\end{methoddesc}
236
237\begin{methoddesc}{getroot}{}
238Gets the root element for this tree.
239
240\begin{datadescni}{Returns:}
241An element instance.
242\end{datadescni}
243\end{methoddesc}
244
245\begin{methoddesc}{parse}{source\optional{, parser}}
246Loads 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
249standard XMLTreeBuilder parser is used.
250
251\begin{datadescni}{Returns:}
252The section root element.
253\end{datadescni}
254\end{methoddesc}
255
256\begin{methoddesc}{write}{file\optional{, encoding}}
257Writes 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}}
266QName wrapper. This can be used to wrap a QName attribute value, in
267order to get proper namespace handling on output.
268\var{text_or_uri} is a string containing the QName value,
269in the form {\{}uri{\}}local, or, if the tag argument is given,
270the URI part of a QName.
271If \var{tag} is given, the first argument is interpreted as
272an URI, and this argument is interpreted as a local name.
273
274\begin{datadescni}{Returns:}
275An 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}}
283Generic element structure builder. This builder converts a sequence
284of start, data, and end method calls to a well-formed element structure.
285You can use this class to build an element structure using a custom XML
286parser, or a parser for some other XML-like format.
287The \var{element_factory} is called to create new Element instances when
288given.
289\end{classdesc}
290
291\begin{methoddesc}{close}{}
292Flushes the parser buffers, and returns the toplevel documen
293element.
294
295\begin{datadescni}{Returns:}
296An Element instance.
297\end{datadescni}
298\end{methoddesc}
299
300\begin{methoddesc}{data}{data}
301Adds text to the current element.
302\var{data} is a string. This should be either an 8-bit string
303containing ASCII text, or a Unicode string.
304\end{methoddesc}
305
306\begin{methoddesc}{end}{tag}
307Closes the current element.
308\var{tag} is the element name.
309
310\begin{datadescni}{Returns:}
311The closed element.
312\end{datadescni}
313\end{methoddesc}
314
315\begin{methoddesc}{start}{tag, attrs}
316Opens a new element.
317\var{tag} is the element name.
318\var{attrs} is a dictionary containing element attributes.
319
320\begin{datadescni}{Returns:}
321The 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}}
329Element structure builder for XML source data, based on the
330expat parser.
331\var{html} are predefined HTML entities. This flag is not supported
332by the current implementation.
333\var{target} is the target object. If omitted, the builder uses an
334instance of the standard TreeBuilder class.
335\end{classdesc}
336
337\begin{methoddesc}{close}{}
338Finishes feeding data to the parser.
339
340\begin{datadescni}{Returns:}
341An element structure.
342\end{datadescni}
343\end{methoddesc}
344
345\begin{methoddesc}{doctype}{name, pubid, system}
346Handles 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}
353Feeds data to the parser.
354
355\var{data} is encoded data.
356\end{methoddesc}