1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>20.5. xml.etree.ElementTree — The ElementTree XML API — Python 3.5.2 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.2 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
33
<link rel="up" title="20. Structured Markup Processing Tools" href="markup.html" />
34
<link rel="next" title="20.6. xml.dom — The Document Object Model API" href="xml.dom.html" />
35
<link rel="prev" title="20.4. XML Processing Modules" href="xml.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
38
<script type="text/javascript" src="../_static/copybutton.js"></script>
39
<script type="text/javascript" src="../_static/version_switch.js"></script>
45
<body role="document">
46
<div class="related" role="navigation" aria-label="related navigation">
49
<li class="right" style="margin-right: 10px">
50
<a href="../genindex.html" title="General Index"
51
accesskey="I">index</a></li>
53
<a href="../py-modindex.html" title="Python Module Index"
56
<a href="xml.dom.html" title="20.6. xml.dom — The Document Object Model API"
57
accesskey="N">next</a> |</li>
59
<a href="xml.html" title="20.4. XML Processing Modules"
60
accesskey="P">previous</a> |</li>
61
<li><img src="../_static/py.png" alt=""
62
style="vertical-align: middle; margin-top: -1px"/></li>
63
<li><a href="https://www.python.org/">Python</a> »</li>
65
<span class="version_switcher_placeholder">3.5.2</span>
66
<a href="../index.html">Documentation </a> »
69
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
70
<li class="nav-item nav-item-2"><a href="markup.html" accesskey="U">20. Structured Markup Processing Tools</a> »</li>
74
<div class="inline-search" style="display: none" role="search">
75
<form class="inline-search" action="../search.html" method="get">
76
<input placeholder="Quick search" type="text" name="q" />
77
<input type="submit" value="Go" />
78
<input type="hidden" name="check_keywords" value="yes" />
79
<input type="hidden" name="area" value="default" />
82
<script type="text/javascript">$('.inline-search').show(0);</script>
89
<div class="document">
90
<div class="documentwrapper">
91
<div class="bodywrapper">
92
<div class="body" role="main">
94
<div class="section" id="module-xml.etree.ElementTree">
95
<span id="xml-etree-elementtree-the-elementtree-xml-api"></span><h1>20.5. <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></code></a> — The ElementTree XML API<a class="headerlink" href="#module-xml.etree.ElementTree" title="Permalink to this headline">¶</a></h1>
96
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/xml/etree/ElementTree.py">Lib/xml/etree/ElementTree.py</a></p>
97
<hr class="docutils" />
98
<p>The <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></code></a> module implements a simple and efficient API
99
for parsing and creating XML data.</p>
100
<div class="versionchanged">
101
<p><span class="versionmodified">Changed in version 3.3: </span>This module will use a fast implementation whenever available.
102
The <code class="xref py py-mod docutils literal"><span class="pre">xml.etree.cElementTree</span></code> module is deprecated.</p>
104
<div class="admonition warning">
105
<p class="first admonition-title">Warning</p>
106
<p class="last">The <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></code></a> module is not secure against
107
maliciously constructed data. If you need to parse untrusted or
108
unauthenticated data see <a class="reference internal" href="xml.html#xml-vulnerabilities"><span>XML vulnerabilities</span></a>.</p>
110
<div class="section" id="tutorial">
111
<h2>20.5.1. Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2>
112
<p>This is a short tutorial for using <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></code></a> (<code class="docutils literal"><span class="pre">ET</span></code> in
113
short). The goal is to demonstrate some of the building blocks and basic
114
concepts of the module.</p>
115
<div class="section" id="xml-tree-and-elements">
116
<h3>20.5.1.1. XML tree and elements<a class="headerlink" href="#xml-tree-and-elements" title="Permalink to this headline">¶</a></h3>
117
<p>XML is an inherently hierarchical data format, and the most natural way to
118
represent it is with a tree. <code class="docutils literal"><span class="pre">ET</span></code> has two classes for this purpose -
119
<a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal"><span class="pre">ElementTree</span></code></a> represents the whole XML document as a tree, and
120
<a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> represents a single node in this tree. Interactions with
121
the whole document (reading and writing to/from files) are usually done
122
on the <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal"><span class="pre">ElementTree</span></code></a> level. Interactions with a single XML element
123
and its sub-elements are done on the <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> level.</p>
125
<div class="section" id="parsing-xml">
126
<span id="elementtree-parsing-xml"></span><h3>20.5.1.2. Parsing XML<a class="headerlink" href="#parsing-xml" title="Permalink to this headline">¶</a></h3>
127
<p>We’ll be using the following XML document as the sample data for this section:</p>
128
<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span>
129
<span class="nt"><data></span>
130
<span class="nt"><country</span> <span class="na">name=</span><span class="s">"Liechtenstein"</span><span class="nt">></span>
131
<span class="nt"><rank></span>1<span class="nt"></rank></span>
132
<span class="nt"><year></span>2008<span class="nt"></year></span>
133
<span class="nt"><gdppc></span>141100<span class="nt"></gdppc></span>
134
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Austria"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span>
135
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Switzerland"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span>
136
<span class="nt"></country></span>
137
<span class="nt"><country</span> <span class="na">name=</span><span class="s">"Singapore"</span><span class="nt">></span>
138
<span class="nt"><rank></span>4<span class="nt"></rank></span>
139
<span class="nt"><year></span>2011<span class="nt"></year></span>
140
<span class="nt"><gdppc></span>59900<span class="nt"></gdppc></span>
141
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Malaysia"</span> <span class="na">direction=</span><span class="s">"N"</span><span class="nt">/></span>
142
<span class="nt"></country></span>
143
<span class="nt"><country</span> <span class="na">name=</span><span class="s">"Panama"</span><span class="nt">></span>
144
<span class="nt"><rank></span>68<span class="nt"></rank></span>
145
<span class="nt"><year></span>2011<span class="nt"></year></span>
146
<span class="nt"><gdppc></span>13600<span class="nt"></gdppc></span>
147
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Costa Rica"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span>
148
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Colombia"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span>
149
<span class="nt"></country></span>
150
<span class="nt"></data></span>
153
<p>We can import this data by reading from a file:</p>
154
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">as</span> <span class="nn">ET</span>
155
<span class="n">tree</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">'country_data.xml'</span><span class="p">)</span>
156
<span class="n">root</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">getroot</span><span class="p">()</span>
159
<p>Or directly from a string:</p>
160
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">root</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">country_data_as_string</span><span class="p">)</span>
163
<p><a class="reference internal" href="#xml.etree.ElementTree.fromstring" title="xml.etree.ElementTree.fromstring"><code class="xref py py-func docutils literal"><span class="pre">fromstring()</span></code></a> parses XML from a string directly into an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a>,
164
which is the root element of the parsed tree. Other parsing functions may
165
create an <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal"><span class="pre">ElementTree</span></code></a>. Check the documentation to be sure.</p>
166
<p>As an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a>, <code class="docutils literal"><span class="pre">root</span></code> has a tag and a dictionary of attributes:</p>
167
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">root</span><span class="o">.</span><span class="n">tag</span>
168
<span class="go">'data'</span>
169
<span class="gp">>>> </span><span class="n">root</span><span class="o">.</span><span class="n">attrib</span>
170
<span class="go">{}</span>
173
<p>It also has children nodes over which we can iterate:</p>
174
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">root</span><span class="p">:</span>
175
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">child</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="n">child</span><span class="o">.</span><span class="n">attrib</span><span class="p">)</span>
176
<span class="gp">...</span>
177
<span class="go">country {'name': 'Liechtenstein'}</span>
178
<span class="go">country {'name': 'Singapore'}</span>
179
<span class="go">country {'name': 'Panama'}</span>
182
<p>Children are nested, and we can access specific child nodes by index:</p>
183
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">root</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">text</span>
184
<span class="go">'2008'</span>
187
<div class="admonition note">
188
<p class="first admonition-title">Note</p>
189
<p class="last">Not all elements of the XML input will end up as elements of the
190
parsed tree. Currently, this module skips over any XML comments,
191
processing instructions, and document type declarations in the
192
input. Nevertheless, trees built using this module’s API rather
193
than parsing from XML text can have comments and processing
194
instructions in them; they will be included when generating XML
195
output. A document type declaration may be accessed by passing a
196
custom <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></code></a> instance to the <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a>
200
<div class="section" id="pull-api-for-non-blocking-parsing">
201
<span id="elementtree-pull-parsing"></span><h3>20.5.1.3. Pull API for non-blocking parsing<a class="headerlink" href="#pull-api-for-non-blocking-parsing" title="Permalink to this headline">¶</a></h3>
202
<p>Most parsing functions provided by this module require the whole document
203
to be read at once before returning any result. It is possible to use an
204
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> and feed data into it incrementally, but it is a push API that
205
calls methods on a callback target, which is too low-level and inconvenient for
206
most needs. Sometimes what the user really wants is to be able to parse XML
207
incrementally, without blocking operations, while enjoying the convenience of
208
fully constructed <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> objects.</p>
209
<p>The most powerful tool for doing this is <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></code></a>. It does not
210
require a blocking read to obtain the XML data, and is instead fed with data
211
incrementally with <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.feed" title="xml.etree.ElementTree.XMLPullParser.feed"><code class="xref py py-meth docutils literal"><span class="pre">XMLPullParser.feed()</span></code></a> calls. To get the parsed XML
212
elements, call <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><code class="xref py py-meth docutils literal"><span class="pre">XMLPullParser.read_events()</span></code></a>. Here is an example:</p>
213
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XMLPullParser</span><span class="p">([</span><span class="s1">'start'</span><span class="p">,</span> <span class="s1">'end'</span><span class="p">])</span>
214
<span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="s1">'<mytag>sometext'</span><span class="p">)</span>
215
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">read_events</span><span class="p">())</span>
216
<span class="go">[('start', <Element 'mytag' at 0x7fa66db2be58>)]</span>
217
<span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="s1">' more text</mytag>'</span><span class="p">)</span>
218
<span class="gp">>>> </span><span class="k">for</span> <span class="n">event</span><span class="p">,</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">parser</span><span class="o">.</span><span class="n">read_events</span><span class="p">():</span>
219
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
220
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">elem</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="s1">'text='</span><span class="p">,</span> <span class="n">elem</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
221
<span class="gp">...</span>
222
<span class="go">end</span>
225
<p>The obvious use case is applications that operate in a non-blocking fashion
226
where the XML data is being received from a socket or read incrementally from
227
some storage device. In such cases, blocking reads are unacceptable.</p>
228
<p>Because it’s so flexible, <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></code></a> can be inconvenient to use for
229
simpler use-cases. If you don’t mind your application blocking on reading XML
230
data but would still like to have incremental parsing capabilities, take a look
231
at <a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><code class="xref py py-func docutils literal"><span class="pre">iterparse()</span></code></a>. It can be useful when you’re reading a large XML document
232
and don’t want to hold it wholly in memory.</p>
234
<div class="section" id="finding-interesting-elements">
235
<h3>20.5.1.4. Finding interesting elements<a class="headerlink" href="#finding-interesting-elements" title="Permalink to this headline">¶</a></h3>
236
<p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> has some useful methods that help iterate recursively over all
237
the sub-tree below it (its children, their children, and so on). For example,
238
<a class="reference internal" href="#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><code class="xref py py-meth docutils literal"><span class="pre">Element.iter()</span></code></a>:</p>
239
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">neighbor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s1">'neighbor'</span><span class="p">):</span>
240
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">neighbor</span><span class="o">.</span><span class="n">attrib</span><span class="p">)</span>
241
<span class="gp">...</span>
242
<span class="go">{'name': 'Austria', 'direction': 'E'}</span>
243
<span class="go">{'name': 'Switzerland', 'direction': 'W'}</span>
244
<span class="go">{'name': 'Malaysia', 'direction': 'N'}</span>
245
<span class="go">{'name': 'Costa Rica', 'direction': 'W'}</span>
246
<span class="go">{'name': 'Colombia', 'direction': 'E'}</span>
249
<p><a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><code class="xref py py-meth docutils literal"><span class="pre">Element.findall()</span></code></a> finds only elements with a tag which are direct
250
children of the current element. <a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><code class="xref py py-meth docutils literal"><span class="pre">Element.find()</span></code></a> finds the <em>first</em> child
251
with a particular tag, and <a class="reference internal" href="#xml.etree.ElementTree.Element.text" title="xml.etree.ElementTree.Element.text"><code class="xref py py-attr docutils literal"><span class="pre">Element.text</span></code></a> accesses the element’s text
252
content. <a class="reference internal" href="#xml.etree.ElementTree.Element.get" title="xml.etree.ElementTree.Element.get"><code class="xref py py-meth docutils literal"><span class="pre">Element.get()</span></code></a> accesses the element’s attributes:</p>
253
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">country</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'country'</span><span class="p">):</span>
254
<span class="gp">... </span> <span class="n">rank</span> <span class="o">=</span> <span class="n">country</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'rank'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
255
<span class="gp">... </span> <span class="n">name</span> <span class="o">=</span> <span class="n">country</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'name'</span><span class="p">)</span>
256
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">rank</span><span class="p">)</span>
257
<span class="gp">...</span>
258
<span class="go">Liechtenstein 1</span>
259
<span class="go">Singapore 4</span>
260
<span class="go">Panama 68</span>
263
<p>More sophisticated specification of which elements to look for is possible by
264
using <a class="reference internal" href="#elementtree-xpath"><span>XPath</span></a>.</p>
266
<div class="section" id="modifying-an-xml-file">
267
<h3>20.5.1.5. Modifying an XML File<a class="headerlink" href="#modifying-an-xml-file" title="Permalink to this headline">¶</a></h3>
268
<p><a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal"><span class="pre">ElementTree</span></code></a> provides a simple way to build XML documents and write them to files.
269
The <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><code class="xref py py-meth docutils literal"><span class="pre">ElementTree.write()</span></code></a> method serves this purpose.</p>
270
<p>Once created, an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> object may be manipulated by directly changing
271
its fields (such as <a class="reference internal" href="#xml.etree.ElementTree.Element.text" title="xml.etree.ElementTree.Element.text"><code class="xref py py-attr docutils literal"><span class="pre">Element.text</span></code></a>), adding and modifying attributes
272
(<a class="reference internal" href="#xml.etree.ElementTree.Element.set" title="xml.etree.ElementTree.Element.set"><code class="xref py py-meth docutils literal"><span class="pre">Element.set()</span></code></a> method), as well as adding new children (for example
273
with <a class="reference internal" href="#xml.etree.ElementTree.Element.append" title="xml.etree.ElementTree.Element.append"><code class="xref py py-meth docutils literal"><span class="pre">Element.append()</span></code></a>).</p>
274
<p>Let’s say we want to add one to each country’s rank, and add an <code class="docutils literal"><span class="pre">updated</span></code>
275
attribute to the rank element:</p>
276
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">rank</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s1">'rank'</span><span class="p">):</span>
277
<span class="gp">... </span> <span class="n">new_rank</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">rank</span><span class="o">.</span><span class="n">text</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
278
<span class="gp">... </span> <span class="n">rank</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">new_rank</span><span class="p">)</span>
279
<span class="gp">... </span> <span class="n">rank</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s1">'updated'</span><span class="p">,</span> <span class="s1">'yes'</span><span class="p">)</span>
280
<span class="gp">...</span>
281
<span class="gp">>>> </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'output.xml'</span><span class="p">)</span>
284
<p>Our XML now looks like this:</p>
285
<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span>
286
<span class="nt"><data></span>
287
<span class="nt"><country</span> <span class="na">name=</span><span class="s">"Liechtenstein"</span><span class="nt">></span>
288
<span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>2<span class="nt"></rank></span>
289
<span class="nt"><year></span>2008<span class="nt"></year></span>
290
<span class="nt"><gdppc></span>141100<span class="nt"></gdppc></span>
291
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Austria"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span>
292
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Switzerland"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span>
293
<span class="nt"></country></span>
294
<span class="nt"><country</span> <span class="na">name=</span><span class="s">"Singapore"</span><span class="nt">></span>
295
<span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>5<span class="nt"></rank></span>
296
<span class="nt"><year></span>2011<span class="nt"></year></span>
297
<span class="nt"><gdppc></span>59900<span class="nt"></gdppc></span>
298
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Malaysia"</span> <span class="na">direction=</span><span class="s">"N"</span><span class="nt">/></span>
299
<span class="nt"></country></span>
300
<span class="nt"><country</span> <span class="na">name=</span><span class="s">"Panama"</span><span class="nt">></span>
301
<span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>69<span class="nt"></rank></span>
302
<span class="nt"><year></span>2011<span class="nt"></year></span>
303
<span class="nt"><gdppc></span>13600<span class="nt"></gdppc></span>
304
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Costa Rica"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span>
305
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Colombia"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span>
306
<span class="nt"></country></span>
307
<span class="nt"></data></span>
310
<p>We can remove elements using <a class="reference internal" href="#xml.etree.ElementTree.Element.remove" title="xml.etree.ElementTree.Element.remove"><code class="xref py py-meth docutils literal"><span class="pre">Element.remove()</span></code></a>. Let’s say we want to
311
remove all countries with a rank higher than 50:</p>
312
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">country</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'country'</span><span class="p">):</span>
313
<span class="gp">... </span> <span class="n">rank</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">country</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'rank'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
314
<span class="gp">... </span> <span class="k">if</span> <span class="n">rank</span> <span class="o">></span> <span class="mi">50</span><span class="p">:</span>
315
<span class="gp">... </span> <span class="n">root</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">country</span><span class="p">)</span>
316
<span class="gp">...</span>
317
<span class="gp">>>> </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'output.xml'</span><span class="p">)</span>
320
<p>Our XML now looks like this:</p>
321
<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span>
322
<span class="nt"><data></span>
323
<span class="nt"><country</span> <span class="na">name=</span><span class="s">"Liechtenstein"</span><span class="nt">></span>
324
<span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>2<span class="nt"></rank></span>
325
<span class="nt"><year></span>2008<span class="nt"></year></span>
326
<span class="nt"><gdppc></span>141100<span class="nt"></gdppc></span>
327
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Austria"</span> <span class="na">direction=</span><span class="s">"E"</span><span class="nt">/></span>
328
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Switzerland"</span> <span class="na">direction=</span><span class="s">"W"</span><span class="nt">/></span>
329
<span class="nt"></country></span>
330
<span class="nt"><country</span> <span class="na">name=</span><span class="s">"Singapore"</span><span class="nt">></span>
331
<span class="nt"><rank</span> <span class="na">updated=</span><span class="s">"yes"</span><span class="nt">></span>5<span class="nt"></rank></span>
332
<span class="nt"><year></span>2011<span class="nt"></year></span>
333
<span class="nt"><gdppc></span>59900<span class="nt"></gdppc></span>
334
<span class="nt"><neighbor</span> <span class="na">name=</span><span class="s">"Malaysia"</span> <span class="na">direction=</span><span class="s">"N"</span><span class="nt">/></span>
335
<span class="nt"></country></span>
336
<span class="nt"></data></span>
340
<div class="section" id="building-xml-documents">
341
<h3>20.5.1.6. Building XML documents<a class="headerlink" href="#building-xml-documents" title="Permalink to this headline">¶</a></h3>
342
<p>The <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><code class="xref py py-func docutils literal"><span class="pre">SubElement()</span></code></a> function also provides a convenient way to create new
343
sub-elements for a given element:</p>
344
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span>
345
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">)</span>
346
<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">)</span>
347
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">)</span>
348
<span class="gp">>>> </span><span class="n">ET</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
349
<span class="go"><a><b /><c><d /></c></a></span>
353
<div class="section" id="parsing-xml-with-namespaces">
354
<h3>20.5.1.7. Parsing XML with Namespaces<a class="headerlink" href="#parsing-xml-with-namespaces" title="Permalink to this headline">¶</a></h3>
355
<p>If the XML input has <a class="reference external" href="https://en.wikipedia.org/wiki/XML_namespace">namespaces</a>, tags and attributes
356
with prefixes in the form <code class="docutils literal"><span class="pre">prefix:sometag</span></code> get expanded to
357
<code class="docutils literal"><span class="pre">{uri}sometag</span></code> where the <em>prefix</em> is replaced by the full <em>URI</em>.
358
Also, if there is a <a class="reference external" href="https://www.w3.org/TR/2006/REC-xml-names-20060816/#defaulting">default namespace</a>,
359
that full URI gets prepended to all of the non-prefixed tags.</p>
360
<p>Here is an XML example that incorporates two namespaces, one with the
361
prefix “fictional” and the other serving as the default namespace:</p>
362
<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="cp"><?xml version="1.0"?></span>
363
<span class="nt"><actors</span> <span class="na">xmlns:fictional=</span><span class="s">"http://characters.example.com"</span>
364
<span class="na">xmlns=</span><span class="s">"http://people.example.com"</span><span class="nt">></span>
365
<span class="nt"><actor></span>
366
<span class="nt"><name></span>John Cleese<span class="nt"></name></span>
367
<span class="nt"><fictional:character></span>Lancelot<span class="nt"></fictional:character></span>
368
<span class="nt"><fictional:character></span>Archie Leach<span class="nt"></fictional:character></span>
369
<span class="nt"></actor></span>
370
<span class="nt"><actor></span>
371
<span class="nt"><name></span>Eric Idle<span class="nt"></name></span>
372
<span class="nt"><fictional:character></span>Sir Robin<span class="nt"></fictional:character></span>
373
<span class="nt"><fictional:character></span>Gunther<span class="nt"></fictional:character></span>
374
<span class="nt"><fictional:character></span>Commander Clement<span class="nt"></fictional:character></span>
375
<span class="nt"></actor></span>
376
<span class="nt"></actors></span>
379
<p>One way to search and explore this XML example is to manually add the
380
URI to every tag or attribute in the xpath of a
381
<a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><code class="xref py py-meth docutils literal"><span class="pre">find()</span></code></a> or <a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><code class="xref py py-meth docutils literal"><span class="pre">findall()</span></code></a>:</p>
382
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">root</span> <span class="o">=</span> <span class="n">fromstring</span><span class="p">(</span><span class="n">xml_text</span><span class="p">)</span>
383
<span class="k">for</span> <span class="n">actor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'{http://people.example.com}actor'</span><span class="p">):</span>
384
<span class="n">name</span> <span class="o">=</span> <span class="n">actor</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'{http://people.example.com}name'</span><span class="p">)</span>
385
<span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
386
<span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">actor</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'{http://characters.example.com}character'</span><span class="p">):</span>
387
<span class="nb">print</span><span class="p">(</span><span class="s1">' |-->'</span><span class="p">,</span> <span class="n">char</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
390
<p>A better way to search the namespaced XML example is to create a
391
dictionary with your own prefixes and use those in the search functions:</p>
392
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">ns</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'real_person'</span><span class="p">:</span> <span class="s1">'http://people.example.com'</span><span class="p">,</span>
393
<span class="s1">'role'</span><span class="p">:</span> <span class="s1">'http://characters.example.com'</span><span class="p">}</span>
395
<span class="k">for</span> <span class="n">actor</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'real_person:actor'</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span>
396
<span class="n">name</span> <span class="o">=</span> <span class="n">actor</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'real_person:name'</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span>
397
<span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
398
<span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">actor</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'role:character'</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span>
399
<span class="nb">print</span><span class="p">(</span><span class="s1">' |-->'</span><span class="p">,</span> <span class="n">char</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
402
<p>These two approaches both output:</p>
403
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">John</span> <span class="n">Cleese</span>
404
<span class="o">|--></span> <span class="n">Lancelot</span>
405
<span class="o">|--></span> <span class="n">Archie</span> <span class="n">Leach</span>
406
<span class="n">Eric</span> <span class="n">Idle</span>
407
<span class="o">|--></span> <span class="n">Sir</span> <span class="n">Robin</span>
408
<span class="o">|--></span> <span class="n">Gunther</span>
409
<span class="o">|--></span> <span class="n">Commander</span> <span class="n">Clement</span>
413
<div class="section" id="additional-resources">
414
<h3>20.5.1.8. Additional resources<a class="headerlink" href="#additional-resources" title="Permalink to this headline">¶</a></h3>
415
<p>See <a class="reference external" href="http://effbot.org/zone/element-index.htm">http://effbot.org/zone/element-index.htm</a> for tutorials and links to other
419
<div class="section" id="xpath-support">
420
<span id="elementtree-xpath"></span><h2>20.5.2. XPath support<a class="headerlink" href="#xpath-support" title="Permalink to this headline">¶</a></h2>
421
<p>This module provides limited support for
422
<a class="reference external" href="https://www.w3.org/TR/xpath">XPath expressions</a> for locating elements in a
423
tree. The goal is to support a small subset of the abbreviated syntax; a full
424
XPath engine is outside the scope of the module.</p>
425
<div class="section" id="example">
426
<h3>20.5.2.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
427
<p>Here’s an example that demonstrates some of the XPath capabilities of the
428
module. We’ll be using the <code class="docutils literal"><span class="pre">countrydata</span></code> XML document from the
429
<a class="reference internal" href="#elementtree-parsing-xml"><span>Parsing XML</span></a> section:</p>
430
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">as</span> <span class="nn">ET</span>
432
<span class="n">root</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">countrydata</span><span class="p">)</span>
434
<span class="c1"># Top-level elements</span>
435
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">"."</span><span class="p">)</span>
437
<span class="c1"># All 'neighbor' grand-children of 'country' children of the top-level</span>
438
<span class="c1"># elements</span>
439
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">"./country/neighbor"</span><span class="p">)</span>
441
<span class="c1"># Nodes with name='Singapore' that have a 'year' child</span>
442
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">".//year/..[@name='Singapore']"</span><span class="p">)</span>
444
<span class="c1"># 'year' nodes that are children of nodes with name='Singapore'</span>
445
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">".//*[@name='Singapore']/year"</span><span class="p">)</span>
447
<span class="c1"># All 'neighbor' nodes that are the second child of their parent</span>
448
<span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">".//neighbor[2]"</span><span class="p">)</span>
452
<div class="section" id="supported-xpath-syntax">
453
<h3>20.5.2.2. Supported XPath syntax<a class="headerlink" href="#supported-xpath-syntax" title="Permalink to this headline">¶</a></h3>
454
<table border="1" class="docutils">
459
<thead valign="bottom">
460
<tr class="row-odd"><th class="head">Syntax</th>
461
<th class="head">Meaning</th>
465
<tr class="row-even"><td><code class="docutils literal"><span class="pre">tag</span></code></td>
466
<td>Selects all child elements with the given tag.
467
For example, <code class="docutils literal"><span class="pre">spam</span></code> selects all child elements
468
named <code class="docutils literal"><span class="pre">spam</span></code>, and <code class="docutils literal"><span class="pre">spam/egg</span></code> selects all
469
grandchildren named <code class="docutils literal"><span class="pre">egg</span></code> in all children named
470
<code class="docutils literal"><span class="pre">spam</span></code>.</td>
472
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">*</span></code></td>
473
<td>Selects all child elements. For example, <code class="docutils literal"><span class="pre">*/egg</span></code>
474
selects all grandchildren named <code class="docutils literal"><span class="pre">egg</span></code>.</td>
476
<tr class="row-even"><td><code class="docutils literal"><span class="pre">.</span></code></td>
477
<td>Selects the current node. This is mostly useful
478
at the beginning of the path, to indicate that it’s
479
a relative path.</td>
481
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">//</span></code></td>
482
<td>Selects all subelements, on all levels beneath the
483
current element. For example, <code class="docutils literal"><span class="pre">.//egg</span></code> selects
484
all <code class="docutils literal"><span class="pre">egg</span></code> elements in the entire tree.</td>
486
<tr class="row-even"><td><code class="docutils literal"><span class="pre">..</span></code></td>
487
<td>Selects the parent element. Returns <code class="docutils literal"><span class="pre">None</span></code> if the
488
path attempts to reach the ancestors of the start
489
element (the element <code class="docutils literal"><span class="pre">find</span></code> was called on).</td>
491
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">[@attrib]</span></code></td>
492
<td>Selects all elements that have the given attribute.</td>
494
<tr class="row-even"><td><code class="docutils literal"><span class="pre">[@attrib='value']</span></code></td>
495
<td>Selects all elements for which the given attribute
496
has the given value. The value cannot contain
499
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">[tag]</span></code></td>
500
<td>Selects all elements that have a child named
501
<code class="docutils literal"><span class="pre">tag</span></code>. Only immediate children are supported.</td>
503
<tr class="row-even"><td><code class="docutils literal"><span class="pre">[tag='text']</span></code></td>
504
<td>Selects all elements that have a child named
505
<code class="docutils literal"><span class="pre">tag</span></code> whose complete text content, including
506
descendants, equals the given <code class="docutils literal"><span class="pre">text</span></code>.</td>
508
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">[position]</span></code></td>
509
<td>Selects all elements that are located at the given
510
position. The position can be either an integer
511
(1 is the first position), the expression <code class="docutils literal"><span class="pre">last()</span></code>
512
(for the last position), or a position relative to
513
the last position (e.g. <code class="docutils literal"><span class="pre">last()-1</span></code>).</td>
517
<p>Predicates (expressions within square brackets) must be preceded by a tag
518
name, an asterisk, or another predicate. <code class="docutils literal"><span class="pre">position</span></code> predicates must be
519
preceded by a tag name.</p>
522
<div class="section" id="reference">
523
<h2>20.5.3. Reference<a class="headerlink" href="#reference" title="Permalink to this headline">¶</a></h2>
524
<div class="section" id="functions">
525
<span id="elementtree-functions"></span><h3>20.5.3.1. Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3>
526
<dl class="function">
527
<dt id="xml.etree.ElementTree.Comment">
528
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">Comment</code><span class="sig-paren">(</span><em>text=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Comment" title="Permalink to this definition">¶</a></dt>
529
<dd><p>Comment element factory. This factory function creates a special element
530
that will be serialized as an XML comment by the standard serializer. The
531
comment string can be either a bytestring or a Unicode string. <em>text</em> is a
532
string containing the comment string. Returns an element instance
533
representing a comment.</p>
534
<p>Note that <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> skips over comments in the input
535
instead of creating comment objects for them. An <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal"><span class="pre">ElementTree</span></code></a> will
536
only contain comment nodes if they have been inserted into to
537
the tree using one of the <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> methods.</p>
540
<dl class="function">
541
<dt id="xml.etree.ElementTree.dump">
542
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">dump</code><span class="sig-paren">(</span><em>elem</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.dump" title="Permalink to this definition">¶</a></dt>
543
<dd><p>Writes an element tree or element structure to sys.stdout. This function
544
should be used for debugging only.</p>
545
<p>The exact output format is implementation dependent. In this version, it’s
546
written as an ordinary XML file.</p>
547
<p><em>elem</em> is an element tree or an individual element.</p>
550
<dl class="function">
551
<dt id="xml.etree.ElementTree.fromstring">
552
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">fromstring</code><span class="sig-paren">(</span><em>text</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.fromstring" title="Permalink to this definition">¶</a></dt>
553
<dd><p>Parses an XML section from a string constant. Same as <a class="reference internal" href="#xml.etree.ElementTree.XML" title="xml.etree.ElementTree.XML"><code class="xref py py-func docutils literal"><span class="pre">XML()</span></code></a>. <em>text</em>
554
is a string containing XML data. Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> instance.</p>
557
<dl class="function">
558
<dt id="xml.etree.ElementTree.fromstringlist">
559
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">fromstringlist</code><span class="sig-paren">(</span><em>sequence</em>, <em>parser=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.fromstringlist" title="Permalink to this definition">¶</a></dt>
560
<dd><p>Parses an XML document from a sequence of string fragments. <em>sequence</em> is a
561
list or other sequence containing XML data fragments. <em>parser</em> is an
562
optional parser instance. If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a>
563
parser is used. Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> instance.</p>
564
<div class="versionadded">
565
<p><span class="versionmodified">New in version 3.2.</span></p>
569
<dl class="function">
570
<dt id="xml.etree.ElementTree.iselement">
571
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">iselement</code><span class="sig-paren">(</span><em>element</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.iselement" title="Permalink to this definition">¶</a></dt>
572
<dd><p>Checks if an object appears to be a valid element object. <em>element</em> is an
573
element instance. Returns a true value if this is an element object.</p>
576
<dl class="function">
577
<dt id="xml.etree.ElementTree.iterparse">
578
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">iterparse</code><span class="sig-paren">(</span><em>source</em>, <em>events=None</em>, <em>parser=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.iterparse" title="Permalink to this definition">¶</a></dt>
579
<dd><p>Parses an XML section into an element tree incrementally, and reports what’s
580
going on to the user. <em>source</em> is a filename or <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>
581
containing XML data. <em>events</em> is a sequence of events to report back. The
582
supported events are the strings <code class="docutils literal"><span class="pre">"start"</span></code>, <code class="docutils literal"><span class="pre">"end"</span></code>, <code class="docutils literal"><span class="pre">"start-ns"</span></code> and
583
<code class="docutils literal"><span class="pre">"end-ns"</span></code> (the “ns” events are used to get detailed namespace
584
information). If <em>events</em> is omitted, only <code class="docutils literal"><span class="pre">"end"</span></code> events are reported.
585
<em>parser</em> is an optional parser instance. If not given, the standard
586
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> parser is used. <em>parser</em> must be a subclass of
587
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> and can only use the default <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></code></a> as a
588
target. Returns an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> providing <code class="docutils literal"><span class="pre">(event,</span> <span class="pre">elem)</span></code> pairs.</p>
589
<p>Note that while <a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><code class="xref py py-func docutils literal"><span class="pre">iterparse()</span></code></a> builds the tree incrementally, it issues
590
blocking reads on <em>source</em> (or the file it names). As such, it’s unsuitable
591
for applications where blocking reads can’t be made. For fully non-blocking
592
parsing, see <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></code></a>.</p>
593
<div class="admonition note">
594
<p class="first admonition-title">Note</p>
595
<p><a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><code class="xref py py-func docutils literal"><span class="pre">iterparse()</span></code></a> only guarantees that it has seen the “>” character of a
596
starting tag when it emits a “start” event, so the attributes are defined,
597
but the contents of the text and tail attributes are undefined at that
598
point. The same applies to the element children; they may or may not be
600
<p class="last">If you need a fully populated element, look for “end” events instead.</p>
602
<div class="deprecated">
603
<p><span class="versionmodified">Deprecated since version 3.4: </span>The <em>parser</em> argument.</p>
607
<dl class="function">
608
<dt id="xml.etree.ElementTree.parse">
609
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">parse</code><span class="sig-paren">(</span><em>source</em>, <em>parser=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.parse" title="Permalink to this definition">¶</a></dt>
610
<dd><p>Parses an XML section into an element tree. <em>source</em> is a filename or file
611
object containing XML data. <em>parser</em> is an optional parser instance. If
612
not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> parser is used. Returns an
613
<a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal"><span class="pre">ElementTree</span></code></a> instance.</p>
616
<dl class="function">
617
<dt id="xml.etree.ElementTree.ProcessingInstruction">
618
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">ProcessingInstruction</code><span class="sig-paren">(</span><em>target</em>, <em>text=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ProcessingInstruction" title="Permalink to this definition">¶</a></dt>
619
<dd><p>PI element factory. This factory function creates a special element that
620
will be serialized as an XML processing instruction. <em>target</em> is a string
621
containing the PI target. <em>text</em> is a string containing the PI contents, if
622
given. Returns an element instance, representing a processing instruction.</p>
623
<p>Note that <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> skips over processing instructions
624
in the input instead of creating comment objects for them. An
625
<a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal"><span class="pre">ElementTree</span></code></a> will only contain processing instruction nodes if
626
they have been inserted into to the tree using one of the
627
<a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> methods.</p>
630
<dl class="function">
631
<dt id="xml.etree.ElementTree.register_namespace">
632
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">register_namespace</code><span class="sig-paren">(</span><em>prefix</em>, <em>uri</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.register_namespace" title="Permalink to this definition">¶</a></dt>
633
<dd><p>Registers a namespace prefix. The registry is global, and any existing
634
mapping for either the given prefix or the namespace URI will be removed.
635
<em>prefix</em> is a namespace prefix. <em>uri</em> is a namespace uri. Tags and
636
attributes in this namespace will be serialized with the given prefix, if at
638
<div class="versionadded">
639
<p><span class="versionmodified">New in version 3.2.</span></p>
643
<dl class="function">
644
<dt id="xml.etree.ElementTree.SubElement">
645
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">SubElement</code><span class="sig-paren">(</span><em>parent</em>, <em>tag</em>, <em>attrib={}</em>, <em>**extra</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.SubElement" title="Permalink to this definition">¶</a></dt>
646
<dd><p>Subelement factory. This function creates an element instance, and appends
647
it to an existing element.</p>
648
<p>The element name, attribute names, and attribute values can be either
649
bytestrings or Unicode strings. <em>parent</em> is the parent element. <em>tag</em> is
650
the subelement name. <em>attrib</em> is an optional dictionary, containing element
651
attributes. <em>extra</em> contains additional attributes, given as keyword
652
arguments. Returns an element instance.</p>
655
<dl class="function">
656
<dt id="xml.etree.ElementTree.tostring">
657
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">tostring</code><span class="sig-paren">(</span><em>element</em>, <em>encoding="us-ascii"</em>, <em>method="xml"</em>, <em>*</em>, <em>short_empty_elements=True</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.tostring" title="Permalink to this definition">¶</a></dt>
658
<dd><p>Generates a string representation of an XML element, including all
659
subelements. <em>element</em> is an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> instance. <em>encoding</em> <a class="footnote-reference" href="#id5" id="id1">[1]</a> is
660
the output encoding (default is US-ASCII). Use <code class="docutils literal"><span class="pre">encoding="unicode"</span></code> to
661
generate a Unicode string (otherwise, a bytestring is generated). <em>method</em>
662
is either <code class="docutils literal"><span class="pre">"xml"</span></code>, <code class="docutils literal"><span class="pre">"html"</span></code> or <code class="docutils literal"><span class="pre">"text"</span></code> (default is <code class="docutils literal"><span class="pre">"xml"</span></code>).
663
<em>short_empty_elements</em> has the same meaning as in <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><code class="xref py py-meth docutils literal"><span class="pre">ElementTree.write()</span></code></a>.
664
Returns an (optionally) encoded string containing the XML data.</p>
665
<div class="versionadded">
666
<p><span class="versionmodified">New in version 3.4: </span>The <em>short_empty_elements</em> parameter.</p>
670
<dl class="function">
671
<dt id="xml.etree.ElementTree.tostringlist">
672
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">tostringlist</code><span class="sig-paren">(</span><em>element</em>, <em>encoding="us-ascii"</em>, <em>method="xml"</em>, <em>*</em>, <em>short_empty_elements=True</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.tostringlist" title="Permalink to this definition">¶</a></dt>
673
<dd><p>Generates a string representation of an XML element, including all
674
subelements. <em>element</em> is an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> instance. <em>encoding</em> <a class="footnote-reference" href="#id5" id="id2">[1]</a> is
675
the output encoding (default is US-ASCII). Use <code class="docutils literal"><span class="pre">encoding="unicode"</span></code> to
676
generate a Unicode string (otherwise, a bytestring is generated). <em>method</em>
677
is either <code class="docutils literal"><span class="pre">"xml"</span></code>, <code class="docutils literal"><span class="pre">"html"</span></code> or <code class="docutils literal"><span class="pre">"text"</span></code> (default is <code class="docutils literal"><span class="pre">"xml"</span></code>).
678
<em>short_empty_elements</em> has the same meaning as in <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><code class="xref py py-meth docutils literal"><span class="pre">ElementTree.write()</span></code></a>.
679
Returns a list of (optionally) encoded strings containing the XML data.
680
It does not guarantee any specific sequence, except that
681
<code class="docutils literal"><span class="pre">b"".join(tostringlist(element))</span> <span class="pre">==</span> <span class="pre">tostring(element)</span></code>.</p>
682
<div class="versionadded">
683
<p><span class="versionmodified">New in version 3.2.</span></p>
685
<div class="versionadded">
686
<p><span class="versionmodified">New in version 3.4: </span>The <em>short_empty_elements</em> parameter.</p>
690
<dl class="function">
691
<dt id="xml.etree.ElementTree.XML">
692
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">XML</code><span class="sig-paren">(</span><em>text</em>, <em>parser=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XML" title="Permalink to this definition">¶</a></dt>
693
<dd><p>Parses an XML section from a string constant. This function can be used to
694
embed “XML literals” in Python code. <em>text</em> is a string containing XML
695
data. <em>parser</em> is an optional parser instance. If not given, the standard
696
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> parser is used. Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> instance.</p>
699
<dl class="function">
700
<dt id="xml.etree.ElementTree.XMLID">
701
<code class="descclassname">xml.etree.ElementTree.</code><code class="descname">XMLID</code><span class="sig-paren">(</span><em>text</em>, <em>parser=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLID" title="Permalink to this definition">¶</a></dt>
702
<dd><p>Parses an XML section from a string constant, and also returns a dictionary
703
which maps from element id:s to elements. <em>text</em> is a string containing XML
704
data. <em>parser</em> is an optional parser instance. If not given, the standard
705
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> parser is used. Returns a tuple containing an
706
<a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> instance and a dictionary.</p>
710
<div class="section" id="element-objects">
711
<span id="elementtree-element-objects"></span><h3>20.5.3.2. Element Objects<a class="headerlink" href="#element-objects" title="Permalink to this headline">¶</a></h3>
713
<dt id="xml.etree.ElementTree.Element">
714
<em class="property">class </em><code class="descclassname">xml.etree.ElementTree.</code><code class="descname">Element</code><span class="sig-paren">(</span><em>tag</em>, <em>attrib={}</em>, <em>**extra</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element" title="Permalink to this definition">¶</a></dt>
715
<dd><p>Element class. This class defines the Element interface, and provides a
716
reference implementation of this interface.</p>
717
<p>The element name, attribute names, and attribute values can be either
718
bytestrings or Unicode strings. <em>tag</em> is the element name. <em>attrib</em> is
719
an optional dictionary, containing element attributes. <em>extra</em> contains
720
additional attributes, given as keyword arguments.</p>
721
<dl class="attribute">
722
<dt id="xml.etree.ElementTree.Element.tag">
723
<code class="descname">tag</code><a class="headerlink" href="#xml.etree.ElementTree.Element.tag" title="Permalink to this definition">¶</a></dt>
724
<dd><p>A string identifying what kind of data this element represents (the
725
element type, in other words).</p>
728
<dl class="attribute">
729
<dt id="xml.etree.ElementTree.Element.text">
730
<code class="descname">text</code><a class="headerlink" href="#xml.etree.ElementTree.Element.text" title="Permalink to this definition">¶</a></dt>
731
<dt id="xml.etree.ElementTree.Element.tail">
732
<code class="descname">tail</code><a class="headerlink" href="#xml.etree.ElementTree.Element.tail" title="Permalink to this definition">¶</a></dt>
733
<dd><p>These attributes can be used to hold additional data associated with
734
the element. Their values are usually strings but may be any
735
application-specific object. If the element is created from
736
an XML file, the <em>text</em> attribute holds either the text between
737
the element’s start tag and its first child or end tag, or <code class="docutils literal"><span class="pre">None</span></code>, and
738
the <em>tail</em> attribute holds either the text between the element’s
739
end tag and the next tag, or <code class="docutils literal"><span class="pre">None</span></code>. For the XML data</p>
740
<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="nt"><a><b></span>1<span class="nt"><c></span>2<span class="nt"><d/></span>3<span class="nt"></c></b></span>4<span class="nt"></a></span>
743
<p>the <em>a</em> element has <code class="docutils literal"><span class="pre">None</span></code> for both <em>text</em> and <em>tail</em> attributes,
744
the <em>b</em> element has <em>text</em> <code class="docutils literal"><span class="pre">"1"</span></code> and <em>tail</em> <code class="docutils literal"><span class="pre">"4"</span></code>,
745
the <em>c</em> element has <em>text</em> <code class="docutils literal"><span class="pre">"2"</span></code> and <em>tail</em> <code class="docutils literal"><span class="pre">None</span></code>,
746
and the <em>d</em> element has <em>text</em> <code class="docutils literal"><span class="pre">None</span></code> and <em>tail</em> <code class="docutils literal"><span class="pre">"3"</span></code>.</p>
747
<p>To collect the inner text of an element, see <a class="reference internal" href="#xml.etree.ElementTree.Element.itertext" title="xml.etree.ElementTree.Element.itertext"><code class="xref py py-meth docutils literal"><span class="pre">itertext()</span></code></a>, for
748
example <code class="docutils literal"><span class="pre">"".join(element.itertext())</span></code>.</p>
749
<p>Applications may store arbitrary objects in these attributes.</p>
752
<dl class="attribute">
753
<dt id="xml.etree.ElementTree.Element.attrib">
754
<code class="descname">attrib</code><a class="headerlink" href="#xml.etree.ElementTree.Element.attrib" title="Permalink to this definition">¶</a></dt>
755
<dd><p>A dictionary containing the element’s attributes. Note that while the
756
<em>attrib</em> value is always a real mutable Python dictionary, an ElementTree
757
implementation may choose to use another internal representation, and
758
create the dictionary only if someone asks for it. To take advantage of
759
such implementations, use the dictionary methods below whenever possible.</p>
762
<p>The following dictionary-like methods work on the element attributes.</p>
764
<dt id="xml.etree.ElementTree.Element.clear">
765
<code class="descname">clear</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.clear" title="Permalink to this definition">¶</a></dt>
766
<dd><p>Resets an element. This function removes all subelements, clears all
767
attributes, and sets the text and tail attributes to <code class="docutils literal"><span class="pre">None</span></code>.</p>
771
<dt id="xml.etree.ElementTree.Element.get">
772
<code class="descname">get</code><span class="sig-paren">(</span><em>key</em>, <em>default=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.get" title="Permalink to this definition">¶</a></dt>
773
<dd><p>Gets the element attribute named <em>key</em>.</p>
774
<p>Returns the attribute value, or <em>default</em> if the attribute was not found.</p>
778
<dt id="xml.etree.ElementTree.Element.items">
779
<code class="descname">items</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.items" title="Permalink to this definition">¶</a></dt>
780
<dd><p>Returns the element attributes as a sequence of (name, value) pairs. The
781
attributes are returned in an arbitrary order.</p>
785
<dt id="xml.etree.ElementTree.Element.keys">
786
<code class="descname">keys</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.keys" title="Permalink to this definition">¶</a></dt>
787
<dd><p>Returns the elements attribute names as a list. The names are returned
788
in an arbitrary order.</p>
792
<dt id="xml.etree.ElementTree.Element.set">
793
<code class="descname">set</code><span class="sig-paren">(</span><em>key</em>, <em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.set" title="Permalink to this definition">¶</a></dt>
794
<dd><p>Set the attribute <em>key</em> on the element to <em>value</em>.</p>
797
<p>The following methods work on the element’s children (subelements).</p>
799
<dt id="xml.etree.ElementTree.Element.append">
800
<code class="descname">append</code><span class="sig-paren">(</span><em>subelement</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.append" title="Permalink to this definition">¶</a></dt>
801
<dd><p>Adds the element <em>subelement</em> to the end of this element’s internal list
802
of subelements. Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> if <em>subelement</em> is not an
803
<a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a>.</p>
807
<dt id="xml.etree.ElementTree.Element.extend">
808
<code class="descname">extend</code><span class="sig-paren">(</span><em>subelements</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.extend" title="Permalink to this definition">¶</a></dt>
809
<dd><p>Appends <em>subelements</em> from a sequence object with zero or more elements.
810
Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> if a subelement is not an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a>.</p>
811
<div class="versionadded">
812
<p><span class="versionmodified">New in version 3.2.</span></p>
817
<dt id="xml.etree.ElementTree.Element.find">
818
<code class="descname">find</code><span class="sig-paren">(</span><em>match</em>, <em>namespaces=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.find" title="Permalink to this definition">¶</a></dt>
819
<dd><p>Finds the first subelement matching <em>match</em>. <em>match</em> may be a tag name
820
or a <a class="reference internal" href="#elementtree-xpath"><span>path</span></a>. Returns an element instance
821
or <code class="docutils literal"><span class="pre">None</span></code>. <em>namespaces</em> is an optional mapping from namespace prefix
826
<dt id="xml.etree.ElementTree.Element.findall">
827
<code class="descname">findall</code><span class="sig-paren">(</span><em>match</em>, <em>namespaces=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.findall" title="Permalink to this definition">¶</a></dt>
828
<dd><p>Finds all matching subelements, by tag name or
829
<a class="reference internal" href="#elementtree-xpath"><span>path</span></a>. Returns a list containing all matching
830
elements in document order. <em>namespaces</em> is an optional mapping from
831
namespace prefix to full name.</p>
835
<dt id="xml.etree.ElementTree.Element.findtext">
836
<code class="descname">findtext</code><span class="sig-paren">(</span><em>match</em>, <em>default=None</em>, <em>namespaces=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.findtext" title="Permalink to this definition">¶</a></dt>
837
<dd><p>Finds text for the first subelement matching <em>match</em>. <em>match</em> may be
838
a tag name or a <a class="reference internal" href="#elementtree-xpath"><span>path</span></a>. Returns the text content
839
of the first matching element, or <em>default</em> if no element was found.
840
Note that if the matching element has no text content an empty string
841
is returned. <em>namespaces</em> is an optional mapping from namespace prefix
846
<dt id="xml.etree.ElementTree.Element.getchildren">
847
<code class="descname">getchildren</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.getchildren" title="Permalink to this definition">¶</a></dt>
848
<dd><div class="deprecated">
849
<p><span class="versionmodified">Deprecated since version 3.2: </span>Use <code class="docutils literal"><span class="pre">list(elem)</span></code> or iteration.</p>
854
<dt id="xml.etree.ElementTree.Element.getiterator">
855
<code class="descname">getiterator</code><span class="sig-paren">(</span><em>tag=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.getiterator" title="Permalink to this definition">¶</a></dt>
856
<dd><div class="deprecated">
857
<p><span class="versionmodified">Deprecated since version 3.2: </span>Use method <a class="reference internal" href="#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><code class="xref py py-meth docutils literal"><span class="pre">Element.iter()</span></code></a> instead.</p>
862
<dt id="xml.etree.ElementTree.Element.insert">
863
<code class="descname">insert</code><span class="sig-paren">(</span><em>index</em>, <em>subelement</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.insert" title="Permalink to this definition">¶</a></dt>
864
<dd><p>Inserts <em>subelement</em> at the given position in this element. Raises
865
<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> if <em>subelement</em> is not an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a>.</p>
869
<dt id="xml.etree.ElementTree.Element.iter">
870
<code class="descname">iter</code><span class="sig-paren">(</span><em>tag=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.iter" title="Permalink to this definition">¶</a></dt>
871
<dd><p>Creates a tree <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> with the current element as the root.
872
The iterator iterates over this element and all elements below it, in
873
document (depth first) order. If <em>tag</em> is not <code class="docutils literal"><span class="pre">None</span></code> or <code class="docutils literal"><span class="pre">'*'</span></code>, only
874
elements whose tag equals <em>tag</em> are returned from the iterator. If the
875
tree structure is modified during iteration, the result is undefined.</p>
876
<div class="versionadded">
877
<p><span class="versionmodified">New in version 3.2.</span></p>
882
<dt id="xml.etree.ElementTree.Element.iterfind">
883
<code class="descname">iterfind</code><span class="sig-paren">(</span><em>match</em>, <em>namespaces=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.iterfind" title="Permalink to this definition">¶</a></dt>
884
<dd><p>Finds all matching subelements, by tag name or
885
<a class="reference internal" href="#elementtree-xpath"><span>path</span></a>. Returns an iterable yielding all
886
matching elements in document order. <em>namespaces</em> is an optional mapping
887
from namespace prefix to full name.</p>
888
<div class="versionadded">
889
<p><span class="versionmodified">New in version 3.2.</span></p>
894
<dt id="xml.etree.ElementTree.Element.itertext">
895
<code class="descname">itertext</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.itertext" title="Permalink to this definition">¶</a></dt>
896
<dd><p>Creates a text iterator. The iterator loops over this element and all
897
subelements, in document order, and returns all inner text.</p>
898
<div class="versionadded">
899
<p><span class="versionmodified">New in version 3.2.</span></p>
904
<dt id="xml.etree.ElementTree.Element.makeelement">
905
<code class="descname">makeelement</code><span class="sig-paren">(</span><em>tag</em>, <em>attrib</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.makeelement" title="Permalink to this definition">¶</a></dt>
906
<dd><p>Creates a new element object of the same type as this element. Do not
907
call this method, use the <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><code class="xref py py-func docutils literal"><span class="pre">SubElement()</span></code></a> factory function instead.</p>
911
<dt id="xml.etree.ElementTree.Element.remove">
912
<code class="descname">remove</code><span class="sig-paren">(</span><em>subelement</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.remove" title="Permalink to this definition">¶</a></dt>
913
<dd><p>Removes <em>subelement</em> from the element. Unlike the find* methods this
914
method compares elements based on the instance identity, not on tag value
918
<p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> objects also support the following sequence type methods
919
for working with subelements: <a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal"><span class="pre">__delitem__()</span></code></a>,
920
<a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></code></a>,
921
<a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal"><span class="pre">__len__()</span></code></a>.</p>
922
<p>Caution: Elements with no subelements will test as <code class="docutils literal"><span class="pre">False</span></code>. This behavior
923
will change in future versions. Use specific <code class="docutils literal"><span class="pre">len(elem)</span></code> or <code class="docutils literal"><span class="pre">elem</span> <span class="pre">is</span>
924
<span class="pre">None</span></code> test instead.</p>
925
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">)</span>
927
<span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span> <span class="c1"># careful!</span>
928
<span class="nb">print</span><span class="p">(</span><span class="s2">"element not found, or element has no subelements"</span><span class="p">)</span>
930
<span class="k">if</span> <span class="n">element</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
931
<span class="nb">print</span><span class="p">(</span><span class="s2">"element not found"</span><span class="p">)</span>
937
<div class="section" id="elementtree-objects">
938
<span id="elementtree-elementtree-objects"></span><h3>20.5.3.3. ElementTree Objects<a class="headerlink" href="#elementtree-objects" title="Permalink to this headline">¶</a></h3>
940
<dt id="xml.etree.ElementTree.ElementTree">
941
<em class="property">class </em><code class="descclassname">xml.etree.ElementTree.</code><code class="descname">ElementTree</code><span class="sig-paren">(</span><em>element=None</em>, <em>file=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree" title="Permalink to this definition">¶</a></dt>
942
<dd><p>ElementTree wrapper class. This class represents an entire element
943
hierarchy, and adds some extra support for serialization to and from
945
<p><em>element</em> is the root element. The tree is initialized with the contents
946
of the XML <em>file</em> if given.</p>
948
<dt id="xml.etree.ElementTree.ElementTree._setroot">
949
<code class="descname">_setroot</code><span class="sig-paren">(</span><em>element</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree._setroot" title="Permalink to this definition">¶</a></dt>
950
<dd><p>Replaces the root element for this tree. This discards the current
951
contents of the tree, and replaces it with the given element. Use with
952
care. <em>element</em> is an element instance.</p>
956
<dt id="xml.etree.ElementTree.ElementTree.find">
957
<code class="descname">find</code><span class="sig-paren">(</span><em>match</em>, <em>namespaces=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.find" title="Permalink to this definition">¶</a></dt>
958
<dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><code class="xref py py-meth docutils literal"><span class="pre">Element.find()</span></code></a>, starting at the root of the tree.</p>
962
<dt id="xml.etree.ElementTree.ElementTree.findall">
963
<code class="descname">findall</code><span class="sig-paren">(</span><em>match</em>, <em>namespaces=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findall" title="Permalink to this definition">¶</a></dt>
964
<dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.findall" title="xml.etree.ElementTree.Element.findall"><code class="xref py py-meth docutils literal"><span class="pre">Element.findall()</span></code></a>, starting at the root of the tree.</p>
968
<dt id="xml.etree.ElementTree.ElementTree.findtext">
969
<code class="descname">findtext</code><span class="sig-paren">(</span><em>match</em>, <em>default=None</em>, <em>namespaces=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findtext" title="Permalink to this definition">¶</a></dt>
970
<dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.findtext" title="xml.etree.ElementTree.Element.findtext"><code class="xref py py-meth docutils literal"><span class="pre">Element.findtext()</span></code></a>, starting at the root of the tree.</p>
974
<dt id="xml.etree.ElementTree.ElementTree.getiterator">
975
<code class="descname">getiterator</code><span class="sig-paren">(</span><em>tag=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.getiterator" title="Permalink to this definition">¶</a></dt>
976
<dd><div class="deprecated">
977
<p><span class="versionmodified">Deprecated since version 3.2: </span>Use method <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.iter" title="xml.etree.ElementTree.ElementTree.iter"><code class="xref py py-meth docutils literal"><span class="pre">ElementTree.iter()</span></code></a> instead.</p>
982
<dt id="xml.etree.ElementTree.ElementTree.getroot">
983
<code class="descname">getroot</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.getroot" title="Permalink to this definition">¶</a></dt>
984
<dd><p>Returns the root element for this tree.</p>
988
<dt id="xml.etree.ElementTree.ElementTree.iter">
989
<code class="descname">iter</code><span class="sig-paren">(</span><em>tag=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iter" title="Permalink to this definition">¶</a></dt>
990
<dd><p>Creates and returns a tree iterator for the root element. The iterator
991
loops over all elements in this tree, in section order. <em>tag</em> is the tag
992
to look for (default is to return all elements).</p>
996
<dt id="xml.etree.ElementTree.ElementTree.iterfind">
997
<code class="descname">iterfind</code><span class="sig-paren">(</span><em>match</em>, <em>namespaces=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iterfind" title="Permalink to this definition">¶</a></dt>
998
<dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.iterfind" title="xml.etree.ElementTree.Element.iterfind"><code class="xref py py-meth docutils literal"><span class="pre">Element.iterfind()</span></code></a>, starting at the root of the tree.</p>
999
<div class="versionadded">
1000
<p><span class="versionmodified">New in version 3.2.</span></p>
1005
<dt id="xml.etree.ElementTree.ElementTree.parse">
1006
<code class="descname">parse</code><span class="sig-paren">(</span><em>source</em>, <em>parser=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.parse" title="Permalink to this definition">¶</a></dt>
1007
<dd><p>Loads an external XML section into this element tree. <em>source</em> is a file
1008
name or <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>. <em>parser</em> is an optional parser instance.
1009
If not given, the standard <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> parser is used. Returns the
1010
section root element.</p>
1014
<dt id="xml.etree.ElementTree.ElementTree.write">
1015
<code class="descname">write</code><span class="sig-paren">(</span><em>file</em>, <em>encoding="us-ascii"</em>, <em>xml_declaration=None</em>, <em>default_namespace=None</em>, <em>method="xml"</em>, <em>*</em>, <em>short_empty_elements=True</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.write" title="Permalink to this definition">¶</a></dt>
1016
<dd><p>Writes the element tree to a file, as XML. <em>file</em> is a file name, or a
1017
<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> opened for writing. <em>encoding</em> <a class="footnote-reference" href="#id5" id="id3">[1]</a> is the output
1018
encoding (default is US-ASCII).
1019
<em>xml_declaration</em> controls if an XML declaration should be added to the
1020
file. Use <code class="docutils literal"><span class="pre">False</span></code> for never, <code class="docutils literal"><span class="pre">True</span></code> for always, <code class="docutils literal"><span class="pre">None</span></code>
1021
for only if not US-ASCII or UTF-8 or Unicode (default is <code class="docutils literal"><span class="pre">None</span></code>).
1022
<em>default_namespace</em> sets the default XML namespace (for “xmlns”).
1023
<em>method</em> is either <code class="docutils literal"><span class="pre">"xml"</span></code>, <code class="docutils literal"><span class="pre">"html"</span></code> or <code class="docutils literal"><span class="pre">"text"</span></code> (default is
1024
<code class="docutils literal"><span class="pre">"xml"</span></code>).
1025
The keyword-only <em>short_empty_elements</em> parameter controls the formatting
1026
of elements that contain no content. If <code class="docutils literal"><span class="pre">True</span></code> (the default), they are
1027
emitted as a single self-closed tag, otherwise they are emitted as a pair
1028
of start/end tags.</p>
1029
<p>The output is either a string (<a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a>) or binary (<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>).
1030
This is controlled by the <em>encoding</em> argument. If <em>encoding</em> is
1031
<code class="docutils literal"><span class="pre">"unicode"</span></code>, the output is a string; otherwise, it’s binary. Note that
1032
this may conflict with the type of <em>file</em> if it’s an open
1033
<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>; make sure you do not try to write a string to a
1034
binary stream and vice versa.</p>
1035
<div class="versionadded">
1036
<p><span class="versionmodified">New in version 3.4: </span>The <em>short_empty_elements</em> parameter.</p>
1042
<p>This is the XML file that is going to be manipulated:</p>
1043
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">html</span><span class="o">></span>
1044
<span class="o"><</span><span class="n">head</span><span class="o">></span>
1045
<span class="o"><</span><span class="n">title</span><span class="o">></span><span class="n">Example</span> <span class="n">page</span><span class="o"></</span><span class="n">title</span><span class="o">></span>
1046
<span class="o"></</span><span class="n">head</span><span class="o">></span>
1047
<span class="o"><</span><span class="n">body</span><span class="o">></span>
1048
<span class="o"><</span><span class="n">p</span><span class="o">></span><span class="n">Moved</span> <span class="n">to</span> <span class="o"><</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s2">"http://example.org/"</span><span class="o">></span><span class="n">example</span><span class="o">.</span><span class="n">org</span><span class="o"></</span><span class="n">a</span><span class="o">></span>
1049
<span class="ow">or</span> <span class="o"><</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s2">"http://example.com/"</span><span class="o">></span><span class="n">example</span><span class="o">.</span><span class="n">com</span><span class="o"></</span><span class="n">a</span><span class="o">>.</</span><span class="n">p</span><span class="o">></span>
1050
<span class="o"></</span><span class="n">body</span><span class="o">></span>
1051
<span class="o"></</span><span class="n">html</span><span class="o">></span>
1054
<p>Example of changing the attribute “target” of every link in first paragraph:</p>
1055
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">import</span> <span class="n">ElementTree</span>
1056
<span class="gp">>>> </span><span class="n">tree</span> <span class="o">=</span> <span class="n">ElementTree</span><span class="p">()</span>
1057
<span class="gp">>>> </span><span class="n">tree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">"index.xhtml"</span><span class="p">)</span>
1058
<span class="go"><Element 'html' at 0xb77e6fac></span>
1059
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s2">"body/p"</span><span class="p">)</span> <span class="c1"># Finds first occurrence of tag p in body</span>
1060
<span class="gp">>>> </span><span class="n">p</span>
1061
<span class="go"><Element 'p' at 0xb77ec26c></span>
1062
<span class="gp">>>> </span><span class="n">links</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">iter</span><span class="p">(</span><span class="s2">"a"</span><span class="p">))</span> <span class="c1"># Returns list of all links</span>
1063
<span class="gp">>>> </span><span class="n">links</span>
1064
<span class="go">[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]</span>
1065
<span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">links</span><span class="p">:</span> <span class="c1"># Iterates through all found links</span>
1066
<span class="gp">... </span> <span class="n">i</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s2">"target"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"blank"</span>
1067
<span class="gp">>>> </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"output.xhtml"</span><span class="p">)</span>
1071
<div class="section" id="qname-objects">
1072
<span id="elementtree-qname-objects"></span><h3>20.5.3.4. QName Objects<a class="headerlink" href="#qname-objects" title="Permalink to this headline">¶</a></h3>
1074
<dt id="xml.etree.ElementTree.QName">
1075
<em class="property">class </em><code class="descclassname">xml.etree.ElementTree.</code><code class="descname">QName</code><span class="sig-paren">(</span><em>text_or_uri</em>, <em>tag=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.QName" title="Permalink to this definition">¶</a></dt>
1076
<dd><p>QName wrapper. This can be used to wrap a QName attribute value, in order
1077
to get proper namespace handling on output. <em>text_or_uri</em> is a string
1078
containing the QName value, in the form {uri}local, or, if the tag argument
1079
is given, the URI part of a QName. If <em>tag</em> is given, the first argument is
1080
interpreted as a URI, and this argument is interpreted as a local name.
1081
<a class="reference internal" href="#xml.etree.ElementTree.QName" title="xml.etree.ElementTree.QName"><code class="xref py py-class docutils literal"><span class="pre">QName</span></code></a> instances are opaque.</p>
1085
<div class="section" id="treebuilder-objects">
1086
<span id="elementtree-treebuilder-objects"></span><h3>20.5.3.5. TreeBuilder Objects<a class="headerlink" href="#treebuilder-objects" title="Permalink to this headline">¶</a></h3>
1088
<dt id="xml.etree.ElementTree.TreeBuilder">
1089
<em class="property">class </em><code class="descclassname">xml.etree.ElementTree.</code><code class="descname">TreeBuilder</code><span class="sig-paren">(</span><em>element_factory=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder" title="Permalink to this definition">¶</a></dt>
1090
<dd><p>Generic element structure builder. This builder converts a sequence of
1091
start, data, and end method calls to a well-formed element structure. You
1092
can use this class to build an element structure using a custom XML parser,
1093
or a parser for some other XML-like format. <em>element_factory</em>, when given,
1094
must be a callable accepting two positional arguments: a tag and
1095
a dict of attributes. It is expected to return a new element instance.</p>
1097
<dt id="xml.etree.ElementTree.TreeBuilder.close">
1098
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.close" title="Permalink to this definition">¶</a></dt>
1099
<dd><p>Flushes the builder buffers, and returns the toplevel document
1100
element. Returns an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> instance.</p>
1104
<dt id="xml.etree.ElementTree.TreeBuilder.data">
1105
<code class="descname">data</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.data" title="Permalink to this definition">¶</a></dt>
1106
<dd><p>Adds text to the current element. <em>data</em> is a string. This should be
1107
either a bytestring, or a Unicode string.</p>
1111
<dt id="xml.etree.ElementTree.TreeBuilder.end">
1112
<code class="descname">end</code><span class="sig-paren">(</span><em>tag</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.end" title="Permalink to this definition">¶</a></dt>
1113
<dd><p>Closes the current element. <em>tag</em> is the element name. Returns the
1118
<dt id="xml.etree.ElementTree.TreeBuilder.start">
1119
<code class="descname">start</code><span class="sig-paren">(</span><em>tag</em>, <em>attrs</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.start" title="Permalink to this definition">¶</a></dt>
1120
<dd><p>Opens a new element. <em>tag</em> is the element name. <em>attrs</em> is a dictionary
1121
containing element attributes. Returns the opened element.</p>
1124
<p>In addition, a custom <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></code></a> object can provide the
1125
following method:</p>
1127
<dt id="xml.etree.ElementTree.TreeBuilder.doctype">
1128
<code class="descname">doctype</code><span class="sig-paren">(</span><em>name</em>, <em>pubid</em>, <em>system</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.doctype" title="Permalink to this definition">¶</a></dt>
1129
<dd><p>Handles a doctype declaration. <em>name</em> is the doctype name. <em>pubid</em> is
1130
the public identifier. <em>system</em> is the system identifier. This method
1131
does not exist on the default <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></code></a> class.</p>
1132
<div class="versionadded">
1133
<p><span class="versionmodified">New in version 3.2.</span></p>
1140
<div class="section" id="xmlparser-objects">
1141
<span id="elementtree-xmlparser-objects"></span><h3>20.5.3.6. XMLParser Objects<a class="headerlink" href="#xmlparser-objects" title="Permalink to this headline">¶</a></h3>
1143
<dt id="xml.etree.ElementTree.XMLParser">
1144
<em class="property">class </em><code class="descclassname">xml.etree.ElementTree.</code><code class="descname">XMLParser</code><span class="sig-paren">(</span><em>html=0</em>, <em>target=None</em>, <em>encoding=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser" title="Permalink to this definition">¶</a></dt>
1145
<dd><p>This class is the low-level building block of the module. It uses
1146
<a class="reference internal" href="pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal"><span class="pre">xml.parsers.expat</span></code></a> for efficient, event-based parsing of XML. It can
1147
be fed XML data incrementally with the <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.feed" title="xml.etree.ElementTree.XMLParser.feed"><code class="xref py py-meth docutils literal"><span class="pre">feed()</span></code></a> method, and parsing
1148
events are translated to a push API - by invoking callbacks on the <em>target</em>
1149
object. If <em>target</em> is omitted, the standard <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal"><span class="pre">TreeBuilder</span></code></a> is used.
1150
The <em>html</em> argument was historically used for backwards compatibility and is
1151
now deprecated. If <em>encoding</em> <a class="footnote-reference" href="#id5" id="id4">[1]</a> is given, the value overrides the
1152
encoding specified in the XML file.</p>
1153
<div class="deprecated">
1154
<p><span class="versionmodified">Deprecated since version 3.4: </span>The <em>html</em> argument. The remaining arguments should be passed via
1155
keyword to prepare for the removal of the <em>html</em> argument.</p>
1158
<dt id="xml.etree.ElementTree.XMLParser.close">
1159
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.close" title="Permalink to this definition">¶</a></dt>
1160
<dd><p>Finishes feeding data to the parser. Returns the result of calling the
1161
<code class="docutils literal"><span class="pre">close()</span></code> method of the <em>target</em> passed during construction; by default,
1162
this is the toplevel document element.</p>
1166
<dt id="xml.etree.ElementTree.XMLParser.doctype">
1167
<code class="descname">doctype</code><span class="sig-paren">(</span><em>name</em>, <em>pubid</em>, <em>system</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.doctype" title="Permalink to this definition">¶</a></dt>
1168
<dd><div class="deprecated">
1169
<p><span class="versionmodified">Deprecated since version 3.2: </span>Define the <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder.doctype" title="xml.etree.ElementTree.TreeBuilder.doctype"><code class="xref py py-meth docutils literal"><span class="pre">TreeBuilder.doctype()</span></code></a> method on a custom TreeBuilder
1175
<dt id="xml.etree.ElementTree.XMLParser.feed">
1176
<code class="descname">feed</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.feed" title="Permalink to this definition">¶</a></dt>
1177
<dd><p>Feeds data to the parser. <em>data</em> is encoded data.</p>
1180
<p><a class="reference internal" href="#xml.etree.ElementTree.XMLParser.feed" title="xml.etree.ElementTree.XMLParser.feed"><code class="xref py py-meth docutils literal"><span class="pre">XMLParser.feed()</span></code></a> calls <em>target</em>‘s <code class="docutils literal"><span class="pre">start(tag,</span> <span class="pre">attrs_dict)</span></code> method
1181
for each opening tag, its <code class="docutils literal"><span class="pre">end(tag)</span></code> method for each closing tag, and data
1182
is processed by method <code class="docutils literal"><span class="pre">data(data)</span></code>. <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.close" title="xml.etree.ElementTree.XMLParser.close"><code class="xref py py-meth docutils literal"><span class="pre">XMLParser.close()</span></code></a> calls
1183
<em>target</em>‘s method <code class="docutils literal"><span class="pre">close()</span></code>. <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> can be used not only for
1184
building a tree structure. This is an example of counting the maximum depth
1186
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">import</span> <span class="n">XMLParser</span>
1187
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">MaxDepth</span><span class="p">:</span> <span class="c1"># The target object of the parser</span>
1188
<span class="gp">... </span> <span class="n">maxDepth</span> <span class="o">=</span> <span class="mi">0</span>
1189
<span class="gp">... </span> <span class="n">depth</span> <span class="o">=</span> <span class="mi">0</span>
1190
<span class="gp">... </span> <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">attrib</span><span class="p">):</span> <span class="c1"># Called for each opening tag.</span>
1191
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">+=</span> <span class="mi">1</span>
1192
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">></span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span><span class="p">:</span>
1193
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span>
1194
<span class="gp">... </span> <span class="k">def</span> <span class="nf">end</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">):</span> <span class="c1"># Called for each closing tag.</span>
1195
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span> <span class="o">-=</span> <span class="mi">1</span>
1196
<span class="gp">... </span> <span class="k">def</span> <span class="nf">data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
1197
<span class="gp">... </span> <span class="k">pass</span> <span class="c1"># We do not need to do anything with data.</span>
1198
<span class="gp">... </span> <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c1"># Called when all data has been parsed.</span>
1199
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span>
1200
<span class="gp">...</span>
1201
<span class="gp">>>> </span><span class="n">target</span> <span class="o">=</span> <span class="n">MaxDepth</span><span class="p">()</span>
1202
<span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">XMLParser</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">target</span><span class="p">)</span>
1203
<span class="gp">>>> </span><span class="n">exampleXml</span> <span class="o">=</span> <span class="s2">"""</span>
1204
<span class="gp">... </span><span class="s2"><a></span>
1205
<span class="gp">... </span><span class="s2"> <b></span>
1206
<span class="gp">... </span><span class="s2"> </b></span>
1207
<span class="gp">... </span><span class="s2"> <b></span>
1208
<span class="gp">... </span><span class="s2"> <c></span>
1209
<span class="gp">... </span><span class="s2"> <d></span>
1210
<span class="gp">... </span><span class="s2"> </d></span>
1211
<span class="gp">... </span><span class="s2"> </c></span>
1212
<span class="gp">... </span><span class="s2"> </b></span>
1213
<span class="gp">... </span><span class="s2"></a>"""</span>
1214
<span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="n">exampleXml</span><span class="p">)</span>
1215
<span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
1216
<span class="go">4</span>
1222
<div class="section" id="xmlpullparser-objects">
1223
<span id="elementtree-xmlpullparser-objects"></span><h3>20.5.3.7. XMLPullParser Objects<a class="headerlink" href="#xmlpullparser-objects" title="Permalink to this headline">¶</a></h3>
1225
<dt id="xml.etree.ElementTree.XMLPullParser">
1226
<em class="property">class </em><code class="descclassname">xml.etree.ElementTree.</code><code class="descname">XMLPullParser</code><span class="sig-paren">(</span><em>events=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser" title="Permalink to this definition">¶</a></dt>
1227
<dd><p>A pull parser suitable for non-blocking applications. Its input-side API is
1228
similar to that of <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a>, but instead of pushing calls to a
1229
callback target, <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></code></a> collects an internal list of parsing
1230
events and lets the user read from it. <em>events</em> is a sequence of events to
1231
report back. The supported events are the strings <code class="docutils literal"><span class="pre">"start"</span></code>, <code class="docutils literal"><span class="pre">"end"</span></code>,
1232
<code class="docutils literal"><span class="pre">"start-ns"</span></code> and <code class="docutils literal"><span class="pre">"end-ns"</span></code> (the “ns” events are used to get detailed
1233
namespace information). If <em>events</em> is omitted, only <code class="docutils literal"><span class="pre">"end"</span></code> events are
1236
<dt id="xml.etree.ElementTree.XMLPullParser.feed">
1237
<code class="descname">feed</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.feed" title="Permalink to this definition">¶</a></dt>
1238
<dd><p>Feed the given bytes data to the parser.</p>
1242
<dt id="xml.etree.ElementTree.XMLPullParser.close">
1243
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.close" title="Permalink to this definition">¶</a></dt>
1244
<dd><p>Signal the parser that the data stream is terminated. Unlike
1245
<a class="reference internal" href="#xml.etree.ElementTree.XMLParser.close" title="xml.etree.ElementTree.XMLParser.close"><code class="xref py py-meth docutils literal"><span class="pre">XMLParser.close()</span></code></a>, this method always returns <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>.
1246
Any events not yet retrieved when the parser is closed can still be
1247
read with <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><code class="xref py py-meth docutils literal"><span class="pre">read_events()</span></code></a>.</p>
1251
<dt id="xml.etree.ElementTree.XMLPullParser.read_events">
1252
<code class="descname">read_events</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="Permalink to this definition">¶</a></dt>
1253
<dd><p>Return an iterator over the events which have been encountered in the
1255
parser. The iterator yields <code class="docutils literal"><span class="pre">(event,</span> <span class="pre">elem)</span></code> pairs, where <em>event</em> is a
1256
string representing the type of event (e.g. <code class="docutils literal"><span class="pre">"end"</span></code>) and <em>elem</em> is the
1257
encountered <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> object.</p>
1258
<p>Events provided in a previous call to <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><code class="xref py py-meth docutils literal"><span class="pre">read_events()</span></code></a> will not be
1259
yielded again. Events are consumed from the internal queue only when
1260
they are retrieved from the iterator, so multiple readers iterating in
1261
parallel over iterators obtained from <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><code class="xref py py-meth docutils literal"><span class="pre">read_events()</span></code></a> will have
1262
unpredictable results.</p>
1265
<div class="admonition note">
1266
<p class="first admonition-title">Note</p>
1267
<p><a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal"><span class="pre">XMLPullParser</span></code></a> only guarantees that it has seen the “>”
1268
character of a starting tag when it emits a “start” event, so the
1269
attributes are defined, but the contents of the text and tail attributes
1270
are undefined at that point. The same applies to the element children;
1271
they may or may not be present.</p>
1272
<p class="last">If you need a fully populated element, look for “end” events instead.</p>
1274
<div class="versionadded">
1275
<p><span class="versionmodified">New in version 3.4.</span></p>
1280
<div class="section" id="exceptions">
1281
<h3>20.5.3.8. Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h3>
1283
<dt id="xml.etree.ElementTree.ParseError">
1284
<em class="property">class </em><code class="descclassname">xml.etree.ElementTree.</code><code class="descname">ParseError</code><a class="headerlink" href="#xml.etree.ElementTree.ParseError" title="Permalink to this definition">¶</a></dt>
1285
<dd><p>XML parse error, raised by the various parsing methods in this module when
1286
parsing fails. The string representation of an instance of this exception
1287
will contain a user-friendly error message. In addition, it will have
1288
the following attributes available:</p>
1289
<dl class="attribute">
1290
<dt id="xml.etree.ElementTree.ParseError.code">
1291
<code class="descname">code</code><a class="headerlink" href="#xml.etree.ElementTree.ParseError.code" title="Permalink to this definition">¶</a></dt>
1292
<dd><p>A numeric error code from the expat parser. See the documentation of
1293
<a class="reference internal" href="pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal"><span class="pre">xml.parsers.expat</span></code></a> for the list of error codes and their meanings.</p>
1296
<dl class="attribute">
1297
<dt id="xml.etree.ElementTree.ParseError.position">
1298
<code class="descname">position</code><a class="headerlink" href="#xml.etree.ElementTree.ParseError.position" title="Permalink to this definition">¶</a></dt>
1299
<dd><p>A tuple of <em>line</em>, <em>column</em> numbers, specifying where the error occurred.</p>
1304
<p class="rubric">Footnotes</p>
1305
<table class="docutils footnote" frame="void" id="id5" rules="none">
1306
<colgroup><col class="label" /><col /></colgroup>
1307
<tbody valign="top">
1308
<tr><td class="label">[1]</td><td>The encoding string included in XML output should conform to the
1309
appropriate standards. For example, “UTF-8” is valid, but “UTF8” is
1310
not. See <a class="reference external" href="https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl">https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl</a>
1311
and <a class="reference external" href="https://www.iana.org/assignments/character-sets/character-sets.xhtml">https://www.iana.org/assignments/character-sets/character-sets.xhtml</a>.</td></tr>
1322
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
1323
<div class="sphinxsidebarwrapper">
1324
<h3><a href="../contents.html">Table Of Contents</a></h3>
1326
<li><a class="reference internal" href="#">20.5. <code class="docutils literal"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a><ul>
1327
<li><a class="reference internal" href="#tutorial">20.5.1. Tutorial</a><ul>
1328
<li><a class="reference internal" href="#xml-tree-and-elements">20.5.1.1. XML tree and elements</a></li>
1329
<li><a class="reference internal" href="#parsing-xml">20.5.1.2. Parsing XML</a></li>
1330
<li><a class="reference internal" href="#pull-api-for-non-blocking-parsing">20.5.1.3. Pull API for non-blocking parsing</a></li>
1331
<li><a class="reference internal" href="#finding-interesting-elements">20.5.1.4. Finding interesting elements</a></li>
1332
<li><a class="reference internal" href="#modifying-an-xml-file">20.5.1.5. Modifying an XML File</a></li>
1333
<li><a class="reference internal" href="#building-xml-documents">20.5.1.6. Building XML documents</a></li>
1334
<li><a class="reference internal" href="#parsing-xml-with-namespaces">20.5.1.7. Parsing XML with Namespaces</a></li>
1335
<li><a class="reference internal" href="#additional-resources">20.5.1.8. Additional resources</a></li>
1338
<li><a class="reference internal" href="#xpath-support">20.5.2. XPath support</a><ul>
1339
<li><a class="reference internal" href="#example">20.5.2.1. Example</a></li>
1340
<li><a class="reference internal" href="#supported-xpath-syntax">20.5.2.2. Supported XPath syntax</a></li>
1343
<li><a class="reference internal" href="#reference">20.5.3. Reference</a><ul>
1344
<li><a class="reference internal" href="#functions">20.5.3.1. Functions</a></li>
1345
<li><a class="reference internal" href="#element-objects">20.5.3.2. Element Objects</a></li>
1346
<li><a class="reference internal" href="#elementtree-objects">20.5.3.3. ElementTree Objects</a></li>
1347
<li><a class="reference internal" href="#qname-objects">20.5.3.4. QName Objects</a></li>
1348
<li><a class="reference internal" href="#treebuilder-objects">20.5.3.5. TreeBuilder Objects</a></li>
1349
<li><a class="reference internal" href="#xmlparser-objects">20.5.3.6. XMLParser Objects</a></li>
1350
<li><a class="reference internal" href="#xmlpullparser-objects">20.5.3.7. XMLPullParser Objects</a></li>
1351
<li><a class="reference internal" href="#exceptions">20.5.3.8. Exceptions</a></li>
1358
<h4>Previous topic</h4>
1359
<p class="topless"><a href="xml.html"
1360
title="previous chapter">20.4. XML Processing Modules</a></p>
1362
<p class="topless"><a href="xml.dom.html"
1363
title="next chapter">20.6. <code class="docutils literal"><span class="pre">xml.dom</span></code> — The Document Object Model API</a></p>
1364
<div role="note" aria-label="source link">
1366
<ul class="this-page-menu">
1367
<li><a href="../bugs.html">Report a Bug</a></li>
1368
<li><a href="../_sources/library/xml.etree.elementtree.txt"
1369
rel="nofollow">Show Source</a></li>
1374
<div class="clearer"></div>
1376
<div class="related" role="navigation" aria-label="related navigation">
1379
<li class="right" style="margin-right: 10px">
1380
<a href="../genindex.html" title="General Index"
1383
<a href="../py-modindex.html" title="Python Module Index"
1386
<a href="xml.dom.html" title="20.6. xml.dom — The Document Object Model API"
1389
<a href="xml.html" title="20.4. XML Processing Modules"
1390
>previous</a> |</li>
1391
<li><img src="../_static/py.png" alt=""
1392
style="vertical-align: middle; margin-top: -1px"/></li>
1393
<li><a href="https://www.python.org/">Python</a> »</li>
1395
<span class="version_switcher_placeholder">3.5.2</span>
1396
<a href="../index.html">Documentation </a> »
1399
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
1400
<li class="nav-item nav-item-2"><a href="markup.html" >20. Structured Markup Processing Tools</a> »</li>
1404
<div class="inline-search" style="display: none" role="search">
1405
<form class="inline-search" action="../search.html" method="get">
1406
<input placeholder="Quick search" type="text" name="q" />
1407
<input type="submit" value="Go" />
1408
<input type="hidden" name="check_keywords" value="yes" />
1409
<input type="hidden" name="area" value="default" />
1412
<script type="text/javascript">$('.inline-search').show(0);</script>
1418
<div class="footer">
1419
© <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
1421
The Python Software Foundation is a non-profit corporation.
1422
<a href="https://www.python.org/psf/donations/">Please donate.</a>
1424
Last updated on Oct 19, 2016.
1425
<a href="../bugs.html">Found a bug</a>?
1427
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'