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KO}ӽmSşذqsOy?\[,d@'73'j%kOe`1.g2"e =YIzS2|zŐƄa\U,dP;jhhhaxǶ?КZ՚.q SE+XrbOu%\GتX(H,N^~]JyEZQKceTQ]VGYqnah;y$cQahT&QPZ*iZ8UQQM.qo/T\7X"u?Mttl2Xq(IoW{R^ ux*SYJ! 4S.Jy~ BROS[V|žKNɛP(L6V^|cR7i7nZW1Fd@ Ara{詑|(T*dN]Ko?s=@ |_EvF]׍kR)eBJc" MUUbY6`~V޴dJKß&~'d3i WWWWWW
Current Directory: /usr/share/doc/python2-docs/html/library
Viewing File: /usr/share/doc/python2-docs/html/library/xml.etree.elementtree.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>19.7. xml.etree.ElementTree — The ElementTree XML API &#8212; Python 2.7.16 documentation</title> <link rel="stylesheet" href="../_static/classic.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.16 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="19.8. xml.dom — The Document Object Model API" href="xml.dom.html" /> <link rel="prev" title="19.5. XML Processing Modules" href="xml.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/2/library/xml.etree.elementtree.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="xml.dom.html" title="19.8. xml.dom — The Document Object Model API" accesskey="N">next</a> |</li> <li class="right" > <a href="xml.html" title="19.5. XML Processing Modules" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> &#187;</li> <li> <a href="../index.html">Python 2.7.16 documentation</a> &#187; </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li> <li class="nav-item nav-item-2"><a href="markup.html" accesskey="U">19. Structured Markup Processing Tools</a> &#187;</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-xml.etree.ElementTree"> <span id="xml-etree-elementtree-the-elementtree-xml-api"></span><h1>19.7. <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 notranslate"><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> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </div> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/2.7/Lib/xml/etree/ElementTree.py">Lib/xml/etree/ElementTree.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> type is a flexible container object, designed to store hierarchical data structures in memory. The type can be described as a cross between a list and a dictionary.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <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 notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see <a class="reference internal" href="xml.html#xml-vulnerabilities"><span class="std std-ref">XML vulnerabilities</span></a>.</p> </div> <p>Each element has a number of properties associated with it:</p> <ul class="simple"> <li>a tag which is a string identifying what kind of data this element represents (the element type, in other words).</li> <li>a number of attributes, stored in a Python dictionary.</li> <li>a text string.</li> <li>an optional tail string.</li> <li>a number of child elements, stored in a Python sequence</li> </ul> <p>To create an element instance, use the <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> constructor or the <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><code class="xref py py-func docutils literal notranslate"><span class="pre">SubElement()</span></code></a> factory function.</p> <p>The <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> class can be used to wrap an element structure, and convert it from and to XML.</p> <p>A C implementation of this API is available as <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.cElementTree</span></code>.</p> <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 docs. Fredrik Lundh’s page is also the location of the development version of the xml.etree.ElementTree.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.7: </span>The ElementTree API is updated to 1.3. For more information, see <a class="reference external" href="http://effbot.org/zone/elementtree-13-intro.htm">Introducing ElementTree 1.3</a>.</p> </div> <div class="section" id="tutorial"> <h2>19.7.1. Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2> <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 notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> (<code class="docutils literal notranslate"><span class="pre">ET</span></code> in short). The goal is to demonstrate some of the building blocks and basic concepts of the module.</p> <div class="section" id="xml-tree-and-elements"> <h3>19.7.1.1. XML tree and elements<a class="headerlink" href="#xml-tree-and-elements" title="Permalink to this headline">¶</a></h3> <p>XML is an inherently hierarchical data format, and the most natural way to represent it is with a tree. <code class="docutils literal notranslate"><span class="pre">ET</span></code> has two classes for this purpose - <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> represents the whole XML document as a tree, and <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> represents a single node in this tree. Interactions with the whole document (reading and writing to/from files) are usually done on the <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> level. Interactions with a single XML element 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 notranslate"><span class="pre">Element</span></code></a> level.</p> </div> <div class="section" id="parsing-xml"> <span id="elementtree-parsing-xml"></span><h3>19.7.1.2. Parsing XML<a class="headerlink" href="#parsing-xml" title="Permalink to this headline">¶</a></h3> <p>We’ll be using the following XML document as the sample data for this section:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span> <span class="nt">&lt;data&gt;</span> <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;rank&gt;</span>1<span class="nt">&lt;/rank&gt;</span> <span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span> <span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Austria&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;/country&gt;</span> <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;rank&gt;</span>4<span class="nt">&lt;/rank&gt;</span> <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span> <span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span> <span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;/country&gt;</span> <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Panama&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;rank&gt;</span>68<span class="nt">&lt;/rank&gt;</span> <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span> <span class="nt">&lt;gdppc&gt;</span>13600<span class="nt">&lt;/gdppc&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Costa Rica&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Colombia&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;/country&gt;</span> <span class="nt">&lt;/data&gt;</span> </pre></div> </div> <p>We have a number of ways to import the data. Reading the file from disk:</p> <div class="highlight-default notranslate"><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> <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">&#39;country_data.xml&#39;</span><span class="p">)</span> <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> </pre></div> </div> <p>Reading the data from a string:</p> <div class="highlight-default notranslate"><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> </pre></div> </div> <p><a class="reference internal" href="#xml.etree.ElementTree.fromstring" title="xml.etree.ElementTree.fromstring"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><span class="pre">Element</span></code></a>, which is the root element of the parsed tree. Other parsing functions may create an <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a>. Check the documentation to be sure.</p> <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 notranslate"><span class="pre">Element</span></code></a>, <code class="docutils literal notranslate"><span class="pre">root</span></code> has a tag and a dictionary of attributes:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">tag</span> <span class="go">&#39;data&#39;</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">attrib</span> <span class="go">{}</span> </pre></div> </div> <p>It also has children nodes over which we can iterate:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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> <span class="gp">... </span> <span class="nb">print</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="gp">...</span> <span class="go">country {&#39;name&#39;: &#39;Liechtenstein&#39;}</span> <span class="go">country {&#39;name&#39;: &#39;Singapore&#39;}</span> <span class="go">country {&#39;name&#39;: &#39;Panama&#39;}</span> </pre></div> </div> <p>Children are nested, and we can access specific child nodes by index:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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> <span class="go">&#39;2008&#39;</span> </pre></div> </div> </div> <div class="section" id="finding-interesting-elements"> <h3>19.7.1.3. Finding interesting elements<a class="headerlink" href="#finding-interesting-elements" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> has some useful methods that help iterate recursively over all the sub-tree below it (its children, their children, and so on). For example, <a class="reference internal" href="#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.iter()</span></code></a>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&#39;neighbor&#39;</span><span class="p">):</span> <span class="gp">... </span> <span class="nb">print</span> <span class="n">neighbor</span><span class="o">.</span><span class="n">attrib</span> <span class="gp">...</span> <span class="go">{&#39;name&#39;: &#39;Austria&#39;, &#39;direction&#39;: &#39;E&#39;}</span> <span class="go">{&#39;name&#39;: &#39;Switzerland&#39;, &#39;direction&#39;: &#39;W&#39;}</span> <span class="go">{&#39;name&#39;: &#39;Malaysia&#39;, &#39;direction&#39;: &#39;N&#39;}</span> <span class="go">{&#39;name&#39;: &#39;Costa Rica&#39;, &#39;direction&#39;: &#39;W&#39;}</span> <span class="go">{&#39;name&#39;: &#39;Colombia&#39;, &#39;direction&#39;: &#39;E&#39;}</span> </pre></div> </div> <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 notranslate"><span class="pre">Element.findall()</span></code></a> finds only elements with a tag which are direct 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 notranslate"><span class="pre">Element.find()</span></code></a> finds the <em>first</em> child 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 notranslate"><span class="pre">Element.text</span></code></a> accesses the element’s text 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 notranslate"><span class="pre">Element.get()</span></code></a> accesses the element’s attributes:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&#39;country&#39;</span><span class="p">):</span> <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">&#39;rank&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">text</span> <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">&#39;name&#39;</span><span class="p">)</span> <span class="gp">... </span> <span class="nb">print</span> <span class="n">name</span><span class="p">,</span> <span class="n">rank</span> <span class="gp">...</span> <span class="go">Liechtenstein 1</span> <span class="go">Singapore 4</span> <span class="go">Panama 68</span> </pre></div> </div> <p>More sophisticated specification of which elements to look for is possible by using <a class="reference internal" href="#elementtree-xpath"><span class="std std-ref">XPath</span></a>.</p> </div> <div class="section" id="modifying-an-xml-file"> <h3>19.7.1.4. Modifying an XML File<a class="headerlink" href="#modifying-an-xml-file" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> provides a simple way to build XML documents and write them to files. 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 notranslate"><span class="pre">ElementTree.write()</span></code></a> method serves this purpose.</p> <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 notranslate"><span class="pre">Element</span></code></a> object may be manipulated by directly changing 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 notranslate"><span class="pre">Element.text</span></code></a>), adding and modifying attributes (<a class="reference internal" href="#xml.etree.ElementTree.Element.set" title="xml.etree.ElementTree.Element.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.set()</span></code></a> method), as well as adding new children (for example 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 notranslate"><span class="pre">Element.append()</span></code></a>).</p> <p>Let’s say we want to add one to each country’s rank, and add an <code class="docutils literal notranslate"><span class="pre">updated</span></code> attribute to the rank element:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&#39;rank&#39;</span><span class="p">):</span> <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> <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> <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">&#39;updated&#39;</span><span class="p">,</span> <span class="s1">&#39;yes&#39;</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;output.xml&#39;</span><span class="p">)</span> </pre></div> </div> <p>Our XML now looks like this:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span> <span class="nt">&lt;data&gt;</span> <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>2<span class="nt">&lt;/rank&gt;</span> <span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span> <span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Austria&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;/country&gt;</span> <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>5<span class="nt">&lt;/rank&gt;</span> <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span> <span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span> <span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;/country&gt;</span> <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Panama&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>69<span class="nt">&lt;/rank&gt;</span> <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span> <span class="nt">&lt;gdppc&gt;</span>13600<span class="nt">&lt;/gdppc&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Costa Rica&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Colombia&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;/country&gt;</span> <span class="nt">&lt;/data&gt;</span> </pre></div> </div> <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 notranslate"><span class="pre">Element.remove()</span></code></a>. Let’s say we want to remove all countries with a rank higher than 50:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&#39;country&#39;</span><span class="p">):</span> <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">&#39;rank&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">)</span> <span class="gp">... </span> <span class="k">if</span> <span class="n">rank</span> <span class="o">&gt;</span> <span class="mi">50</span><span class="p">:</span> <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> <span class="gp">...</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;output.xml&#39;</span><span class="p">)</span> </pre></div> </div> <p>Our XML now looks like this:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span> <span class="nt">&lt;data&gt;</span> <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>2<span class="nt">&lt;/rank&gt;</span> <span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span> <span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Austria&quot;</span> <span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span> <span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;/country&gt;</span> <span class="nt">&lt;country</span> <span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;rank</span> <span class="na">updated=</span><span class="s">&quot;yes&quot;</span><span class="nt">&gt;</span>5<span class="nt">&lt;/rank&gt;</span> <span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span> <span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span> <span class="nt">&lt;neighbor</span> <span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span> <span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span> <span class="nt">&lt;/country&gt;</span> <span class="nt">&lt;/data&gt;</span> </pre></div> </div> </div> <div class="section" id="building-xml-documents"> <h3>19.7.1.5. Building XML documents<a class="headerlink" href="#building-xml-documents" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference internal" href="#xml.etree.ElementTree.SubElement" title="xml.etree.ElementTree.SubElement"><code class="xref py py-func docutils literal notranslate"><span class="pre">SubElement()</span></code></a> function also provides a convenient way to create new sub-elements for a given element:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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">&#39;a&#39;</span><span class="p">)</span> <span class="gp">&gt;&gt;&gt; </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">&#39;b&#39;</span><span class="p">)</span> <span class="gp">&gt;&gt;&gt; </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">&#39;c&#39;</span><span class="p">)</span> <span class="gp">&gt;&gt;&gt; </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">&#39;d&#39;</span><span class="p">)</span> <span class="gp">&gt;&gt;&gt; </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> <span class="go">&lt;a&gt;&lt;b /&gt;&lt;c&gt;&lt;d /&gt;&lt;/c&gt;&lt;/a&gt;</span> </pre></div> </div> </div> <div class="section" id="parsing-xml-with-namespaces"> <h3>19.7.1.6. Parsing XML with Namespaces<a class="headerlink" href="#parsing-xml-with-namespaces" title="Permalink to this headline">¶</a></h3> <p>If the XML input has <a class="reference external" href="https://en.wikipedia.org/wiki/XML_namespace">namespaces</a>, tags and attributes with prefixes in the form <code class="docutils literal notranslate"><span class="pre">prefix:sometag</span></code> get expanded to <code class="docutils literal notranslate"><span class="pre">{uri}sometag</span></code> where the <em>prefix</em> is replaced by the full <em>URI</em>. Also, if there is a <a class="reference external" href="https://www.w3.org/TR/xml-names/#defaulting">default namespace</a>, that full URI gets prepended to all of the non-prefixed tags.</p> <p>Here is an XML example that incorporates two namespaces, one with the prefix “fictional” and the other serving as the default namespace:</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span> <span class="nt">&lt;actors</span> <span class="na">xmlns:fictional=</span><span class="s">&quot;http://characters.example.com&quot;</span> <span class="na">xmlns=</span><span class="s">&quot;http://people.example.com&quot;</span><span class="nt">&gt;</span> <span class="nt">&lt;actor&gt;</span> <span class="nt">&lt;name&gt;</span>John Cleese<span class="nt">&lt;/name&gt;</span> <span class="nt">&lt;fictional:character&gt;</span>Lancelot<span class="nt">&lt;/fictional:character&gt;</span> <span class="nt">&lt;fictional:character&gt;</span>Archie Leach<span class="nt">&lt;/fictional:character&gt;</span> <span class="nt">&lt;/actor&gt;</span> <span class="nt">&lt;actor&gt;</span> <span class="nt">&lt;name&gt;</span>Eric Idle<span class="nt">&lt;/name&gt;</span> <span class="nt">&lt;fictional:character&gt;</span>Sir Robin<span class="nt">&lt;/fictional:character&gt;</span> <span class="nt">&lt;fictional:character&gt;</span>Gunther<span class="nt">&lt;/fictional:character&gt;</span> <span class="nt">&lt;fictional:character&gt;</span>Commander Clement<span class="nt">&lt;/fictional:character&gt;</span> <span class="nt">&lt;/actor&gt;</span> <span class="nt">&lt;/actors&gt;</span> </pre></div> </div> <p>One way to search and explore this XML example is to manually add the URI to every tag or attribute in the xpath of a <a class="reference internal" href="#xml.etree.ElementTree.Element.find" title="xml.etree.ElementTree.Element.find"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">findall()</span></code></a>:</p> <div class="highlight-default notranslate"><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> <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">&#39;{http://people.example.com}actor&#39;</span><span class="p">):</span> <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">&#39;{http://people.example.com}name&#39;</span><span class="p">)</span> <span class="nb">print</span> <span class="n">name</span><span class="o">.</span><span class="n">text</span> <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">&#39;{http://characters.example.com}character&#39;</span><span class="p">):</span> <span class="nb">print</span> <span class="s1">&#39; |--&gt;&#39;</span><span class="p">,</span> <span class="n">char</span><span class="o">.</span><span class="n">text</span> </pre></div> </div> <p>A better way to search the namespaced XML example is to create a dictionary with your own prefixes and use those in the search functions:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ns</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;real_person&#39;</span><span class="p">:</span> <span class="s1">&#39;http://people.example.com&#39;</span><span class="p">,</span> <span class="s1">&#39;role&#39;</span><span class="p">:</span> <span class="s1">&#39;http://characters.example.com&#39;</span><span class="p">}</span> <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">&#39;real_person:actor&#39;</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span> <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">&#39;real_person:name&#39;</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span> <span class="nb">print</span> <span class="n">name</span><span class="o">.</span><span class="n">text</span> <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">&#39;role:character&#39;</span><span class="p">,</span> <span class="n">ns</span><span class="p">):</span> <span class="nb">print</span> <span class="s1">&#39; |--&gt;&#39;</span><span class="p">,</span> <span class="n">char</span><span class="o">.</span><span class="n">text</span> </pre></div> </div> <p>These two approaches both output:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">John</span> <span class="n">Cleese</span> <span class="o">|--&gt;</span> <span class="n">Lancelot</span> <span class="o">|--&gt;</span> <span class="n">Archie</span> <span class="n">Leach</span> <span class="n">Eric</span> <span class="n">Idle</span> <span class="o">|--&gt;</span> <span class="n">Sir</span> <span class="n">Robin</span> <span class="o">|--&gt;</span> <span class="n">Gunther</span> <span class="o">|--&gt;</span> <span class="n">Commander</span> <span class="n">Clement</span> </pre></div> </div> </div> <div class="section" id="additional-resources"> <h3>19.7.1.7. Additional resources<a class="headerlink" href="#additional-resources" title="Permalink to this headline">¶</a></h3> <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 docs.</p> </div> </div> <div class="section" id="xpath-support"> <span id="elementtree-xpath"></span><h2>19.7.2. XPath support<a class="headerlink" href="#xpath-support" title="Permalink to this headline">¶</a></h2> <p>This module provides limited support for <a class="reference external" href="https://www.w3.org/TR/xpath">XPath expressions</a> for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module.</p> <div class="section" id="example"> <h3>19.7.2.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3> <p>Here’s an example that demonstrates some of the XPath capabilities of the module. We’ll be using the <code class="docutils literal notranslate"><span class="pre">countrydata</span></code> XML document from the <a class="reference internal" href="#elementtree-parsing-xml"><span class="std std-ref">Parsing XML</span></a> section:</p> <div class="highlight-default notranslate"><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> <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> <span class="c1"># Top-level elements</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)</span> <span class="c1"># All &#39;neighbor&#39; grand-children of &#39;country&#39; children of the top-level</span> <span class="c1"># elements</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">&quot;./country/neighbor&quot;</span><span class="p">)</span> <span class="c1"># Nodes with name=&#39;Singapore&#39; that have a &#39;year&#39; child</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">&quot;.//year/..[@name=&#39;Singapore&#39;]&quot;</span><span class="p">)</span> <span class="c1"># &#39;year&#39; nodes that are children of nodes with name=&#39;Singapore&#39;</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">&quot;.//*[@name=&#39;Singapore&#39;]/year&quot;</span><span class="p">)</span> <span class="c1"># All &#39;neighbor&#39; nodes that are the second child of their parent</span> <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">&quot;.//neighbor[2]&quot;</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="supported-xpath-syntax"> <h3>19.7.2.2. Supported XPath syntax<a class="headerlink" href="#supported-xpath-syntax" title="Permalink to this headline">¶</a></h3> <table border="1" class="docutils"> <colgroup> <col width="30%" /> <col width="70%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Syntax</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">tag</span></code></td> <td>Selects all child elements with the given tag. For example, <code class="docutils literal notranslate"><span class="pre">spam</span></code> selects all child elements named <code class="docutils literal notranslate"><span class="pre">spam</span></code>, and <code class="docutils literal notranslate"><span class="pre">spam/egg</span></code> selects all grandchildren named <code class="docutils literal notranslate"><span class="pre">egg</span></code> in all children named <code class="docutils literal notranslate"><span class="pre">spam</span></code>.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">*</span></code></td> <td>Selects all child elements. For example, <code class="docutils literal notranslate"><span class="pre">*/egg</span></code> selects all grandchildren named <code class="docutils literal notranslate"><span class="pre">egg</span></code>.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">.</span></code></td> <td>Selects the current node. This is mostly useful at the beginning of the path, to indicate that it’s a relative path.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">//</span></code></td> <td>Selects all subelements, on all levels beneath the current element. For example, <code class="docutils literal notranslate"><span class="pre">.//egg</span></code> selects all <code class="docutils literal notranslate"><span class="pre">egg</span></code> elements in the entire tree.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">..</span></code></td> <td>Selects the parent element.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">[&#64;attrib]</span></code></td> <td>Selects all elements that have the given attribute.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">[&#64;attrib='value']</span></code></td> <td>Selects all elements for which the given attribute has the given value. The value cannot contain quotes.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">[tag]</span></code></td> <td>Selects all elements that have a child named <code class="docutils literal notranslate"><span class="pre">tag</span></code>. Only immediate children are supported.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">[tag='text']</span></code></td> <td>Selects all elements that have a child named <code class="docutils literal notranslate"><span class="pre">tag</span></code> whose complete text content, including descendants, equals the given <code class="docutils literal notranslate"><span class="pre">text</span></code>.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">[position]</span></code></td> <td>Selects all elements that are located at the given position. The position can be either an integer (1 is the first position), the expression <code class="docutils literal notranslate"><span class="pre">last()</span></code> (for the last position), or a position relative to the last position (e.g. <code class="docutils literal notranslate"><span class="pre">last()-1</span></code>).</td> </tr> </tbody> </table> <p>Predicates (expressions within square brackets) must be preceded by a tag name, an asterisk, or another predicate. <code class="docutils literal notranslate"><span class="pre">position</span></code> predicates must be preceded by a tag name.</p> </div> </div> <div class="section" id="reference"> <h2>19.7.3. Reference<a class="headerlink" href="#reference" title="Permalink to this headline">¶</a></h2> <div class="section" id="functions"> <span id="elementtree-functions"></span><h3>19.7.3.1. Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3> <dl class="function"> <dt id="xml.etree.ElementTree.Comment"> <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> <dd><p>Comment element factory. This factory function creates a special element that will be serialized as an XML comment by the standard serializer. The comment string can be either a bytestring or a Unicode string. <em>text</em> is a string containing the comment string. Returns an element instance representing a comment.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.dump"> <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> <dd><p>Writes an element tree or element structure to sys.stdout. This function should be used for debugging only.</p> <p>The exact output format is implementation dependent. In this version, it’s written as an ordinary XML file.</p> <p><em>elem</em> is an element tree or an individual element.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.fromstring"> <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> <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 notranslate"><span class="pre">XML()</span></code></a>. <em>text</em> 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 notranslate"><span class="pre">Element</span></code></a> instance.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.fromstringlist"> <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> <dd><p>Parses an XML document from a sequence of string fragments. <em>sequence</em> is a list or other sequence containing XML data fragments. <em>parser</em> is an 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 notranslate"><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 notranslate"><span class="pre">Element</span></code></a> instance.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.iselement"> <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> <dd><p>Checks if an object appears to be a valid element object. <em>element</em> is an element instance. Returns a true value if this is an element object.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.iterparse"> <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> <dd><p>Parses an XML section into an element tree incrementally, and reports what’s going on to the user. <em>source</em> is a filename or file object containing XML data. <em>events</em> is a list of events to report back. If omitted, only “end” events are reported. <em>parser</em> is an 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 notranslate"><span class="pre">XMLParser</span></code></a> parser is used. <em>parser</em> is not supported by <code class="docutils literal notranslate"><span class="pre">cElementTree</span></code>. 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 notranslate"><span class="pre">(event,</span> <span class="pre">elem)</span></code> pairs.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p><a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><code class="xref py py-func docutils literal notranslate"><span class="pre">iterparse()</span></code></a> only guarantees that it has seen the “&gt;” character of a starting tag when it emits a “start” event, so the attributes are defined, but the contents of the text and tail attributes are undefined at that point. The same applies to the element children; they may or may not be present.</p> <p class="last">If you need a fully populated element, look for “end” events instead.</p> </div> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.parse"> <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> <dd><p>Parses an XML section into an element tree. <em>source</em> is a filename or file object containing XML data. <em>parser</em> is an 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 notranslate"><span class="pre">XMLParser</span></code></a> parser is used. Returns an <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> instance.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.ProcessingInstruction"> <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> <dd><p>PI element factory. This factory function creates a special element that will be serialized as an XML processing instruction. <em>target</em> is a string containing the PI target. <em>text</em> is a string containing the PI contents, if given. Returns an element instance, representing a processing instruction.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.register_namespace"> <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> <dd><p>Registers a namespace prefix. The registry is global, and any existing mapping for either the given prefix or the namespace URI will be removed. <em>prefix</em> is a namespace prefix. <em>uri</em> is a namespace uri. Tags and attributes in this namespace will be serialized with the given prefix, if at all possible.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.SubElement"> <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> <dd><p>Subelement factory. This function creates an element instance, and appends it to an existing element.</p> <p>The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. <em>parent</em> is the parent element. <em>tag</em> is the subelement name. <em>attrib</em> is an optional dictionary, containing element attributes. <em>extra</em> contains additional attributes, given as keyword arguments. Returns an element instance.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.tostring"> <code class="descclassname">xml.etree.ElementTree.</code><code class="descname">tostring</code><span class="sig-paren">(</span><em>element</em>, <em>encoding=&quot;us-ascii&quot;</em>, <em>method=&quot;xml&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.tostring" title="Permalink to this definition">¶</a></dt> <dd><p>Generates a string representation of an XML element, including all 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 notranslate"><span class="pre">Element</span></code></a> instance. <em>encoding</em> <a class="footnote-reference" href="#id5" id="id1">[1]</a> is the output encoding (default is US-ASCII). <em>method</em> is either <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;html&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;text&quot;</span></code> (default is <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>). Returns an encoded string containing the XML data.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.tostringlist"> <code class="descclassname">xml.etree.ElementTree.</code><code class="descname">tostringlist</code><span class="sig-paren">(</span><em>element</em>, <em>encoding=&quot;us-ascii&quot;</em>, <em>method=&quot;xml&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.tostringlist" title="Permalink to this definition">¶</a></dt> <dd><p>Generates a string representation of an XML element, including all 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 notranslate"><span class="pre">Element</span></code></a> instance. <em>encoding</em> <a class="footnote-reference" href="#id5" id="id2">[1]</a> is the output encoding (default is US-ASCII). <em>method</em> is either <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;html&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;text&quot;</span></code> (default is <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>). Returns a list of encoded strings containing the XML data. It does not guarantee any specific sequence, except that <code class="docutils literal notranslate"><span class="pre">&quot;&quot;.join(tostringlist(element))</span> <span class="pre">==</span> <span class="pre">tostring(element)</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.XML"> <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> <dd><p>Parses an XML section from a string constant. This function can be used to embed “XML literals” in Python code. <em>text</em> is a string containing XML data. <em>parser</em> is an 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 notranslate"><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 notranslate"><span class="pre">Element</span></code></a> instance.</p> </dd></dl> <dl class="function"> <dt id="xml.etree.ElementTree.XMLID"> <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> <dd><p>Parses an XML section from a string constant, and also returns a dictionary which maps from element id:s to elements. <em>text</em> is a string containing XML data. <em>parser</em> is an 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 notranslate"><span class="pre">XMLParser</span></code></a> parser is used. Returns a tuple containing an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instance and a dictionary.</p> </dd></dl> </div> <div class="section" id="element-objects"> <span id="elementtree-element-objects"></span><h3>19.7.3.2. Element Objects<a class="headerlink" href="#element-objects" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="xml.etree.ElementTree.Element"> <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> <dd><p>Element class. This class defines the Element interface, and provides a reference implementation of this interface.</p> <p>The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. <em>tag</em> is the element name. <em>attrib</em> is an optional dictionary, containing element attributes. <em>extra</em> contains additional attributes, given as keyword arguments.</p> <dl class="attribute"> <dt id="xml.etree.ElementTree.Element.tag"> <code class="descname">tag</code><a class="headerlink" href="#xml.etree.ElementTree.Element.tag" title="Permalink to this definition">¶</a></dt> <dd><p>A string identifying what kind of data this element represents (the element type, in other words).</p> </dd></dl> <dl class="attribute"> <dt id="xml.etree.ElementTree.Element.text"> <code class="descname">text</code><a class="headerlink" href="#xml.etree.ElementTree.Element.text" title="Permalink to this definition">¶</a></dt> <dt id="xml.etree.ElementTree.Element.tail"> <code class="descname">tail</code><a class="headerlink" href="#xml.etree.ElementTree.Element.tail" title="Permalink to this definition">¶</a></dt> <dd><p>These attributes can be used to hold additional data associated with the element. Their values are usually strings but may be any application-specific object. If the element is created from an XML file, the <em>text</em> attribute holds either the text between the element’s start tag and its first child or end tag, or <code class="docutils literal notranslate"><span class="pre">None</span></code>, and the <em>tail</em> attribute holds either the text between the element’s end tag and the next tag, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. For the XML data</p> <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="nt">&lt;a&gt;&lt;b&gt;</span>1<span class="nt">&lt;c&gt;</span>2<span class="nt">&lt;d/&gt;</span>3<span class="nt">&lt;/c&gt;&lt;/b&gt;</span>4<span class="nt">&lt;/a&gt;</span> </pre></div> </div> <p>the <em>a</em> element has <code class="docutils literal notranslate"><span class="pre">None</span></code> for both <em>text</em> and <em>tail</em> attributes, the <em>b</em> element has <em>text</em> <code class="docutils literal notranslate"><span class="pre">&quot;1&quot;</span></code> and <em>tail</em> <code class="docutils literal notranslate"><span class="pre">&quot;4&quot;</span></code>, the <em>c</em> element has <em>text</em> <code class="docutils literal notranslate"><span class="pre">&quot;2&quot;</span></code> and <em>tail</em> <code class="docutils literal notranslate"><span class="pre">None</span></code>, and the <em>d</em> element has <em>text</em> <code class="docutils literal notranslate"><span class="pre">None</span></code> and <em>tail</em> <code class="docutils literal notranslate"><span class="pre">&quot;3&quot;</span></code>.</p> <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 notranslate"><span class="pre">itertext()</span></code></a>, for example <code class="docutils literal notranslate"><span class="pre">&quot;&quot;.join(element.itertext())</span></code>.</p> <p>Applications may store arbitrary objects in these attributes.</p> </dd></dl> <dl class="attribute"> <dt id="xml.etree.ElementTree.Element.attrib"> <code class="descname">attrib</code><a class="headerlink" href="#xml.etree.ElementTree.Element.attrib" title="Permalink to this definition">¶</a></dt> <dd><p>A dictionary containing the element’s attributes. Note that while the <em>attrib</em> value is always a real mutable Python dictionary, an ElementTree implementation may choose to use another internal representation, and create the dictionary only if someone asks for it. To take advantage of such implementations, use the dictionary methods below whenever possible.</p> </dd></dl> <p>The following dictionary-like methods work on the element attributes.</p> <dl class="method"> <dt id="xml.etree.ElementTree.Element.clear"> <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> <dd><p>Resets an element. This function removes all subelements, clears all attributes, and sets the text and tail attributes to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.get"> <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> <dd><p>Gets the element attribute named <em>key</em>.</p> <p>Returns the attribute value, or <em>default</em> if the attribute was not found.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.items"> <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> <dd><p>Returns the element attributes as a sequence of (name, value) pairs. The attributes are returned in an arbitrary order.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.keys"> <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> <dd><p>Returns the elements attribute names as a list. The names are returned in an arbitrary order.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.set"> <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> <dd><p>Set the attribute <em>key</em> on the element to <em>value</em>.</p> </dd></dl> <p>The following methods work on the element’s children (subelements).</p> <dl class="method"> <dt id="xml.etree.ElementTree.Element.append"> <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> <dd><p>Adds the element <em>subelement</em> to the end of this elements internal list of subelements.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.extend"> <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> <dd><p>Appends <em>subelements</em> from a sequence object with zero or more elements. Raises <a class="reference internal" href="exceptions.html#exceptions.AssertionError" title="exceptions.AssertionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AssertionError</span></code></a> if a subelement is not a valid object.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.find"> <code class="descname">find</code><span class="sig-paren">(</span><em>match</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.find" title="Permalink to this definition">¶</a></dt> <dd><p>Finds the first subelement matching <em>match</em>. <em>match</em> may be a tag name or path. Returns an element instance or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.findall"> <code class="descname">findall</code><span class="sig-paren">(</span><em>match</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.findall" title="Permalink to this definition">¶</a></dt> <dd><p>Finds all matching subelements, by tag name or path. Returns a list containing all matching elements in document order.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.findtext"> <code class="descname">findtext</code><span class="sig-paren">(</span><em>match</em>, <em>default=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.findtext" title="Permalink to this definition">¶</a></dt> <dd><p>Finds text for the first subelement matching <em>match</em>. <em>match</em> may be a tag name or path. Returns the text content of the first matching element, or <em>default</em> if no element was found. Note that if the matching element has no text content an empty string is returned.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.getchildren"> <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> <dd><div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.7: </span>Use <code class="docutils literal notranslate"><span class="pre">list(elem)</span></code> or iteration.</p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.getiterator"> <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> <dd><div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.7: </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 notranslate"><span class="pre">Element.iter()</span></code></a> instead.</p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.insert"> <code class="descname">insert</code><span class="sig-paren">(</span><em>index</em>, <em>element</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.insert" title="Permalink to this definition">¶</a></dt> <dd><p>Inserts a subelement at the given position in this element.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.iter"> <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> <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. The iterator iterates over this element and all elements below it, in document (depth first) order. If <em>tag</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> or <code class="docutils literal notranslate"><span class="pre">'*'</span></code>, only elements whose tag equals <em>tag</em> are returned from the iterator. If the tree structure is modified during iteration, the result is undefined.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.iterfind"> <code class="descname">iterfind</code><span class="sig-paren">(</span><em>match</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.iterfind" title="Permalink to this definition">¶</a></dt> <dd><p>Finds all matching subelements, by tag name or path. Returns an iterable yielding all matching elements in document order.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.itertext"> <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> <dd><p>Creates a text iterator. The iterator loops over this element and all subelements, in document order, and returns all inner text.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.makeelement"> <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> <dd><p>Creates a new element object of the same type as this element. Do not 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 notranslate"><span class="pre">SubElement()</span></code></a> factory function instead.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.Element.remove"> <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> <dd><p>Removes <em>subelement</em> from the element. Unlike the find* methods this method compares elements based on the instance identity, not on tag value or contents.</p> </dd></dl> <p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> objects also support the following sequence type methods 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 notranslate"><span class="pre">__delitem__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">__setitem__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code></a>.</p> <p>Caution: Elements with no subelements will test as <code class="docutils literal notranslate"><span class="pre">False</span></code>. This behavior will change in future versions. Use specific <code class="docutils literal notranslate"><span class="pre">len(elem)</span></code> or <code class="docutils literal notranslate"><span class="pre">elem</span> <span class="pre">is</span> <span class="pre">None</span></code> test instead.</p> <div class="highlight-default notranslate"><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">&#39;foo&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span> <span class="c1"># careful!</span> <span class="nb">print</span> <span class="s2">&quot;element not found, or element has no subelements&quot;</span> <span class="k">if</span> <span class="n">element</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="nb">print</span> <span class="s2">&quot;element not found&quot;</span> </pre></div> </div> </dd></dl> </div> <div class="section" id="elementtree-objects"> <span id="elementtree-elementtree-objects"></span><h3>19.7.3.3. ElementTree Objects<a class="headerlink" href="#elementtree-objects" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="xml.etree.ElementTree.ElementTree"> <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> <dd><p>ElementTree wrapper class. This class represents an entire element hierarchy, and adds some extra support for serialization to and from standard XML.</p> <p><em>element</em> is the root element. The tree is initialized with the contents of the XML <em>file</em> if given.</p> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree._setroot"> <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> <dd><p>Replaces the root element for this tree. This discards the current contents of the tree, and replaces it with the given element. Use with care. <em>element</em> is an element instance.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.find"> <code class="descname">find</code><span class="sig-paren">(</span><em>match</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.find" title="Permalink to this definition">¶</a></dt> <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 notranslate"><span class="pre">Element.find()</span></code></a>, starting at the root of the tree.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.findall"> <code class="descname">findall</code><span class="sig-paren">(</span><em>match</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findall" title="Permalink to this definition">¶</a></dt> <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 notranslate"><span class="pre">Element.findall()</span></code></a>, starting at the root of the tree.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.findtext"> <code class="descname">findtext</code><span class="sig-paren">(</span><em>match</em>, <em>default=None</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findtext" title="Permalink to this definition">¶</a></dt> <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 notranslate"><span class="pre">Element.findtext()</span></code></a>, starting at the root of the tree.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.getiterator"> <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> <dd><div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.7: </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 notranslate"><span class="pre">ElementTree.iter()</span></code></a> instead.</p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.getroot"> <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> <dd><p>Returns the root element for this tree.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.iter"> <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> <dd><p>Creates and returns a tree iterator for the root element. The iterator loops over all elements in this tree, in section order. <em>tag</em> is the tag to look for (default is to return all elements).</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.iterfind"> <code class="descname">iterfind</code><span class="sig-paren">(</span><em>match</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iterfind" title="Permalink to this definition">¶</a></dt> <dd><p>Finds all matching subelements, by tag name or path. Same as getroot().iterfind(match). Returns an iterable yielding all matching elements in document order.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.parse"> <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> <dd><p>Loads an external XML section into this element tree. <em>source</em> is a file name or file object. <em>parser</em> is an optional parser instance. If not given, the standard XMLParser parser is used. Returns the section root element.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.ElementTree.write"> <code class="descname">write</code><span class="sig-paren">(</span><em>file</em>, <em>encoding=&quot;us-ascii&quot;</em>, <em>xml_declaration=None</em>, <em>default_namespace=None</em>, <em>method=&quot;xml&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.write" title="Permalink to this definition">¶</a></dt> <dd><p>Writes the element tree to a file, as XML. <em>file</em> is a file name, or a file object opened for writing. <em>encoding</em> <a class="footnote-reference" href="#id5" id="id3">[1]</a> is the output encoding (default is US-ASCII). <em>xml_declaration</em> controls if an XML declaration should be added to the file. Use <code class="docutils literal notranslate"><span class="pre">False</span></code> for never, <code class="docutils literal notranslate"><span class="pre">True</span></code> for always, <code class="docutils literal notranslate"><span class="pre">None</span></code> for only if not US-ASCII or UTF-8 (default is <code class="docutils literal notranslate"><span class="pre">None</span></code>). <em>default_namespace</em> sets the default XML namespace (for “xmlns”). <em>method</em> is either <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;html&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;text&quot;</span></code> (default is <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>). Returns an encoded string.</p> </dd></dl> </dd></dl> <p>This is the XML file that is going to be manipulated:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">html</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="n">head</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="n">title</span><span class="o">&gt;</span><span class="n">Example</span> <span class="n">page</span><span class="o">&lt;/</span><span class="n">title</span><span class="o">&gt;</span> <span class="o">&lt;/</span><span class="n">head</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="n">body</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="n">p</span><span class="o">&gt;</span><span class="n">Moved</span> <span class="n">to</span> <span class="o">&lt;</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s2">&quot;http://example.org/&quot;</span><span class="o">&gt;</span><span class="n">example</span><span class="o">.</span><span class="n">org</span><span class="o">&lt;/</span><span class="n">a</span><span class="o">&gt;</span> <span class="ow">or</span> <span class="o">&lt;</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s2">&quot;http://example.com/&quot;</span><span class="o">&gt;</span><span class="n">example</span><span class="o">.</span><span class="n">com</span><span class="o">&lt;/</span><span class="n">a</span><span class="o">&gt;.&lt;/</span><span class="n">p</span><span class="o">&gt;</span> <span class="o">&lt;/</span><span class="n">body</span><span class="o">&gt;</span> <span class="o">&lt;/</span><span class="n">html</span><span class="o">&gt;</span> </pre></div> </div> <p>Example of changing the attribute “target” of every link in first paragraph:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">import</span> <span class="n">ElementTree</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span> <span class="o">=</span> <span class="n">ElementTree</span><span class="p">()</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;index.xhtml&quot;</span><span class="p">)</span> <span class="go">&lt;Element &#39;html&#39; at 0xb77e6fac&gt;</span> <span class="gp">&gt;&gt;&gt; </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">&quot;body/p&quot;</span><span class="p">)</span> <span class="c1"># Finds first occurrence of tag p in body</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="go">&lt;Element &#39;p&#39; at 0xb77ec26c&gt;</span> <span class="gp">&gt;&gt;&gt; </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">&quot;a&quot;</span><span class="p">))</span> <span class="c1"># Returns list of all links</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">links</span> <span class="go">[&lt;Element &#39;a&#39; at 0xb77ec2ac&gt;, &lt;Element &#39;a&#39; at 0xb77ec1cc&gt;]</span> <span class="gp">&gt;&gt;&gt; </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> <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">&quot;target&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;blank&quot;</span> <span class="gp">...</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;output.xhtml&quot;</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="qname-objects"> <span id="elementtree-qname-objects"></span><h3>19.7.3.4. QName Objects<a class="headerlink" href="#qname-objects" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="xml.etree.ElementTree.QName"> <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> <dd><p>QName wrapper. This can be used to wrap a QName attribute value, in order to get proper namespace handling on output. <em>text_or_uri</em> is a string containing the QName value, in the form {uri}local, or, if the tag argument is given, the URI part of a QName. If <em>tag</em> is given, the first argument is interpreted as a URI, and this argument is interpreted as a local name. <a class="reference internal" href="#xml.etree.ElementTree.QName" title="xml.etree.ElementTree.QName"><code class="xref py py-class docutils literal notranslate"><span class="pre">QName</span></code></a> instances are opaque.</p> </dd></dl> </div> <div class="section" id="treebuilder-objects"> <span id="elementtree-treebuilder-objects"></span><h3>19.7.3.5. TreeBuilder Objects<a class="headerlink" href="#treebuilder-objects" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="xml.etree.ElementTree.TreeBuilder"> <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> <dd><p>Generic element structure builder. This builder converts a sequence of start, data, and end method calls to a well-formed element structure. You can use this class to build an element structure using a custom XML parser, or a parser for some other XML-like format. The <em>element_factory</em> is called to create new <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> instances when given.</p> <dl class="method"> <dt id="xml.etree.ElementTree.TreeBuilder.close"> <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> <dd><p>Flushes the builder buffers, and returns the toplevel document 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 notranslate"><span class="pre">Element</span></code></a> instance.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.TreeBuilder.data"> <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> <dd><p>Adds text to the current element. <em>data</em> is a string. This should be either a bytestring, or a Unicode string.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.TreeBuilder.end"> <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> <dd><p>Closes the current element. <em>tag</em> is the element name. Returns the closed element.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.TreeBuilder.start"> <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> <dd><p>Opens a new element. <em>tag</em> is the element name. <em>attrs</em> is a dictionary containing element attributes. Returns the opened element.</p> </dd></dl> <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 notranslate"><span class="pre">TreeBuilder</span></code></a> object can provide the following method:</p> <dl class="method"> <dt id="xml.etree.ElementTree.TreeBuilder.doctype"> <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> <dd><p>Handles a doctype declaration. <em>name</em> is the doctype name. <em>pubid</em> is the public identifier. <em>system</em> is the system identifier. This method 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 notranslate"><span class="pre">TreeBuilder</span></code></a> class.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> </dd></dl> </div> <div class="section" id="xmlparser-objects"> <span id="elementtree-xmlparser-objects"></span><h3>19.7.3.6. XMLParser Objects<a class="headerlink" href="#xmlparser-objects" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="xml.etree.ElementTree.XMLParser"> <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> <dd><p><a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> structure builder for XML source data, based on the expat parser. <em>html</em> are predefined HTML entities. This flag is not supported by the current implementation. <em>target</em> is the target object. If omitted, the builder uses an instance of the standard TreeBuilder class. <em>encoding</em> <a class="footnote-reference" href="#id5" id="id4">[1]</a> is optional. If given, the value overrides the encoding specified in the XML file.</p> <dl class="method"> <dt id="xml.etree.ElementTree.XMLParser.close"> <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> <dd><p>Finishes feeding data to the parser. Returns an element structure.</p> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.XMLParser.doctype"> <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> <dd><div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.7: </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 notranslate"><span class="pre">TreeBuilder.doctype()</span></code></a> method on a custom TreeBuilder target.</p> </div> </dd></dl> <dl class="method"> <dt id="xml.etree.ElementTree.XMLParser.feed"> <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> <dd><p>Feeds data to the parser. <em>data</em> is encoded data.</p> </dd></dl> </dd></dl> <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 notranslate"><span class="pre">XMLParser.feed()</span></code></a> calls <em>target</em>’s <code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code> method for each opening tag, its <code class="xref py py-meth docutils literal notranslate"><span class="pre">end()</span></code> method for each closing tag, and data is processed by method <code class="xref py py-meth docutils literal notranslate"><span class="pre">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 notranslate"><span class="pre">XMLParser.close()</span></code></a> calls <em>target</em>’s method <code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">XMLParser</span></code></a> can be used not only for building a tree structure. This is an example of counting the maximum depth of an XML file:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">import</span> <span class="n">XMLParser</span> <span class="gp">&gt;&gt;&gt; </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> <span class="gp">... </span> <span class="n">maxDepth</span> <span class="o">=</span> <span class="mi">0</span> <span class="gp">... </span> <span class="n">depth</span> <span class="o">=</span> <span class="mi">0</span> <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> <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> <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">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span><span class="p">:</span> <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> <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> <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> <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> <span class="gp">... </span> <span class="k">pass</span> <span class="c1"># We do not need to do anything with data.</span> <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> <span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span> <span class="gp">...</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">MaxDepth</span><span class="p">()</span> <span class="gp">&gt;&gt;&gt; </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> <span class="gp">&gt;&gt;&gt; </span><span class="n">exampleXml</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span> <span class="gp">... </span><span class="s2">&lt;a&gt;</span> <span class="gp">... </span><span class="s2"> &lt;b&gt;</span> <span class="gp">... </span><span class="s2"> &lt;/b&gt;</span> <span class="gp">... </span><span class="s2"> &lt;b&gt;</span> <span class="gp">... </span><span class="s2"> &lt;c&gt;</span> <span class="gp">... </span><span class="s2"> &lt;d&gt;</span> <span class="gp">... </span><span class="s2"> &lt;/d&gt;</span> <span class="gp">... </span><span class="s2"> &lt;/c&gt;</span> <span class="gp">... </span><span class="s2"> &lt;/b&gt;</span> <span class="gp">... </span><span class="s2">&lt;/a&gt;&quot;&quot;&quot;</span> <span class="gp">&gt;&gt;&gt; </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> <span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="go">4</span> </pre></div> </div> <p class="rubric">Footnotes</p> <table class="docutils footnote" frame="void" id="id5" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label">[1]</td><td><em>(<a class="fn-backref" href="#id1">1</a>, <a class="fn-backref" href="#id2">2</a>, <a class="fn-backref" href="#id3">3</a>, <a class="fn-backref" href="#id4">4</a>)</em> The encoding string included in XML output should conform to the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is 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> 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> </tbody> </table> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">19.7. <code class="docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a><ul> <li><a class="reference internal" href="#tutorial">19.7.1. Tutorial</a><ul> <li><a class="reference internal" href="#xml-tree-and-elements">19.7.1.1. XML tree and elements</a></li> <li><a class="reference internal" href="#parsing-xml">19.7.1.2. Parsing XML</a></li> <li><a class="reference internal" href="#finding-interesting-elements">19.7.1.3. Finding interesting elements</a></li> <li><a class="reference internal" href="#modifying-an-xml-file">19.7.1.4. Modifying an XML File</a></li> <li><a class="reference internal" href="#building-xml-documents">19.7.1.5. Building XML documents</a></li> <li><a class="reference internal" href="#parsing-xml-with-namespaces">19.7.1.6. Parsing XML with Namespaces</a></li> <li><a class="reference internal" href="#additional-resources">19.7.1.7. Additional resources</a></li> </ul> </li> <li><a class="reference internal" href="#xpath-support">19.7.2. XPath support</a><ul> <li><a class="reference internal" href="#example">19.7.2.1. Example</a></li> <li><a class="reference internal" href="#supported-xpath-syntax">19.7.2.2. Supported XPath syntax</a></li> </ul> </li> <li><a class="reference internal" href="#reference">19.7.3. Reference</a><ul> <li><a class="reference internal" href="#functions">19.7.3.1. Functions</a></li> <li><a class="reference internal" href="#element-objects">19.7.3.2. Element Objects</a></li> <li><a class="reference internal" href="#elementtree-objects">19.7.3.3. ElementTree Objects</a></li> <li><a class="reference internal" href="#qname-objects">19.7.3.4. QName Objects</a></li> <li><a class="reference internal" href="#treebuilder-objects">19.7.3.5. TreeBuilder Objects</a></li> <li><a class="reference internal" href="#xmlparser-objects">19.7.3.6. XMLParser Objects</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="xml.html" title="previous chapter">19.5. XML Processing Modules</a></p> <h4>Next topic</h4> <p class="topless"><a href="xml.dom.html" title="next chapter">19.8. <code class="docutils literal notranslate"><span class="pre">xml.dom</span></code> — The Document Object Model API</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/xml.etree.elementtree.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="xml.dom.html" title="19.8. xml.dom — The Document Object Model API" >next</a> |</li> <li class="right" > <a href="xml.html" title="19.5. XML Processing Modules" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> &#187;</li> <li> <a href="../index.html">Python 2.7.16 documentation</a> &#187; </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li> <li class="nav-item nav-item-2"><a href="markup.html" >19. Structured Markup Processing Tools</a> &#187;</li> </ul> </div> <div class="footer"> &copy; <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Mar 27, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6. </div> </body> </html>