gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1961 line
198KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="xml.etree.ElementTree — The ElementTree XML API" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/xml.etree.elementtree.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/xml/etree/ElementTree.py The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Tutorial: This is a short tutorial for using xml...." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/xml/etree/ElementTree.py The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Tutorial: This is a short tutorial for using xml...." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>xml.etree.ElementTree — The ElementTree XML API &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="xml.dom — The Document Object Model API" href="xml.dom.html" />
  33. <link rel="prev" title="XML Processing Modules" href="xml.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/xml.etree.elementtree.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a><ul>
  86. <li><a class="reference internal" href="#tutorial">Tutorial</a><ul>
  87. <li><a class="reference internal" href="#xml-tree-and-elements">XML tree and elements</a></li>
  88. <li><a class="reference internal" href="#parsing-xml">Parsing XML</a></li>
  89. <li><a class="reference internal" href="#pull-api-for-non-blocking-parsing">Pull API for non-blocking parsing</a></li>
  90. <li><a class="reference internal" href="#finding-interesting-elements">Finding interesting elements</a></li>
  91. <li><a class="reference internal" href="#modifying-an-xml-file">Modifying an XML File</a></li>
  92. <li><a class="reference internal" href="#building-xml-documents">Building XML documents</a></li>
  93. <li><a class="reference internal" href="#parsing-xml-with-namespaces">Parsing XML with Namespaces</a></li>
  94. </ul>
  95. </li>
  96. <li><a class="reference internal" href="#xpath-support">XPath support</a><ul>
  97. <li><a class="reference internal" href="#example">Example</a></li>
  98. <li><a class="reference internal" href="#supported-xpath-syntax">Supported XPath syntax</a></li>
  99. </ul>
  100. </li>
  101. <li><a class="reference internal" href="#reference">Reference</a><ul>
  102. <li><a class="reference internal" href="#functions">Functions</a></li>
  103. </ul>
  104. </li>
  105. <li><a class="reference internal" href="#xinclude-support">XInclude support</a><ul>
  106. <li><a class="reference internal" href="#id3">Example</a></li>
  107. </ul>
  108. </li>
  109. <li><a class="reference internal" href="#id4">Reference</a><ul>
  110. <li><a class="reference internal" href="#elementinclude-functions">Functions</a></li>
  111. <li><a class="reference internal" href="#element-objects">Element Objects</a></li>
  112. <li><a class="reference internal" href="#elementtree-objects">ElementTree Objects</a></li>
  113. <li><a class="reference internal" href="#qname-objects">QName Objects</a></li>
  114. <li><a class="reference internal" href="#treebuilder-objects">TreeBuilder Objects</a></li>
  115. <li><a class="reference internal" href="#xmlparser-objects">XMLParser Objects</a></li>
  116. <li><a class="reference internal" href="#xmlpullparser-objects">XMLPullParser Objects</a></li>
  117. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  118. </ul>
  119. </li>
  120. </ul>
  121. </li>
  122. </ul>
  123. </div>
  124. <div>
  125. <h4>Previous topic</h4>
  126. <p class="topless"><a href="xml.html"
  127. title="previous chapter">XML Processing Modules</a></p>
  128. </div>
  129. <div>
  130. <h4>Next topic</h4>
  131. <p class="topless"><a href="xml.dom.html"
  132. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom</span></code> — The Document Object Model API</a></p>
  133. </div>
  134. <div role="note" aria-label="source link">
  135. <h3>This Page</h3>
  136. <ul class="this-page-menu">
  137. <li><a href="../bugs.html">Report a Bug</a></li>
  138. <li>
  139. <a href="https://github.com/python/cpython/blob/main/Doc/library/xml.etree.elementtree.rst"
  140. rel="nofollow">Show Source
  141. </a>
  142. </li>
  143. </ul>
  144. </div>
  145. </nav>
  146. </div>
  147. </div>
  148. <div class="related" role="navigation" aria-label="related navigation">
  149. <h3>Navigation</h3>
  150. <ul>
  151. <li class="right" style="margin-right: 10px">
  152. <a href="../genindex.html" title="General Index"
  153. accesskey="I">index</a></li>
  154. <li class="right" >
  155. <a href="../py-modindex.html" title="Python Module Index"
  156. >modules</a> |</li>
  157. <li class="right" >
  158. <a href="xml.dom.html" title="xml.dom — The Document Object Model API"
  159. accesskey="N">next</a> |</li>
  160. <li class="right" >
  161. <a href="xml.html" title="XML Processing Modules"
  162. accesskey="P">previous</a> |</li>
  163. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  164. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  165. <li class="switchers">
  166. <div class="language_switcher_placeholder"></div>
  167. <div class="version_switcher_placeholder"></div>
  168. </li>
  169. <li>
  170. </li>
  171. <li id="cpython-language-and-version">
  172. <a href="../index.html">3.12.3 Documentation</a> &#187;
  173. </li>
  174. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  175. <li class="nav-item nav-item-2"><a href="markup.html" accesskey="U">Structured Markup Processing Tools</a> &#187;</li>
  176. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a></li>
  177. <li class="right">
  178. <div class="inline-search" role="search">
  179. <form class="inline-search" action="../search.html" method="get">
  180. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  181. <input type="submit" value="Go" />
  182. </form>
  183. </div>
  184. |
  185. </li>
  186. <li class="right">
  187. <label class="theme-selector-label">
  188. Theme
  189. <select class="theme-selector" oninput="activateTheme(this.value)">
  190. <option value="auto" selected>Auto</option>
  191. <option value="light">Light</option>
  192. <option value="dark">Dark</option>
  193. </select>
  194. </label> |</li>
  195. </ul>
  196. </div>
  197. <div class="document">
  198. <div class="documentwrapper">
  199. <div class="bodywrapper">
  200. <div class="body" role="main">
  201. <section id="module-xml.etree.ElementTree">
  202. <span id="xml-etree-elementtree-the-elementtree-xml-api"></span><h1><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="Link to this heading">¶</a></h1>
  203. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/xml/etree/ElementTree.py">Lib/xml/etree/ElementTree.py</a></p>
  204. <hr class="docutils" />
  205. <p>The <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> module implements a simple and efficient API
  206. for parsing and creating XML data.</p>
  207. <div class="versionchanged">
  208. <p><span class="versionmodified changed">Changed in version 3.3: </span>This module will use a fast implementation whenever available.</p>
  209. </div>
  210. <div class="deprecated">
  211. <p><span class="versionmodified deprecated">Deprecated since version 3.3: </span>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.cElementTree</span></code> module is deprecated.</p>
  212. </div>
  213. <div class="admonition warning">
  214. <p class="admonition-title">Warning</p>
  215. <p>The <a class="reference internal" href="#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> module is not secure against
  216. maliciously constructed data. If you need to parse untrusted or
  217. unauthenticated data see <a class="reference internal" href="xml.html#xml-vulnerabilities"><span class="std std-ref">XML vulnerabilities</span></a>.</p>
  218. </div>
  219. <section id="tutorial">
  220. <h2>Tutorial<a class="headerlink" href="#tutorial" title="Link to this heading">¶</a></h2>
  221. <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
  222. short). The goal is to demonstrate some of the building blocks and basic
  223. concepts of the module.</p>
  224. <section id="xml-tree-and-elements">
  225. <h3>XML tree and elements<a class="headerlink" href="#xml-tree-and-elements" title="Link to this heading">¶</a></h3>
  226. <p>XML is an inherently hierarchical data format, and the most natural way to
  227. represent it is with a tree. <code class="docutils literal notranslate"><span class="pre">ET</span></code> has two classes for this purpose -
  228. <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
  229. <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
  230. the whole document (reading and writing to/from files) are usually done
  231. 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
  232. 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>
  233. </section>
  234. <section id="parsing-xml">
  235. <span id="elementtree-parsing-xml"></span><h3>Parsing XML<a class="headerlink" href="#parsing-xml" title="Link to this heading">¶</a></h3>
  236. <p>We’ll be using the fictive <code class="file docutils literal notranslate"><span class="pre">country_data.xml</span></code> XML document as the sample data for this section:</p>
  237. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
  238. <span class="nt">&lt;data&gt;</span>
  239. <span class="w"> </span><span class="nt">&lt;country</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span>
  240. <span class="w"> </span><span class="nt">&lt;rank&gt;</span>1<span class="nt">&lt;/rank&gt;</span>
  241. <span class="w"> </span><span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span>
  242. <span class="w"> </span><span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span>
  243. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Austria&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
  244. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
  245. <span class="w"> </span><span class="nt">&lt;/country&gt;</span>
  246. <span class="w"> </span><span class="nt">&lt;country</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span>
  247. <span class="w"> </span><span class="nt">&lt;rank&gt;</span>4<span class="nt">&lt;/rank&gt;</span>
  248. <span class="w"> </span><span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
  249. <span class="w"> </span><span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span>
  250. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span>
  251. <span class="w"> </span><span class="nt">&lt;/country&gt;</span>
  252. <span class="w"> </span><span class="nt">&lt;country</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Panama&quot;</span><span class="nt">&gt;</span>
  253. <span class="w"> </span><span class="nt">&lt;rank&gt;</span>68<span class="nt">&lt;/rank&gt;</span>
  254. <span class="w"> </span><span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
  255. <span class="w"> </span><span class="nt">&lt;gdppc&gt;</span>13600<span class="nt">&lt;/gdppc&gt;</span>
  256. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Costa Rica&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
  257. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Colombia&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
  258. <span class="w"> </span><span class="nt">&lt;/country&gt;</span>
  259. <span class="nt">&lt;/data&gt;</span>
  260. </pre></div>
  261. </div>
  262. <p>We can import this data by reading from a file:</p>
  263. <div class="highlight-python3 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>
  264. <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>
  265. <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>
  266. </pre></div>
  267. </div>
  268. <p>Or directly from a string:</p>
  269. <div class="highlight-python3 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>
  270. </pre></div>
  271. </div>
  272. <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>,
  273. which is the root element of the parsed tree. Other parsing functions may
  274. 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>
  275. <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>
  276. <div class="highlight-python3 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>
  277. <span class="go">&#39;data&#39;</span>
  278. <span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">attrib</span>
  279. <span class="go">{}</span>
  280. </pre></div>
  281. </div>
  282. <p>It also has children nodes over which we can iterate:</p>
  283. <div class="highlight-python3 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>
  284. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">child</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="n">child</span><span class="o">.</span><span class="n">attrib</span><span class="p">)</span>
  285. <span class="gp">...</span>
  286. <span class="go">country {&#39;name&#39;: &#39;Liechtenstein&#39;}</span>
  287. <span class="go">country {&#39;name&#39;: &#39;Singapore&#39;}</span>
  288. <span class="go">country {&#39;name&#39;: &#39;Panama&#39;}</span>
  289. </pre></div>
  290. </div>
  291. <p>Children are nested, and we can access specific child nodes by index:</p>
  292. <div class="highlight-python3 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>
  293. <span class="go">&#39;2008&#39;</span>
  294. </pre></div>
  295. </div>
  296. <div class="admonition note">
  297. <p class="admonition-title">Note</p>
  298. <p>Not all elements of the XML input will end up as elements of the
  299. parsed tree. Currently, this module skips over any XML comments,
  300. processing instructions, and document type declarations in the
  301. input. Nevertheless, trees built using this module’s API rather
  302. than parsing from XML text can have comments and processing
  303. instructions in them; they will be included when generating XML
  304. output. A document type declaration may be accessed by passing a
  305. 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> instance to the <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a>
  306. constructor.</p>
  307. </div>
  308. </section>
  309. <section id="pull-api-for-non-blocking-parsing">
  310. <span id="elementtree-pull-parsing"></span><h3>Pull API for non-blocking parsing<a class="headerlink" href="#pull-api-for-non-blocking-parsing" title="Link to this heading">¶</a></h3>
  311. <p>Most parsing functions provided by this module require the whole document
  312. to be read at once before returning any result. It is possible to use an
  313. <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> and feed data into it incrementally, but it is a push API that
  314. calls methods on a callback target, which is too low-level and inconvenient for
  315. most needs. Sometimes what the user really wants is to be able to parse XML
  316. incrementally, without blocking operations, while enjoying the convenience of
  317. fully constructed <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.</p>
  318. <p>The most powerful tool for doing this is <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLPullParser</span></code></a>. It does not
  319. require a blocking read to obtain the XML data, and is instead fed with data
  320. incrementally with <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.feed" title="xml.etree.ElementTree.XMLPullParser.feed"><code class="xref py py-meth docutils literal notranslate"><span class="pre">XMLPullParser.feed()</span></code></a> calls. To get the parsed XML
  321. elements, call <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><code class="xref py py-meth docutils literal notranslate"><span class="pre">XMLPullParser.read_events()</span></code></a>. Here is an example:</p>
  322. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XMLPullParser</span><span class="p">([</span><span class="s1">&#39;start&#39;</span><span class="p">,</span> <span class="s1">&#39;end&#39;</span><span class="p">])</span>
  323. <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="s1">&#39;&lt;mytag&gt;sometext&#39;</span><span class="p">)</span>
  324. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">read_events</span><span class="p">())</span>
  325. <span class="go">[(&#39;start&#39;, &lt;Element &#39;mytag&#39; at 0x7fa66db2be58&gt;)]</span>
  326. <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="s1">&#39; more text&lt;/mytag&gt;&#39;</span><span class="p">)</span>
  327. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">event</span><span class="p">,</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">parser</span><span class="o">.</span><span class="n">read_events</span><span class="p">():</span>
  328. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
  329. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">elem</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="s1">&#39;text=&#39;</span><span class="p">,</span> <span class="n">elem</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
  330. <span class="gp">...</span>
  331. <span class="go">end</span>
  332. <span class="go">mytag text= sometext more text</span>
  333. </pre></div>
  334. </div>
  335. <p>The obvious use case is applications that operate in a non-blocking fashion
  336. where the XML data is being received from a socket or read incrementally from
  337. some storage device. In such cases, blocking reads are unacceptable.</p>
  338. <p>Because it’s so flexible, <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLPullParser</span></code></a> can be inconvenient to use for
  339. simpler use-cases. If you don’t mind your application blocking on reading XML
  340. data but would still like to have incremental parsing capabilities, take a look
  341. at <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>. It can be useful when you’re reading a large XML document
  342. and don’t want to hold it wholly in memory.</p>
  343. <p>Where <em>immediate</em> feedback through events is wanted, calling method
  344. <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.flush" title="xml.etree.ElementTree.XMLPullParser.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">XMLPullParser.flush()</span></code></a> can help reduce delay;
  345. please make sure to study the related security notes.</p>
  346. </section>
  347. <section id="finding-interesting-elements">
  348. <h3>Finding interesting elements<a class="headerlink" href="#finding-interesting-elements" title="Link to this heading">¶</a></h3>
  349. <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
  350. the sub-tree below it (its children, their children, and so on). For example,
  351. <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>
  352. <div class="highlight-python3 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>
  353. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">neighbor</span><span class="o">.</span><span class="n">attrib</span><span class="p">)</span>
  354. <span class="gp">...</span>
  355. <span class="go">{&#39;name&#39;: &#39;Austria&#39;, &#39;direction&#39;: &#39;E&#39;}</span>
  356. <span class="go">{&#39;name&#39;: &#39;Switzerland&#39;, &#39;direction&#39;: &#39;W&#39;}</span>
  357. <span class="go">{&#39;name&#39;: &#39;Malaysia&#39;, &#39;direction&#39;: &#39;N&#39;}</span>
  358. <span class="go">{&#39;name&#39;: &#39;Costa Rica&#39;, &#39;direction&#39;: &#39;W&#39;}</span>
  359. <span class="go">{&#39;name&#39;: &#39;Colombia&#39;, &#39;direction&#39;: &#39;E&#39;}</span>
  360. </pre></div>
  361. </div>
  362. <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
  363. 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
  364. 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
  365. 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>
  366. <div class="highlight-python3 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>
  367. <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>
  368. <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>
  369. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">rank</span><span class="p">)</span>
  370. <span class="gp">...</span>
  371. <span class="go">Liechtenstein 1</span>
  372. <span class="go">Singapore 4</span>
  373. <span class="go">Panama 68</span>
  374. </pre></div>
  375. </div>
  376. <p>More sophisticated specification of which elements to look for is possible by
  377. using <a class="reference internal" href="#elementtree-xpath"><span class="std std-ref">XPath</span></a>.</p>
  378. </section>
  379. <section id="modifying-an-xml-file">
  380. <h3>Modifying an XML File<a class="headerlink" href="#modifying-an-xml-file" title="Link to this heading">¶</a></h3>
  381. <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.
  382. 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>
  383. <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
  384. 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
  385. (<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
  386. 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>
  387. <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>
  388. attribute to the rank element:</p>
  389. <div class="highlight-python3 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>
  390. <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>
  391. <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>
  392. <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>
  393. <span class="gp">...</span>
  394. <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>
  395. </pre></div>
  396. </div>
  397. <p>Our XML now looks like this:</p>
  398. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
  399. <span class="nt">&lt;data&gt;</span>
  400. <span class="w"> </span><span class="nt">&lt;country</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span>
  401. <span class="w"> </span><span class="nt">&lt;rank</span><span class="w"> </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>
  402. <span class="w"> </span><span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span>
  403. <span class="w"> </span><span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span>
  404. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Austria&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
  405. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
  406. <span class="w"> </span><span class="nt">&lt;/country&gt;</span>
  407. <span class="w"> </span><span class="nt">&lt;country</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span>
  408. <span class="w"> </span><span class="nt">&lt;rank</span><span class="w"> </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>
  409. <span class="w"> </span><span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
  410. <span class="w"> </span><span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span>
  411. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span>
  412. <span class="w"> </span><span class="nt">&lt;/country&gt;</span>
  413. <span class="w"> </span><span class="nt">&lt;country</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Panama&quot;</span><span class="nt">&gt;</span>
  414. <span class="w"> </span><span class="nt">&lt;rank</span><span class="w"> </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>
  415. <span class="w"> </span><span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
  416. <span class="w"> </span><span class="nt">&lt;gdppc&gt;</span>13600<span class="nt">&lt;/gdppc&gt;</span>
  417. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Costa Rica&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
  418. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Colombia&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
  419. <span class="w"> </span><span class="nt">&lt;/country&gt;</span>
  420. <span class="nt">&lt;/data&gt;</span>
  421. </pre></div>
  422. </div>
  423. <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
  424. remove all countries with a rank higher than 50:</p>
  425. <div class="highlight-python3 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>
  426. <span class="gp">... </span> <span class="c1"># using root.findall() to avoid removal during traversal</span>
  427. <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>
  428. <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>
  429. <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>
  430. <span class="gp">...</span>
  431. <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>
  432. </pre></div>
  433. </div>
  434. <p>Note that concurrent modification while iterating can lead to problems,
  435. just like when iterating and modifying Python lists or dicts.
  436. Therefore, the example first collects all matching elements with
  437. <code class="docutils literal notranslate"><span class="pre">root.findall()</span></code>, and only then iterates over the list of matches.</p>
  438. <p>Our XML now looks like this:</p>
  439. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
  440. <span class="nt">&lt;data&gt;</span>
  441. <span class="w"> </span><span class="nt">&lt;country</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Liechtenstein&quot;</span><span class="nt">&gt;</span>
  442. <span class="w"> </span><span class="nt">&lt;rank</span><span class="w"> </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>
  443. <span class="w"> </span><span class="nt">&lt;year&gt;</span>2008<span class="nt">&lt;/year&gt;</span>
  444. <span class="w"> </span><span class="nt">&lt;gdppc&gt;</span>141100<span class="nt">&lt;/gdppc&gt;</span>
  445. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Austria&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;E&quot;</span><span class="nt">/&gt;</span>
  446. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Switzerland&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;W&quot;</span><span class="nt">/&gt;</span>
  447. <span class="w"> </span><span class="nt">&lt;/country&gt;</span>
  448. <span class="w"> </span><span class="nt">&lt;country</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Singapore&quot;</span><span class="nt">&gt;</span>
  449. <span class="w"> </span><span class="nt">&lt;rank</span><span class="w"> </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>
  450. <span class="w"> </span><span class="nt">&lt;year&gt;</span>2011<span class="nt">&lt;/year&gt;</span>
  451. <span class="w"> </span><span class="nt">&lt;gdppc&gt;</span>59900<span class="nt">&lt;/gdppc&gt;</span>
  452. <span class="w"> </span><span class="nt">&lt;neighbor</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;Malaysia&quot;</span><span class="w"> </span><span class="na">direction=</span><span class="s">&quot;N&quot;</span><span class="nt">/&gt;</span>
  453. <span class="w"> </span><span class="nt">&lt;/country&gt;</span>
  454. <span class="nt">&lt;/data&gt;</span>
  455. </pre></div>
  456. </div>
  457. </section>
  458. <section id="building-xml-documents">
  459. <h3>Building XML documents<a class="headerlink" href="#building-xml-documents" title="Link to this heading">¶</a></h3>
  460. <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
  461. sub-elements for a given element:</p>
  462. <div class="highlight-python3 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>
  463. <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>
  464. <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>
  465. <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>
  466. <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>
  467. <span class="go">&lt;a&gt;&lt;b /&gt;&lt;c&gt;&lt;d /&gt;&lt;/c&gt;&lt;/a&gt;</span>
  468. </pre></div>
  469. </div>
  470. </section>
  471. <section id="parsing-xml-with-namespaces">
  472. <h3>Parsing XML with Namespaces<a class="headerlink" href="#parsing-xml-with-namespaces" title="Link to this heading">¶</a></h3>
  473. <p>If the XML input has <a class="reference external" href="https://en.wikipedia.org/wiki/XML_namespace">namespaces</a>, tags and attributes
  474. with prefixes in the form <code class="docutils literal notranslate"><span class="pre">prefix:sometag</span></code> get expanded to
  475. <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>.
  476. Also, if there is a <a class="reference external" href="https://www.w3.org/TR/xml-names/#defaulting">default namespace</a>,
  477. that full URI gets prepended to all of the non-prefixed tags.</p>
  478. <p>Here is an XML example that incorporates two namespaces, one with the
  479. prefix “fictional” and the other serving as the default namespace:</p>
  480. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
  481. <span class="nt">&lt;actors</span><span class="w"> </span><span class="na">xmlns:fictional=</span><span class="s">&quot;http://characters.example.com&quot;</span>
  482. <span class="w"> </span><span class="na">xmlns=</span><span class="s">&quot;http://people.example.com&quot;</span><span class="nt">&gt;</span>
  483. <span class="w"> </span><span class="nt">&lt;actor&gt;</span>
  484. <span class="w"> </span><span class="nt">&lt;name&gt;</span>John<span class="w"> </span>Cleese<span class="nt">&lt;/name&gt;</span>
  485. <span class="w"> </span><span class="nt">&lt;fictional:character&gt;</span>Lancelot<span class="nt">&lt;/fictional:character&gt;</span>
  486. <span class="w"> </span><span class="nt">&lt;fictional:character&gt;</span>Archie<span class="w"> </span>Leach<span class="nt">&lt;/fictional:character&gt;</span>
  487. <span class="w"> </span><span class="nt">&lt;/actor&gt;</span>
  488. <span class="w"> </span><span class="nt">&lt;actor&gt;</span>
  489. <span class="w"> </span><span class="nt">&lt;name&gt;</span>Eric<span class="w"> </span>Idle<span class="nt">&lt;/name&gt;</span>
  490. <span class="w"> </span><span class="nt">&lt;fictional:character&gt;</span>Sir<span class="w"> </span>Robin<span class="nt">&lt;/fictional:character&gt;</span>
  491. <span class="w"> </span><span class="nt">&lt;fictional:character&gt;</span>Gunther<span class="nt">&lt;/fictional:character&gt;</span>
  492. <span class="w"> </span><span class="nt">&lt;fictional:character&gt;</span>Commander<span class="w"> </span>Clement<span class="nt">&lt;/fictional:character&gt;</span>
  493. <span class="w"> </span><span class="nt">&lt;/actor&gt;</span>
  494. <span class="nt">&lt;/actors&gt;</span>
  495. </pre></div>
  496. </div>
  497. <p>One way to search and explore this XML example is to manually add the
  498. URI to every tag or attribute in the xpath of a
  499. <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>
  500. <div class="highlight-python3 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>
  501. <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>
  502. <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>
  503. <span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
  504. <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>
  505. <span class="nb">print</span><span class="p">(</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><span class="p">)</span>
  506. </pre></div>
  507. </div>
  508. <p>A better way to search the namespaced XML example is to create a
  509. dictionary with your own prefixes and use those in the search functions:</p>
  510. <div class="highlight-python3 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>
  511. <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>
  512. <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>
  513. <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>
  514. <span class="nb">print</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
  515. <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>
  516. <span class="nb">print</span><span class="p">(</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><span class="p">)</span>
  517. </pre></div>
  518. </div>
  519. <p>These two approaches both output:</p>
  520. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">John</span> <span class="n">Cleese</span>
  521. <span class="o">|--&gt;</span> <span class="n">Lancelot</span>
  522. <span class="o">|--&gt;</span> <span class="n">Archie</span> <span class="n">Leach</span>
  523. <span class="n">Eric</span> <span class="n">Idle</span>
  524. <span class="o">|--&gt;</span> <span class="n">Sir</span> <span class="n">Robin</span>
  525. <span class="o">|--&gt;</span> <span class="n">Gunther</span>
  526. <span class="o">|--&gt;</span> <span class="n">Commander</span> <span class="n">Clement</span>
  527. </pre></div>
  528. </div>
  529. </section>
  530. </section>
  531. <section id="xpath-support">
  532. <span id="elementtree-xpath"></span><h2>XPath support<a class="headerlink" href="#xpath-support" title="Link to this heading">¶</a></h2>
  533. <p>This module provides limited support for
  534. <a class="reference external" href="https://www.w3.org/TR/xpath">XPath expressions</a> for locating elements in a
  535. tree. The goal is to support a small subset of the abbreviated syntax; a full
  536. XPath engine is outside the scope of the module.</p>
  537. <section id="example">
  538. <h3>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h3>
  539. <p>Here’s an example that demonstrates some of the XPath capabilities of the
  540. module. We’ll be using the <code class="docutils literal notranslate"><span class="pre">countrydata</span></code> XML document from the
  541. <a class="reference internal" href="#elementtree-parsing-xml"><span class="std std-ref">Parsing XML</span></a> section:</p>
  542. <div class="highlight-python3 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>
  543. <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>
  544. <span class="c1"># Top-level elements</span>
  545. <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>
  546. <span class="c1"># All &#39;neighbor&#39; grand-children of &#39;country&#39; children of the top-level</span>
  547. <span class="c1"># elements</span>
  548. <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>
  549. <span class="c1"># Nodes with name=&#39;Singapore&#39; that have a &#39;year&#39; child</span>
  550. <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>
  551. <span class="c1"># &#39;year&#39; nodes that are children of nodes with name=&#39;Singapore&#39;</span>
  552. <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>
  553. <span class="c1"># All &#39;neighbor&#39; nodes that are the second child of their parent</span>
  554. <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>
  555. </pre></div>
  556. </div>
  557. <p>For XML with namespaces, use the usual qualified <code class="docutils literal notranslate"><span class="pre">{namespace}tag</span></code> notation:</p>
  558. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># All dublin-core &quot;title&quot; tags in the document</span>
  559. <span class="n">root</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">&quot;.//{http://purl.org/dc/elements/1.1/}title&quot;</span><span class="p">)</span>
  560. </pre></div>
  561. </div>
  562. </section>
  563. <section id="supported-xpath-syntax">
  564. <h3>Supported XPath syntax<a class="headerlink" href="#supported-xpath-syntax" title="Link to this heading">¶</a></h3>
  565. <table class="docutils align-default">
  566. <thead>
  567. <tr class="row-odd"><th class="head"><p>Syntax</p></th>
  568. <th class="head"><p>Meaning</p></th>
  569. </tr>
  570. </thead>
  571. <tbody>
  572. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">tag</span></code></p></td>
  573. <td><p>Selects all child elements with the given tag.
  574. For example, <code class="docutils literal notranslate"><span class="pre">spam</span></code> selects all child elements
  575. 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
  576. grandchildren named <code class="docutils literal notranslate"><span class="pre">egg</span></code> in all children named
  577. <code class="docutils literal notranslate"><span class="pre">spam</span></code>. <code class="docutils literal notranslate"><span class="pre">{namespace}*</span></code> selects all tags in the
  578. given namespace, <code class="docutils literal notranslate"><span class="pre">{*}spam</span></code> selects tags named
  579. <code class="docutils literal notranslate"><span class="pre">spam</span></code> in any (or no) namespace, and <code class="docutils literal notranslate"><span class="pre">{}*</span></code>
  580. only selects tags that are not in a namespace.</p>
  581. <div class="versionchanged">
  582. <p><span class="versionmodified changed">Changed in version 3.8: </span>Support for star-wildcards was added.</p>
  583. </div>
  584. </td>
  585. </tr>
  586. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">*</span></code></p></td>
  587. <td><p>Selects all child elements, including comments and
  588. processing instructions. For example, <code class="docutils literal notranslate"><span class="pre">*/egg</span></code>
  589. selects all grandchildren named <code class="docutils literal notranslate"><span class="pre">egg</span></code>.</p></td>
  590. </tr>
  591. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">.</span></code></p></td>
  592. <td><p>Selects the current node. This is mostly useful
  593. at the beginning of the path, to indicate that it’s
  594. a relative path.</p></td>
  595. </tr>
  596. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">//</span></code></p></td>
  597. <td><p>Selects all subelements, on all levels beneath the
  598. current element. For example, <code class="docutils literal notranslate"><span class="pre">.//egg</span></code> selects
  599. all <code class="docutils literal notranslate"><span class="pre">egg</span></code> elements in the entire tree.</p></td>
  600. </tr>
  601. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">..</span></code></p></td>
  602. <td><p>Selects the parent element. Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> if the
  603. path attempts to reach the ancestors of the start
  604. element (the element <code class="docutils literal notranslate"><span class="pre">find</span></code> was called on).</p></td>
  605. </tr>
  606. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">[&#64;attrib]</span></code></p></td>
  607. <td><p>Selects all elements that have the given attribute.</p></td>
  608. </tr>
  609. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">[&#64;attrib='value']</span></code></p></td>
  610. <td><p>Selects all elements for which the given attribute
  611. has the given value. The value cannot contain
  612. quotes.</p></td>
  613. </tr>
  614. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">[&#64;attrib!='value']</span></code></p></td>
  615. <td><p>Selects all elements for which the given attribute
  616. does not have the given value. The value cannot
  617. contain quotes.</p>
  618. <div class="versionadded">
  619. <p><span class="versionmodified added">New in version 3.10.</span></p>
  620. </div>
  621. </td>
  622. </tr>
  623. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">[tag]</span></code></p></td>
  624. <td><p>Selects all elements that have a child named
  625. <code class="docutils literal notranslate"><span class="pre">tag</span></code>. Only immediate children are supported.</p></td>
  626. </tr>
  627. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">[.='text']</span></code></p></td>
  628. <td><p>Selects all elements whose complete text content,
  629. including descendants, equals the given <code class="docutils literal notranslate"><span class="pre">text</span></code>.</p>
  630. <div class="versionadded">
  631. <p><span class="versionmodified added">New in version 3.7.</span></p>
  632. </div>
  633. </td>
  634. </tr>
  635. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">[.!='text']</span></code></p></td>
  636. <td><p>Selects all elements whose complete text content,
  637. including descendants, does not equal the given
  638. <code class="docutils literal notranslate"><span class="pre">text</span></code>.</p>
  639. <div class="versionadded">
  640. <p><span class="versionmodified added">New in version 3.10.</span></p>
  641. </div>
  642. </td>
  643. </tr>
  644. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">[tag='text']</span></code></p></td>
  645. <td><p>Selects all elements that have a child named
  646. <code class="docutils literal notranslate"><span class="pre">tag</span></code> whose complete text content, including
  647. descendants, equals the given <code class="docutils literal notranslate"><span class="pre">text</span></code>.</p></td>
  648. </tr>
  649. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">[tag!='text']</span></code></p></td>
  650. <td><p>Selects all elements that have a child named
  651. <code class="docutils literal notranslate"><span class="pre">tag</span></code> whose complete text content, including
  652. descendants, does not equal the given <code class="docutils literal notranslate"><span class="pre">text</span></code>.</p>
  653. <div class="versionadded">
  654. <p><span class="versionmodified added">New in version 3.10.</span></p>
  655. </div>
  656. </td>
  657. </tr>
  658. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">[position]</span></code></p></td>
  659. <td><p>Selects all elements that are located at the given
  660. position. The position can be either an integer
  661. (1 is the first position), the expression <code class="docutils literal notranslate"><span class="pre">last()</span></code>
  662. (for the last position), or a position relative to
  663. the last position (e.g. <code class="docutils literal notranslate"><span class="pre">last()-1</span></code>).</p></td>
  664. </tr>
  665. </tbody>
  666. </table>
  667. <p>Predicates (expressions within square brackets) must be preceded by a tag
  668. name, an asterisk, or another predicate. <code class="docutils literal notranslate"><span class="pre">position</span></code> predicates must be
  669. preceded by a tag name.</p>
  670. </section>
  671. </section>
  672. <section id="reference">
  673. <h2>Reference<a class="headerlink" href="#reference" title="Link to this heading">¶</a></h2>
  674. <section id="functions">
  675. <span id="elementtree-functions"></span><h3>Functions<a class="headerlink" href="#functions" title="Link to this heading">¶</a></h3>
  676. <dl class="py function">
  677. <dt class="sig sig-object py" id="xml.etree.ElementTree.canonicalize">
  678. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">canonicalize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">xml_data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">out</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">from_file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">options</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.canonicalize" title="Link to this definition">¶</a></dt>
  679. <dd><p><a class="reference external" href="https://www.w3.org/TR/xml-c14n2/">C14N 2.0</a> transformation function.</p>
  680. <p>Canonicalization is a way to normalise XML output in a way that allows
  681. byte-by-byte comparisons and digital signatures. It reduced the freedom
  682. that XML serializers have and instead generates a more constrained XML
  683. representation. The main restrictions regard the placement of namespace
  684. declarations, the ordering of attributes, and ignorable whitespace.</p>
  685. <p>This function takes an XML data string (<em>xml_data</em>) or a file path or
  686. file-like object (<em>from_file</em>) as input, converts it to the canonical
  687. form, and writes it out using the <em>out</em> file(-like) object, if provided,
  688. or returns it as a text string if not. The output file receives text,
  689. not bytes. It should therefore be opened in text mode with <code class="docutils literal notranslate"><span class="pre">utf-8</span></code>
  690. encoding.</p>
  691. <p>Typical uses:</p>
  692. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">xml_data</span> <span class="o">=</span> <span class="s2">&quot;&lt;root&gt;...&lt;/root&gt;&quot;</span>
  693. <span class="nb">print</span><span class="p">(</span><span class="n">canonicalize</span><span class="p">(</span><span class="n">xml_data</span><span class="p">))</span>
  694. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;c14n_output.xml&quot;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">out_file</span><span class="p">:</span>
  695. <span class="n">canonicalize</span><span class="p">(</span><span class="n">xml_data</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="n">out_file</span><span class="p">)</span>
  696. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;c14n_output.xml&quot;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">out_file</span><span class="p">:</span>
  697. <span class="n">canonicalize</span><span class="p">(</span><span class="n">from_file</span><span class="o">=</span><span class="s2">&quot;inputfile.xml&quot;</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="n">out_file</span><span class="p">)</span>
  698. </pre></div>
  699. </div>
  700. <p>The configuration <em>options</em> are as follows:</p>
  701. <ul class="simple">
  702. <li><p><em>with_comments</em>: set to true to include comments (default: false)</p></li>
  703. <li><dl class="simple">
  704. <dt><em>strip_text</em>: set to true to strip whitespace before and after text content</dt><dd><p>(default: false)</p>
  705. </dd>
  706. </dl>
  707. </li>
  708. <li><dl class="simple">
  709. <dt><em>rewrite_prefixes</em>: set to true to replace namespace prefixes by “n{number}”</dt><dd><p>(default: false)</p>
  710. </dd>
  711. </dl>
  712. </li>
  713. <li><dl class="simple">
  714. <dt><em>qname_aware_tags</em>: a set of qname aware tag names in which prefixes</dt><dd><p>should be replaced in text content (default: empty)</p>
  715. </dd>
  716. </dl>
  717. </li>
  718. <li><dl class="simple">
  719. <dt><em>qname_aware_attrs</em>: a set of qname aware attribute names in which prefixes</dt><dd><p>should be replaced in text content (default: empty)</p>
  720. </dd>
  721. </dl>
  722. </li>
  723. <li><p><em>exclude_attrs</em>: a set of attribute names that should not be serialised</p></li>
  724. <li><p><em>exclude_tags</em>: a set of tag names that should not be serialised</p></li>
  725. </ul>
  726. <p>In the option list above, “a set” refers to any collection or iterable of
  727. strings, no ordering is expected.</p>
  728. <div class="versionadded">
  729. <p><span class="versionmodified added">New in version 3.8.</span></p>
  730. </div>
  731. </dd></dl>
  732. <dl class="py function">
  733. <dt class="sig sig-object py" id="xml.etree.ElementTree.Comment">
  734. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">Comment</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Comment" title="Link to this definition">¶</a></dt>
  735. <dd><p>Comment element factory. This factory function creates a special element
  736. that will be serialized as an XML comment by the standard serializer. The
  737. comment string can be either a bytestring or a Unicode string. <em>text</em> is a
  738. string containing the comment string. Returns an element instance
  739. representing a comment.</p>
  740. <p>Note that <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> skips over comments in the input
  741. instead of creating comment objects for them. An <a class="reference internal" href="#xml.etree.ElementTree.ElementTree" title="xml.etree.ElementTree.ElementTree"><code class="xref py py-class docutils literal notranslate"><span class="pre">ElementTree</span></code></a> will
  742. only contain comment nodes if they have been inserted into to
  743. the tree using one of the <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> methods.</p>
  744. </dd></dl>
  745. <dl class="py function">
  746. <dt class="sig sig-object py" id="xml.etree.ElementTree.dump">
  747. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">dump</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">elem</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.dump" title="Link to this definition">¶</a></dt>
  748. <dd><p>Writes an element tree or element structure to sys.stdout. This function
  749. should be used for debugging only.</p>
  750. <p>The exact output format is implementation dependent. In this version, it’s
  751. written as an ordinary XML file.</p>
  752. <p><em>elem</em> is an element tree or an individual element.</p>
  753. <div class="versionchanged">
  754. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <a class="reference internal" href="#xml.etree.ElementTree.dump" title="xml.etree.ElementTree.dump"><code class="xref py py-func docutils literal notranslate"><span class="pre">dump()</span></code></a> function now preserves the attribute order specified
  755. by the user.</p>
  756. </div>
  757. </dd></dl>
  758. <dl class="py function">
  759. <dt class="sig sig-object py" id="xml.etree.ElementTree.fromstring">
  760. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">fromstring</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.fromstring" title="Link to this definition">¶</a></dt>
  761. <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>
  762. is a string containing XML data. <em>parser</em> is an optional parser instance.
  763. 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.
  764. 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>
  765. </dd></dl>
  766. <dl class="py function">
  767. <dt class="sig sig-object py" id="xml.etree.ElementTree.fromstringlist">
  768. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">fromstringlist</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sequence</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.fromstringlist" title="Link to this definition">¶</a></dt>
  769. <dd><p>Parses an XML document from a sequence of string fragments. <em>sequence</em> is a
  770. list or other sequence containing XML data fragments. <em>parser</em> is an
  771. 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>
  772. 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>
  773. <div class="versionadded">
  774. <p><span class="versionmodified added">New in version 3.2.</span></p>
  775. </div>
  776. </dd></dl>
  777. <dl class="py function">
  778. <dt class="sig sig-object py" id="xml.etree.ElementTree.indent">
  779. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">indent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tree</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">space</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'</span>&#160; <span class="pre">'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.indent" title="Link to this definition">¶</a></dt>
  780. <dd><p>Appends whitespace to the subtree to indent the tree visually.
  781. This can be used to generate pretty-printed XML output.
  782. <em>tree</em> can be an Element or ElementTree. <em>space</em> is the whitespace
  783. string that will be inserted for each indentation level, two space
  784. characters by default. For indenting partial subtrees inside of an
  785. already indented tree, pass the initial indentation level as <em>level</em>.</p>
  786. <div class="versionadded">
  787. <p><span class="versionmodified added">New in version 3.9.</span></p>
  788. </div>
  789. </dd></dl>
  790. <dl class="py function">
  791. <dt class="sig sig-object py" id="xml.etree.ElementTree.iselement">
  792. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">iselement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.iselement" title="Link to this definition">¶</a></dt>
  793. <dd><p>Check if an object appears to be a valid element object. <em>element</em> is an
  794. element instance. Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this is an element object.</p>
  795. </dd></dl>
  796. <dl class="py function">
  797. <dt class="sig sig-object py" id="xml.etree.ElementTree.iterparse">
  798. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">iterparse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">events</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.iterparse" title="Link to this definition">¶</a></dt>
  799. <dd><p>Parses an XML section into an element tree incrementally, and reports what’s
  800. going on to the user. <em>source</em> is a filename or <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>
  801. containing XML data. <em>events</em> is a sequence of events to report back. The
  802. supported events are the strings <code class="docutils literal notranslate"><span class="pre">&quot;start&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;end&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;comment&quot;</span></code>,
  803. <code class="docutils literal notranslate"><span class="pre">&quot;pi&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;start-ns&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;end-ns&quot;</span></code>
  804. (the “ns” events are used to get detailed namespace
  805. information). If <em>events</em> is omitted, only <code class="docutils literal notranslate"><span class="pre">&quot;end&quot;</span></code> events are reported.
  806. <em>parser</em> is an optional parser instance. If not given, the standard
  807. <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> must be a subclass of
  808. <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> and can only use the default <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">TreeBuilder</span></code></a> as a
  809. target. Returns an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> providing <code class="docutils literal notranslate"><span class="pre">(event,</span> <span class="pre">elem)</span></code> pairs;
  810. it has a <code class="docutils literal notranslate"><span class="pre">root</span></code> attribute that references the root element of the
  811. resulting XML tree once <em>source</em> is fully read.</p>
  812. <p>Note that while <a class="reference internal" href="#xml.etree.ElementTree.iterparse" title="xml.etree.ElementTree.iterparse"><code class="xref py py-func docutils literal notranslate"><span class="pre">iterparse()</span></code></a> builds the tree incrementally, it issues
  813. blocking reads on <em>source</em> (or the file it names). As such, it’s unsuitable
  814. for applications where blocking reads can’t be made. For fully non-blocking
  815. parsing, see <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLPullParser</span></code></a>.</p>
  816. <div class="admonition note">
  817. <p class="admonition-title">Note</p>
  818. <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
  819. starting tag when it emits a “start” event, so the attributes are defined,
  820. but the contents of the text and tail attributes are undefined at that
  821. point. The same applies to the element children; they may or may not be
  822. present.</p>
  823. <p>If you need a fully populated element, look for “end” events instead.</p>
  824. </div>
  825. <div class="deprecated">
  826. <p><span class="versionmodified deprecated">Deprecated since version 3.4: </span>The <em>parser</em> argument.</p>
  827. </div>
  828. <div class="versionchanged">
  829. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <code class="docutils literal notranslate"><span class="pre">comment</span></code> and <code class="docutils literal notranslate"><span class="pre">pi</span></code> events were added.</p>
  830. </div>
  831. </dd></dl>
  832. <dl class="py function">
  833. <dt class="sig sig-object py" id="xml.etree.ElementTree.parse">
  834. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.parse" title="Link to this definition">¶</a></dt>
  835. <dd><p>Parses an XML section into an element tree. <em>source</em> is a filename or file
  836. object containing XML data. <em>parser</em> is an optional parser instance. If
  837. 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
  838. <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>
  839. </dd></dl>
  840. <dl class="py function">
  841. <dt class="sig sig-object py" id="xml.etree.ElementTree.ProcessingInstruction">
  842. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">ProcessingInstruction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">text</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ProcessingInstruction" title="Link to this definition">¶</a></dt>
  843. <dd><p>PI element factory. This factory function creates a special element that
  844. will be serialized as an XML processing instruction. <em>target</em> is a string
  845. containing the PI target. <em>text</em> is a string containing the PI contents, if
  846. given. Returns an element instance, representing a processing instruction.</p>
  847. <p>Note that <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> skips over processing instructions
  848. in the input instead of creating PI objects for them. An
  849. <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> will only contain processing instruction nodes if
  850. they have been inserted into to the tree using one of the
  851. <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> methods.</p>
  852. </dd></dl>
  853. <dl class="py function">
  854. <dt class="sig sig-object py" id="xml.etree.ElementTree.register_namespace">
  855. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">register_namespace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uri</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.register_namespace" title="Link to this definition">¶</a></dt>
  856. <dd><p>Registers a namespace prefix. The registry is global, and any existing
  857. mapping for either the given prefix or the namespace URI will be removed.
  858. <em>prefix</em> is a namespace prefix. <em>uri</em> is a namespace uri. Tags and
  859. attributes in this namespace will be serialized with the given prefix, if at
  860. all possible.</p>
  861. <div class="versionadded">
  862. <p><span class="versionmodified added">New in version 3.2.</span></p>
  863. </div>
  864. </dd></dl>
  865. <dl class="py function">
  866. <dt class="sig sig-object py" id="xml.etree.ElementTree.SubElement">
  867. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">SubElement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parent</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tag</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrib</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">extra</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.SubElement" title="Link to this definition">¶</a></dt>
  868. <dd><p>Subelement factory. This function creates an element instance, and appends
  869. it to an existing element.</p>
  870. <p>The element name, attribute names, and attribute values can be either
  871. bytestrings or Unicode strings. <em>parent</em> is the parent element. <em>tag</em> is
  872. the subelement name. <em>attrib</em> is an optional dictionary, containing element
  873. attributes. <em>extra</em> contains additional attributes, given as keyword
  874. arguments. Returns an element instance.</p>
  875. </dd></dl>
  876. <dl class="py function">
  877. <dt class="sig sig-object py" id="xml.etree.ElementTree.tostring">
  878. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">tostring</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'us-ascii'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'xml'</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">xml_declaration</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default_namespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">short_empty_elements</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.tostring" title="Link to this definition">¶</a></dt>
  879. <dd><p>Generates a string representation of an XML element, including all
  880. 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 brackets" href="#id9" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> is
  881. the output encoding (default is US-ASCII). Use <code class="docutils literal notranslate"><span class="pre">encoding=&quot;unicode&quot;</span></code> to
  882. generate a Unicode string (otherwise, a bytestring is generated). <em>method</em>
  883. 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>).
  884. <em>xml_declaration</em>, <em>default_namespace</em> and <em>short_empty_elements</em> has the same
  885. meaning as in <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ElementTree.write()</span></code></a>. Returns an (optionally) encoded string
  886. containing the XML data.</p>
  887. <div class="versionchanged">
  888. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>short_empty_elements</em> parameter.</p>
  889. </div>
  890. <div class="versionchanged">
  891. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>xml_declaration</em> and <em>default_namespace</em> parameters.</p>
  892. </div>
  893. <div class="versionchanged">
  894. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <a class="reference internal" href="#xml.etree.ElementTree.tostring" title="xml.etree.ElementTree.tostring"><code class="xref py py-func docutils literal notranslate"><span class="pre">tostring()</span></code></a> function now preserves the attribute order
  895. specified by the user.</p>
  896. </div>
  897. </dd></dl>
  898. <dl class="py function">
  899. <dt class="sig sig-object py" id="xml.etree.ElementTree.tostringlist">
  900. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">tostringlist</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'us-ascii'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'xml'</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">xml_declaration</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default_namespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">short_empty_elements</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.tostringlist" title="Link to this definition">¶</a></dt>
  901. <dd><p>Generates a string representation of an XML element, including all
  902. 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 brackets" href="#id9" id="id2" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> is
  903. the output encoding (default is US-ASCII). Use <code class="docutils literal notranslate"><span class="pre">encoding=&quot;unicode&quot;</span></code> to
  904. generate a Unicode string (otherwise, a bytestring is generated). <em>method</em>
  905. 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>).
  906. <em>xml_declaration</em>, <em>default_namespace</em> and <em>short_empty_elements</em> has the same
  907. meaning as in <a class="reference internal" href="#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ElementTree.write()</span></code></a>. Returns a list of (optionally) encoded
  908. strings containing the XML data. It does not guarantee any specific sequence,
  909. except that <code class="docutils literal notranslate"><span class="pre">b&quot;&quot;.join(tostringlist(element))</span> <span class="pre">==</span> <span class="pre">tostring(element)</span></code>.</p>
  910. <div class="versionadded">
  911. <p><span class="versionmodified added">New in version 3.2.</span></p>
  912. </div>
  913. <div class="versionchanged">
  914. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>short_empty_elements</em> parameter.</p>
  915. </div>
  916. <div class="versionchanged">
  917. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>xml_declaration</em> and <em>default_namespace</em> parameters.</p>
  918. </div>
  919. <div class="versionchanged">
  920. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <a class="reference internal" href="#xml.etree.ElementTree.tostringlist" title="xml.etree.ElementTree.tostringlist"><code class="xref py py-func docutils literal notranslate"><span class="pre">tostringlist()</span></code></a> function now preserves the attribute order
  921. specified by the user.</p>
  922. </div>
  923. </dd></dl>
  924. <dl class="py function">
  925. <dt class="sig sig-object py" id="xml.etree.ElementTree.XML">
  926. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">XML</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XML" title="Link to this definition">¶</a></dt>
  927. <dd><p>Parses an XML section from a string constant. This function can be used to
  928. embed “XML literals” in Python code. <em>text</em> is a string containing XML
  929. data. <em>parser</em> is an optional parser instance. If not given, the standard
  930. <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>
  931. </dd></dl>
  932. <dl class="py function">
  933. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLID">
  934. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">XMLID</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLID" title="Link to this definition">¶</a></dt>
  935. <dd><p>Parses an XML section from a string constant, and also returns a dictionary
  936. which maps from element id:s to elements. <em>text</em> is a string containing XML
  937. data. <em>parser</em> is an optional parser instance. If not given, the standard
  938. <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
  939. <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>
  940. </dd></dl>
  941. </section>
  942. </section>
  943. <section id="xinclude-support">
  944. <span id="elementtree-xinclude"></span><h2>XInclude support<a class="headerlink" href="#xinclude-support" title="Link to this heading">¶</a></h2>
  945. <p>This module provides limited support for
  946. <a class="reference external" href="https://www.w3.org/TR/xinclude/">XInclude directives</a>, via the <a class="reference internal" href="#module-xml.etree.ElementInclude" title="xml.etree.ElementInclude"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementInclude</span></code></a> helper module. This module can be used to insert subtrees and text strings into element trees, based on information in the tree.</p>
  947. <section id="id3">
  948. <h3>Example<a class="headerlink" href="#id3" title="Link to this heading">¶</a></h3>
  949. <p>Here’s an example that demonstrates use of the XInclude module. To include an XML document in the current document, use the <code class="docutils literal notranslate"><span class="pre">{http://www.w3.org/2001/XInclude}include</span></code> element and set the <strong>parse</strong> attribute to <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>, and use the <strong>href</strong> attribute to specify the document to include.</p>
  950. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
  951. <span class="nt">&lt;document</span><span class="w"> </span><span class="na">xmlns:xi=</span><span class="s">&quot;http://www.w3.org/2001/XInclude&quot;</span><span class="nt">&gt;</span>
  952. <span class="w"> </span><span class="nt">&lt;xi:include</span><span class="w"> </span><span class="na">href=</span><span class="s">&quot;source.xml&quot;</span><span class="w"> </span><span class="na">parse=</span><span class="s">&quot;xml&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
  953. <span class="nt">&lt;/document&gt;</span>
  954. </pre></div>
  955. </div>
  956. <p>By default, the <strong>href</strong> attribute is treated as a file name. You can use custom loaders to override this behaviour. Also note that the standard helper does not support XPointer syntax.</p>
  957. <p>To process this file, load it as usual, and pass the root element to 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:</p>
  958. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml.etree</span> <span class="kn">import</span> <span class="n">ElementTree</span><span class="p">,</span> <span class="n">ElementInclude</span>
  959. <span class="n">tree</span> <span class="o">=</span> <span class="n">ElementTree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;document.xml&quot;</span><span class="p">)</span>
  960. <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>
  961. <span class="n">ElementInclude</span><span class="o">.</span><span class="n">include</span><span class="p">(</span><span class="n">root</span><span class="p">)</span>
  962. </pre></div>
  963. </div>
  964. <p>The ElementInclude module replaces the <code class="docutils literal notranslate"><span class="pre">{http://www.w3.org/2001/XInclude}include</span></code> element with the root element from the <strong>source.xml</strong> document. The result might look something like this:</p>
  965. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="nt">&lt;document</span><span class="w"> </span><span class="na">xmlns:xi=</span><span class="s">&quot;http://www.w3.org/2001/XInclude&quot;</span><span class="nt">&gt;</span>
  966. <span class="w"> </span><span class="nt">&lt;para&gt;</span>This<span class="w"> </span>is<span class="w"> </span>a<span class="w"> </span>paragraph.<span class="nt">&lt;/para&gt;</span>
  967. <span class="nt">&lt;/document&gt;</span>
  968. </pre></div>
  969. </div>
  970. <p>If the <strong>parse</strong> attribute is omitted, it defaults to “xml”. The href attribute is required.</p>
  971. <p>To include a text document, use the <code class="docutils literal notranslate"><span class="pre">{http://www.w3.org/2001/XInclude}include</span></code> element, and set the <strong>parse</strong> attribute to “text”:</p>
  972. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot;?&gt;</span>
  973. <span class="nt">&lt;document</span><span class="w"> </span><span class="na">xmlns:xi=</span><span class="s">&quot;http://www.w3.org/2001/XInclude&quot;</span><span class="nt">&gt;</span>
  974. <span class="w"> </span>Copyright<span class="w"> </span>(c)<span class="w"> </span><span class="nt">&lt;xi:include</span><span class="w"> </span><span class="na">href=</span><span class="s">&quot;year.txt&quot;</span><span class="w"> </span><span class="na">parse=</span><span class="s">&quot;text&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>.
  975. <span class="nt">&lt;/document&gt;</span>
  976. </pre></div>
  977. </div>
  978. <p>The result might look something like:</p>
  979. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="nt">&lt;document</span><span class="w"> </span><span class="na">xmlns:xi=</span><span class="s">&quot;http://www.w3.org/2001/XInclude&quot;</span><span class="nt">&gt;</span>
  980. <span class="w"> </span>Copyright<span class="w"> </span>(c)<span class="w"> </span>2003.
  981. <span class="nt">&lt;/document&gt;</span>
  982. </pre></div>
  983. </div>
  984. </section>
  985. </section>
  986. <section id="id4">
  987. <h2>Reference<a class="headerlink" href="#id4" title="Link to this heading">¶</a></h2>
  988. <section id="elementinclude-functions">
  989. <span id="id5"></span><h3>Functions<a class="headerlink" href="#elementinclude-functions" title="Link to this heading">¶</a></h3>
  990. <dl class="py function" id="module-xml.etree.ElementInclude">
  991. <dt class="sig sig-object py" id="xml.etree.ElementInclude.default_loader">
  992. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementInclude.</span></span><span class="sig-name descname"><span class="pre">default_loader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">href</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementInclude.default_loader" title="Link to this definition">¶</a></dt>
  993. <dd><p>Default loader. This default loader reads an included resource from disk. <em>href</em> is a URL.
  994. <em>parse</em> is for parse mode either “xml” or “text”. <em>encoding</em>
  995. is an optional text encoding. If not given, encoding is <code class="docutils literal notranslate"><span class="pre">utf-8</span></code>. Returns the
  996. expanded resource. If the parse mode is <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>, this is an ElementTree
  997. instance. If the parse mode is “text”, this is a Unicode string. If the
  998. loader fails, it can return None or raise an exception.</p>
  999. </dd></dl>
  1000. <dl class="py function">
  1001. <dt class="sig sig-object py" id="xml.etree.ElementInclude.include">
  1002. <span class="sig-prename descclassname"><span class="pre">xml.etree.ElementInclude.</span></span><span class="sig-name descname"><span class="pre">include</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">elem</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loader</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">base_url</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">6</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementInclude.include" title="Link to this definition">¶</a></dt>
  1003. <dd><p>This function expands XInclude directives. <em>elem</em> is the root element. <em>loader</em> is
  1004. an optional resource loader. If omitted, it defaults to <a class="reference internal" href="#xml.etree.ElementInclude.default_loader" title="xml.etree.ElementInclude.default_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">default_loader()</span></code></a>.
  1005. If given, it should be a callable that implements the same interface as
  1006. <a class="reference internal" href="#xml.etree.ElementInclude.default_loader" title="xml.etree.ElementInclude.default_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">default_loader()</span></code></a>. <em>base_url</em> is base URL of the original file, to resolve
  1007. relative include file references. <em>max_depth</em> is the maximum number of recursive
  1008. inclusions. Limited to reduce the risk of malicious content explosion. Pass a
  1009. negative value to disable the limitation.</p>
  1010. <p>Returns the expanded resource. If the parse mode is
  1011. <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>, this is an ElementTree instance. If the parse mode is “text”,
  1012. this is a Unicode string. If the loader fails, it can return None or
  1013. raise an exception.</p>
  1014. <div class="versionchanged">
  1015. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added the <em>base_url</em> and <em>max_depth</em> parameters.</p>
  1016. </div>
  1017. </dd></dl>
  1018. </section>
  1019. <section id="element-objects">
  1020. <span id="elementtree-element-objects"></span><h3>Element Objects<a class="headerlink" href="#element-objects" title="Link to this heading">¶</a></h3>
  1021. <dl class="py class">
  1022. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element">
  1023. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">Element</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrib</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">extra</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element" title="Link to this definition">¶</a></dt>
  1024. <dd><p>Element class. This class defines the Element interface, and provides a
  1025. reference implementation of this interface.</p>
  1026. <p>The element name, attribute names, and attribute values can be either
  1027. bytestrings or Unicode strings. <em>tag</em> is the element name. <em>attrib</em> is
  1028. an optional dictionary, containing element attributes. <em>extra</em> contains
  1029. additional attributes, given as keyword arguments.</p>
  1030. <dl class="py attribute">
  1031. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.tag">
  1032. <span class="sig-name descname"><span class="pre">tag</span></span><a class="headerlink" href="#xml.etree.ElementTree.Element.tag" title="Link to this definition">¶</a></dt>
  1033. <dd><p>A string identifying what kind of data this element represents (the
  1034. element type, in other words).</p>
  1035. </dd></dl>
  1036. <dl class="py attribute">
  1037. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.text">
  1038. <span class="sig-name descname"><span class="pre">text</span></span><a class="headerlink" href="#xml.etree.ElementTree.Element.text" title="Link to this definition">¶</a></dt>
  1039. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.tail">
  1040. <span class="sig-name descname"><span class="pre">tail</span></span><a class="headerlink" href="#xml.etree.ElementTree.Element.tail" title="Link to this definition">¶</a></dt>
  1041. <dd><p>These attributes can be used to hold additional data associated with
  1042. the element. Their values are usually strings but may be any
  1043. application-specific object. If the element is created from
  1044. an XML file, the <em>text</em> attribute holds either the text between
  1045. 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
  1046. the <em>tail</em> attribute holds either the text between the element’s
  1047. end tag and the next tag, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. For the XML data</p>
  1048. <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>
  1049. </pre></div>
  1050. </div>
  1051. <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,
  1052. 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>,
  1053. 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>,
  1054. 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>
  1055. <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
  1056. example <code class="docutils literal notranslate"><span class="pre">&quot;&quot;.join(element.itertext())</span></code>.</p>
  1057. <p>Applications may store arbitrary objects in these attributes.</p>
  1058. </dd></dl>
  1059. <dl class="py attribute">
  1060. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.attrib">
  1061. <span class="sig-name descname"><span class="pre">attrib</span></span><a class="headerlink" href="#xml.etree.ElementTree.Element.attrib" title="Link to this definition">¶</a></dt>
  1062. <dd><p>A dictionary containing the element’s attributes. Note that while the
  1063. <em>attrib</em> value is always a real mutable Python dictionary, an ElementTree
  1064. implementation may choose to use another internal representation, and
  1065. create the dictionary only if someone asks for it. To take advantage of
  1066. such implementations, use the dictionary methods below whenever possible.</p>
  1067. </dd></dl>
  1068. <p>The following dictionary-like methods work on the element attributes.</p>
  1069. <dl class="py method">
  1070. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.clear">
  1071. <span class="sig-name descname"><span class="pre">clear</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.clear" title="Link to this definition">¶</a></dt>
  1072. <dd><p>Resets an element. This function removes all subelements, clears all
  1073. attributes, and sets the text and tail attributes to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1074. </dd></dl>
  1075. <dl class="py method">
  1076. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.get">
  1077. <span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.get" title="Link to this definition">¶</a></dt>
  1078. <dd><p>Gets the element attribute named <em>key</em>.</p>
  1079. <p>Returns the attribute value, or <em>default</em> if the attribute was not found.</p>
  1080. </dd></dl>
  1081. <dl class="py method">
  1082. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.items">
  1083. <span class="sig-name descname"><span class="pre">items</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.items" title="Link to this definition">¶</a></dt>
  1084. <dd><p>Returns the element attributes as a sequence of (name, value) pairs. The
  1085. attributes are returned in an arbitrary order.</p>
  1086. </dd></dl>
  1087. <dl class="py method">
  1088. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.keys">
  1089. <span class="sig-name descname"><span class="pre">keys</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.keys" title="Link to this definition">¶</a></dt>
  1090. <dd><p>Returns the elements attribute names as a list. The names are returned
  1091. in an arbitrary order.</p>
  1092. </dd></dl>
  1093. <dl class="py method">
  1094. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.set">
  1095. <span class="sig-name descname"><span class="pre">set</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.set" title="Link to this definition">¶</a></dt>
  1096. <dd><p>Set the attribute <em>key</em> on the element to <em>value</em>.</p>
  1097. </dd></dl>
  1098. <p>The following methods work on the element’s children (subelements).</p>
  1099. <dl class="py method">
  1100. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.append">
  1101. <span class="sig-name descname"><span class="pre">append</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">subelement</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.append" title="Link to this definition">¶</a></dt>
  1102. <dd><p>Adds the element <em>subelement</em> to the end of this element’s internal list
  1103. of subelements. Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if <em>subelement</em> is not an
  1104. <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>.</p>
  1105. </dd></dl>
  1106. <dl class="py method">
  1107. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.extend">
  1108. <span class="sig-name descname"><span class="pre">extend</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">subelements</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.extend" title="Link to this definition">¶</a></dt>
  1109. <dd><p>Appends <em>subelements</em> from a sequence object with zero or more elements.
  1110. Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if a subelement is not an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a>.</p>
  1111. <div class="versionadded">
  1112. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1113. </div>
  1114. </dd></dl>
  1115. <dl class="py method">
  1116. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.find">
  1117. <span class="sig-name descname"><span class="pre">find</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.find" title="Link to this definition">¶</a></dt>
  1118. <dd><p>Finds the first subelement matching <em>match</em>. <em>match</em> may be a tag name
  1119. or a <a class="reference internal" href="#elementtree-xpath"><span class="std std-ref">path</span></a>. Returns an element instance
  1120. or <code class="docutils literal notranslate"><span class="pre">None</span></code>. <em>namespaces</em> is an optional mapping from namespace prefix
  1121. to full name. Pass <code class="docutils literal notranslate"><span class="pre">''</span></code> as prefix to move all unprefixed tag names
  1122. in the expression into the given namespace.</p>
  1123. </dd></dl>
  1124. <dl class="py method">
  1125. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.findall">
  1126. <span class="sig-name descname"><span class="pre">findall</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.findall" title="Link to this definition">¶</a></dt>
  1127. <dd><p>Finds all matching subelements, by tag name or
  1128. <a class="reference internal" href="#elementtree-xpath"><span class="std std-ref">path</span></a>. Returns a list containing all matching
  1129. elements in document order. <em>namespaces</em> is an optional mapping from
  1130. namespace prefix to full name. Pass <code class="docutils literal notranslate"><span class="pre">''</span></code> as prefix to move all
  1131. unprefixed tag names in the expression into the given namespace.</p>
  1132. </dd></dl>
  1133. <dl class="py method">
  1134. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.findtext">
  1135. <span class="sig-name descname"><span class="pre">findtext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.findtext" title="Link to this definition">¶</a></dt>
  1136. <dd><p>Finds text for the first subelement matching <em>match</em>. <em>match</em> may be
  1137. a tag name or a <a class="reference internal" href="#elementtree-xpath"><span class="std std-ref">path</span></a>. Returns the text content
  1138. of the first matching element, or <em>default</em> if no element was found.
  1139. Note that if the matching element has no text content an empty string
  1140. is returned. <em>namespaces</em> is an optional mapping from namespace prefix
  1141. to full name. Pass <code class="docutils literal notranslate"><span class="pre">''</span></code> as prefix to move all unprefixed tag names
  1142. in the expression into the given namespace.</p>
  1143. </dd></dl>
  1144. <dl class="py method">
  1145. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.insert">
  1146. <span class="sig-name descname"><span class="pre">insert</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">index</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">subelement</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.insert" title="Link to this definition">¶</a></dt>
  1147. <dd><p>Inserts <em>subelement</em> at the given position in this element. Raises
  1148. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if <em>subelement</em> is not an <a class="reference internal" href="#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a>.</p>
  1149. </dd></dl>
  1150. <dl class="py method">
  1151. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.iter">
  1152. <span class="sig-name descname"><span class="pre">iter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.iter" title="Link to this definition">¶</a></dt>
  1153. <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.
  1154. The iterator iterates over this element and all elements below it, in
  1155. 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
  1156. elements whose tag equals <em>tag</em> are returned from the iterator. If the
  1157. tree structure is modified during iteration, the result is undefined.</p>
  1158. <div class="versionadded">
  1159. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1160. </div>
  1161. </dd></dl>
  1162. <dl class="py method">
  1163. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.iterfind">
  1164. <span class="sig-name descname"><span class="pre">iterfind</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.iterfind" title="Link to this definition">¶</a></dt>
  1165. <dd><p>Finds all matching subelements, by tag name or
  1166. <a class="reference internal" href="#elementtree-xpath"><span class="std std-ref">path</span></a>. Returns an iterable yielding all
  1167. matching elements in document order. <em>namespaces</em> is an optional mapping
  1168. from namespace prefix to full name.</p>
  1169. <div class="versionadded">
  1170. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1171. </div>
  1172. </dd></dl>
  1173. <dl class="py method">
  1174. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.itertext">
  1175. <span class="sig-name descname"><span class="pre">itertext</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.itertext" title="Link to this definition">¶</a></dt>
  1176. <dd><p>Creates a text iterator. The iterator loops over this element and all
  1177. subelements, in document order, and returns all inner text.</p>
  1178. <div class="versionadded">
  1179. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1180. </div>
  1181. </dd></dl>
  1182. <dl class="py method">
  1183. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.makeelement">
  1184. <span class="sig-name descname"><span class="pre">makeelement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrib</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.makeelement" title="Link to this definition">¶</a></dt>
  1185. <dd><p>Creates a new element object of the same type as this element. Do not
  1186. 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>
  1187. </dd></dl>
  1188. <dl class="py method">
  1189. <dt class="sig sig-object py" id="xml.etree.ElementTree.Element.remove">
  1190. <span class="sig-name descname"><span class="pre">remove</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">subelement</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.Element.remove" title="Link to this definition">¶</a></dt>
  1191. <dd><p>Removes <em>subelement</em> from the element. Unlike the find* methods this
  1192. method compares elements based on the instance identity, not on tag value
  1193. or contents.</p>
  1194. </dd></dl>
  1195. <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
  1196. 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>,
  1197. <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>,
  1198. <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>
  1199. <p>Caution: Elements with no subelements will test as <code class="docutils literal notranslate"><span class="pre">False</span></code>. Testing the
  1200. truth value of an Element is deprecated and will raise an exception in
  1201. Python 3.14. 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>
  1202. <div class="highlight-python3 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>
  1203. <span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span> <span class="c1"># careful!</span>
  1204. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;element not found, or element has no subelements&quot;</span><span class="p">)</span>
  1205. <span class="k">if</span> <span class="n">element</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
  1206. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;element not found&quot;</span><span class="p">)</span>
  1207. </pre></div>
  1208. </div>
  1209. <div class="versionchanged">
  1210. <p><span class="versionmodified changed">Changed in version 3.12: </span>Testing the truth value of an Element emits <a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>.</p>
  1211. </div>
  1212. <p>Prior to Python 3.8, the serialisation order of the XML attributes of
  1213. elements was artificially made predictable by sorting the attributes by
  1214. their name. Based on the now guaranteed ordering of dicts, this arbitrary
  1215. reordering was removed in Python 3.8 to preserve the order in which
  1216. attributes were originally parsed or created by user code.</p>
  1217. <p>In general, user code should try not to depend on a specific ordering of
  1218. attributes, given that the <a class="reference external" href="https://www.w3.org/TR/xml-infoset/">XML Information Set</a> explicitly excludes the attribute
  1219. order from conveying information. Code should be prepared to deal with
  1220. any ordering on input. In cases where deterministic XML output is required,
  1221. e.g. for cryptographic signing or test data sets, canonical serialisation
  1222. is available with the <a class="reference internal" href="#xml.etree.ElementTree.canonicalize" title="xml.etree.ElementTree.canonicalize"><code class="xref py py-func docutils literal notranslate"><span class="pre">canonicalize()</span></code></a> function.</p>
  1223. <p>In cases where canonical output is not applicable but a specific attribute
  1224. order is still desirable on output, code should aim for creating the
  1225. attributes directly in the desired order, to avoid perceptual mismatches
  1226. for readers of the code. In cases where this is difficult to achieve, a
  1227. recipe like the following can be applied prior to serialisation to enforce
  1228. an order independently from the Element creation:</p>
  1229. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">reorder_attributes</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
  1230. <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">iter</span><span class="p">():</span>
  1231. <span class="n">attrib</span> <span class="o">=</span> <span class="n">el</span><span class="o">.</span><span class="n">attrib</span>
  1232. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">attrib</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
  1233. <span class="c1"># adjust attribute order, e.g. by sorting</span>
  1234. <span class="n">attribs</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">attrib</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
  1235. <span class="n">attrib</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
  1236. <span class="n">attrib</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attribs</span><span class="p">)</span>
  1237. </pre></div>
  1238. </div>
  1239. </dd></dl>
  1240. </section>
  1241. <section id="elementtree-objects">
  1242. <span id="elementtree-elementtree-objects"></span><h3>ElementTree Objects<a class="headerlink" href="#elementtree-objects" title="Link to this heading">¶</a></h3>
  1243. <dl class="py class">
  1244. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree">
  1245. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">ElementTree</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree" title="Link to this definition">¶</a></dt>
  1246. <dd><p>ElementTree wrapper class. This class represents an entire element
  1247. hierarchy, and adds some extra support for serialization to and from
  1248. standard XML.</p>
  1249. <p><em>element</em> is the root element. The tree is initialized with the contents
  1250. of the XML <em>file</em> if given.</p>
  1251. <dl class="py method">
  1252. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree._setroot">
  1253. <span class="sig-name descname"><span class="pre">_setroot</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree._setroot" title="Link to this definition">¶</a></dt>
  1254. <dd><p>Replaces the root element for this tree. This discards the current
  1255. contents of the tree, and replaces it with the given element. Use with
  1256. care. <em>element</em> is an element instance.</p>
  1257. </dd></dl>
  1258. <dl class="py method">
  1259. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.find">
  1260. <span class="sig-name descname"><span class="pre">find</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.find" title="Link to this definition">¶</a></dt>
  1261. <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>
  1262. </dd></dl>
  1263. <dl class="py method">
  1264. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.findall">
  1265. <span class="sig-name descname"><span class="pre">findall</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findall" title="Link to this definition">¶</a></dt>
  1266. <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>
  1267. </dd></dl>
  1268. <dl class="py method">
  1269. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.findtext">
  1270. <span class="sig-name descname"><span class="pre">findtext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.findtext" title="Link to this definition">¶</a></dt>
  1271. <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>
  1272. </dd></dl>
  1273. <dl class="py method">
  1274. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.getroot">
  1275. <span class="sig-name descname"><span class="pre">getroot</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.getroot" title="Link to this definition">¶</a></dt>
  1276. <dd><p>Returns the root element for this tree.</p>
  1277. </dd></dl>
  1278. <dl class="py method">
  1279. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.iter">
  1280. <span class="sig-name descname"><span class="pre">iter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iter" title="Link to this definition">¶</a></dt>
  1281. <dd><p>Creates and returns a tree iterator for the root element. The iterator
  1282. loops over all elements in this tree, in section order. <em>tag</em> is the tag
  1283. to look for (default is to return all elements).</p>
  1284. </dd></dl>
  1285. <dl class="py method">
  1286. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.iterfind">
  1287. <span class="sig-name descname"><span class="pre">iterfind</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">match</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">namespaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.iterfind" title="Link to this definition">¶</a></dt>
  1288. <dd><p>Same as <a class="reference internal" href="#xml.etree.ElementTree.Element.iterfind" title="xml.etree.ElementTree.Element.iterfind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.iterfind()</span></code></a>, starting at the root of the tree.</p>
  1289. <div class="versionadded">
  1290. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1291. </div>
  1292. </dd></dl>
  1293. <dl class="py method">
  1294. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.parse">
  1295. <span class="sig-name descname"><span class="pre">parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.parse" title="Link to this definition">¶</a></dt>
  1296. <dd><p>Loads an external XML section into this element tree. <em>source</em> is a file
  1297. name or <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>. <em>parser</em> is an optional parser instance.
  1298. 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 the
  1299. section root element.</p>
  1300. </dd></dl>
  1301. <dl class="py method">
  1302. <dt class="sig sig-object py" id="xml.etree.ElementTree.ElementTree.write">
  1303. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'us-ascii'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">xml_declaration</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default_namespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'xml'</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">short_empty_elements</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.ElementTree.write" title="Link to this definition">¶</a></dt>
  1304. <dd><p>Writes the element tree to a file, as XML. <em>file</em> is a file name, or a
  1305. <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> opened for writing. <em>encoding</em> <a class="footnote-reference brackets" href="#id9" id="id6" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> is the output
  1306. encoding (default is US-ASCII).
  1307. <em>xml_declaration</em> controls if an XML declaration should be added to the
  1308. 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>
  1309. for only if not US-ASCII or UTF-8 or Unicode (default is <code class="docutils literal notranslate"><span class="pre">None</span></code>).
  1310. <em>default_namespace</em> sets the default XML namespace (for “xmlns”).
  1311. <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
  1312. <code class="docutils literal notranslate"><span class="pre">&quot;xml&quot;</span></code>).
  1313. The keyword-only <em>short_empty_elements</em> parameter controls the formatting
  1314. of elements that contain no content. If <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), they are
  1315. emitted as a single self-closed tag, otherwise they are emitted as a pair
  1316. of start/end tags.</p>
  1317. <p>The output is either a string (<a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>) or binary (<a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>).
  1318. This is controlled by the <em>encoding</em> argument. If <em>encoding</em> is
  1319. <code class="docutils literal notranslate"><span class="pre">&quot;unicode&quot;</span></code>, the output is a string; otherwise, it’s binary. Note that
  1320. this may conflict with the type of <em>file</em> if it’s an open
  1321. <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>; make sure you do not try to write a string to a
  1322. binary stream and vice versa.</p>
  1323. <div class="versionchanged">
  1324. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>short_empty_elements</em> parameter.</p>
  1325. </div>
  1326. <div class="versionchanged">
  1327. <p><span class="versionmodified changed">Changed in version 3.8: </span>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">write()</span></code></a> method now preserves the attribute order specified
  1328. by the user.</p>
  1329. </div>
  1330. </dd></dl>
  1331. </dd></dl>
  1332. <p>This is the XML file that is going to be manipulated:</p>
  1333. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">html</span><span class="o">&gt;</span>
  1334. <span class="o">&lt;</span><span class="n">head</span><span class="o">&gt;</span>
  1335. <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>
  1336. <span class="o">&lt;/</span><span class="n">head</span><span class="o">&gt;</span>
  1337. <span class="o">&lt;</span><span class="n">body</span><span class="o">&gt;</span>
  1338. <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>
  1339. <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>
  1340. <span class="o">&lt;/</span><span class="n">body</span><span class="o">&gt;</span>
  1341. <span class="o">&lt;/</span><span class="n">html</span><span class="o">&gt;</span>
  1342. </pre></div>
  1343. </div>
  1344. <p>Example of changing the attribute “target” of every link in first paragraph:</p>
  1345. <div class="highlight-python3 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="kn">import</span> <span class="n">ElementTree</span>
  1346. <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>
  1347. <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>
  1348. <span class="go">&lt;Element &#39;html&#39; at 0xb77e6fac&gt;</span>
  1349. <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>
  1350. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>
  1351. <span class="go">&lt;Element &#39;p&#39; at 0xb77ec26c&gt;</span>
  1352. <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>
  1353. <span class="gp">&gt;&gt;&gt; </span><span class="n">links</span>
  1354. <span class="go">[&lt;Element &#39;a&#39; at 0xb77ec2ac&gt;, &lt;Element &#39;a&#39; at 0xb77ec1cc&gt;]</span>
  1355. <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>
  1356. <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>
  1357. <span class="gp">...</span>
  1358. <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>
  1359. </pre></div>
  1360. </div>
  1361. </section>
  1362. <section id="qname-objects">
  1363. <span id="elementtree-qname-objects"></span><h3>QName Objects<a class="headerlink" href="#qname-objects" title="Link to this heading">¶</a></h3>
  1364. <dl class="py class">
  1365. <dt class="sig sig-object py" id="xml.etree.ElementTree.QName">
  1366. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">QName</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text_or_uri</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.QName" title="Link to this definition">¶</a></dt>
  1367. <dd><p>QName wrapper. This can be used to wrap a QName attribute value, in order
  1368. to get proper namespace handling on output. <em>text_or_uri</em> is a string
  1369. containing the QName value, in the form {uri}local, or, if the tag argument
  1370. is given, the URI part of a QName. If <em>tag</em> is given, the first argument is
  1371. interpreted as a URI, and this argument is interpreted as a local name.
  1372. <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>
  1373. </dd></dl>
  1374. </section>
  1375. <section id="treebuilder-objects">
  1376. <span id="elementtree-treebuilder-objects"></span><h3>TreeBuilder Objects<a class="headerlink" href="#treebuilder-objects" title="Link to this heading">¶</a></h3>
  1377. <dl class="py class">
  1378. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder">
  1379. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">TreeBuilder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">element_factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">comment_factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pi_factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">insert_comments</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">insert_pis</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder" title="Link to this definition">¶</a></dt>
  1380. <dd><p>Generic element structure builder. This builder converts a sequence of
  1381. start, data, end, comment and pi method calls to a well-formed element
  1382. structure. You can use this class to build an element structure using
  1383. a custom XML parser, or a parser for some other XML-like format.</p>
  1384. <p><em>element_factory</em>, when given, must be a callable accepting two positional
  1385. arguments: a tag and a dict of attributes. It is expected to return a new
  1386. element instance.</p>
  1387. <p>The <em>comment_factory</em> and <em>pi_factory</em> functions, when given, should behave
  1388. like the <a class="reference internal" href="#xml.etree.ElementTree.Comment" title="xml.etree.ElementTree.Comment"><code class="xref py py-func docutils literal notranslate"><span class="pre">Comment()</span></code></a> and <a class="reference internal" href="#xml.etree.ElementTree.ProcessingInstruction" title="xml.etree.ElementTree.ProcessingInstruction"><code class="xref py py-func docutils literal notranslate"><span class="pre">ProcessingInstruction()</span></code></a> functions to
  1389. create comments and processing instructions. When not given, the default
  1390. factories will be used. When <em>insert_comments</em> and/or <em>insert_pis</em> is true,
  1391. comments/pis will be inserted into the tree if they appear within the root
  1392. element (but not outside of it).</p>
  1393. <dl class="py method">
  1394. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.close">
  1395. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.close" title="Link to this definition">¶</a></dt>
  1396. <dd><p>Flushes the builder buffers, and returns the toplevel document
  1397. 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>
  1398. </dd></dl>
  1399. <dl class="py method">
  1400. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.data">
  1401. <span class="sig-name descname"><span class="pre">data</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.data" title="Link to this definition">¶</a></dt>
  1402. <dd><p>Adds text to the current element. <em>data</em> is a string. This should be
  1403. either a bytestring, or a Unicode string.</p>
  1404. </dd></dl>
  1405. <dl class="py method">
  1406. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.end">
  1407. <span class="sig-name descname"><span class="pre">end</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.end" title="Link to this definition">¶</a></dt>
  1408. <dd><p>Closes the current element. <em>tag</em> is the element name. Returns the
  1409. closed element.</p>
  1410. </dd></dl>
  1411. <dl class="py method">
  1412. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.start">
  1413. <span class="sig-name descname"><span class="pre">start</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tag</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attrs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.start" title="Link to this definition">¶</a></dt>
  1414. <dd><p>Opens a new element. <em>tag</em> is the element name. <em>attrs</em> is a dictionary
  1415. containing element attributes. Returns the opened element.</p>
  1416. </dd></dl>
  1417. <dl class="py method">
  1418. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.comment">
  1419. <span class="sig-name descname"><span class="pre">comment</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.comment" title="Link to this definition">¶</a></dt>
  1420. <dd><p>Creates a comment with the given <em>text</em>. If <code class="docutils literal notranslate"><span class="pre">insert_comments</span></code> is true,
  1421. this will also add it to the tree.</p>
  1422. <div class="versionadded">
  1423. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1424. </div>
  1425. </dd></dl>
  1426. <dl class="py method">
  1427. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.pi">
  1428. <span class="sig-name descname"><span class="pre">pi</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">text</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.pi" title="Link to this definition">¶</a></dt>
  1429. <dd><p>Creates a process instruction with the given <em>target</em> name and <em>text</em>.
  1430. If <code class="docutils literal notranslate"><span class="pre">insert_pis</span></code> is true, this will also add it to the tree.</p>
  1431. <div class="versionadded">
  1432. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1433. </div>
  1434. </dd></dl>
  1435. <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
  1436. following methods:</p>
  1437. <dl class="py method">
  1438. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.doctype">
  1439. <span class="sig-name descname"><span class="pre">doctype</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pubid</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">system</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.doctype" title="Link to this definition">¶</a></dt>
  1440. <dd><p>Handles a doctype declaration. <em>name</em> is the doctype name. <em>pubid</em> is
  1441. the public identifier. <em>system</em> is the system identifier. This method
  1442. 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>
  1443. <div class="versionadded">
  1444. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1445. </div>
  1446. </dd></dl>
  1447. <dl class="py method">
  1448. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.start_ns">
  1449. <span class="sig-name descname"><span class="pre">start_ns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uri</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.start_ns" title="Link to this definition">¶</a></dt>
  1450. <dd><p>Is called whenever the parser encounters a new namespace declaration,
  1451. before the <code class="docutils literal notranslate"><span class="pre">start()</span></code> callback for the opening element that defines it.
  1452. <em>prefix</em> is <code class="docutils literal notranslate"><span class="pre">''</span></code> for the default namespace and the declared
  1453. namespace prefix name otherwise. <em>uri</em> is the namespace URI.</p>
  1454. <div class="versionadded">
  1455. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1456. </div>
  1457. </dd></dl>
  1458. <dl class="py method">
  1459. <dt class="sig sig-object py" id="xml.etree.ElementTree.TreeBuilder.end_ns">
  1460. <span class="sig-name descname"><span class="pre">end_ns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.TreeBuilder.end_ns" title="Link to this definition">¶</a></dt>
  1461. <dd><p>Is called after the <code class="docutils literal notranslate"><span class="pre">end()</span></code> callback of an element that declared
  1462. a namespace prefix mapping, with the name of the <em>prefix</em> that went
  1463. out of scope.</p>
  1464. <div class="versionadded">
  1465. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1466. </div>
  1467. </dd></dl>
  1468. </dd></dl>
  1469. <dl class="py class">
  1470. <dt class="sig sig-object py" id="xml.etree.ElementTree.C14NWriterTarget">
  1471. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">C14NWriterTarget</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">write</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">with_comments</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strip_text</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rewrite_prefixes</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">qname_aware_tags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">qname_aware_attrs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exclude_attrs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exclude_tags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.C14NWriterTarget" title="Link to this definition">¶</a></dt>
  1472. <dd><p>A <a class="reference external" href="https://www.w3.org/TR/xml-c14n2/">C14N 2.0</a> writer. Arguments are the
  1473. same as for the <a class="reference internal" href="#xml.etree.ElementTree.canonicalize" title="xml.etree.ElementTree.canonicalize"><code class="xref py py-func docutils literal notranslate"><span class="pre">canonicalize()</span></code></a> function. This class does not build a
  1474. tree but translates the callback events directly into a serialised form
  1475. using the <em>write</em> function.</p>
  1476. <div class="versionadded">
  1477. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1478. </div>
  1479. </dd></dl>
  1480. </section>
  1481. <section id="xmlparser-objects">
  1482. <span id="elementtree-xmlparser-objects"></span><h3>XMLParser Objects<a class="headerlink" href="#xmlparser-objects" title="Link to this heading">¶</a></h3>
  1483. <dl class="py class">
  1484. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLParser">
  1485. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">XMLParser</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser" title="Link to this definition">¶</a></dt>
  1486. <dd><p>This class is the low-level building block of the module. It uses
  1487. <a class="reference internal" href="pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code></a> for efficient, event-based parsing of XML. It can
  1488. be fed XML data incrementally with the <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.feed" title="xml.etree.ElementTree.XMLParser.feed"><code class="xref py py-meth docutils literal notranslate"><span class="pre">feed()</span></code></a> method, and parsing
  1489. events are translated to a push API - by invoking callbacks on the <em>target</em>
  1490. object. If <em>target</em> is omitted, the standard <a class="reference internal" href="#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">TreeBuilder</span></code></a> is used.
  1491. If <em>encoding</em> <a class="footnote-reference brackets" href="#id9" id="id8" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> is given, the value overrides the
  1492. encoding specified in the XML file.</p>
  1493. <div class="versionchanged">
  1494. <p><span class="versionmodified changed">Changed in version 3.8: </span>Parameters are now <a class="reference internal" href="../glossary.html#keyword-only-parameter"><span class="std std-ref">keyword-only</span></a>.
  1495. The <em>html</em> argument no longer supported.</p>
  1496. </div>
  1497. <dl class="py method">
  1498. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLParser.close">
  1499. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.close" title="Link to this definition">¶</a></dt>
  1500. <dd><p>Finishes feeding data to the parser. Returns the result of calling the
  1501. <code class="docutils literal notranslate"><span class="pre">close()</span></code> method of the <em>target</em> passed during construction; by default,
  1502. this is the toplevel document element.</p>
  1503. </dd></dl>
  1504. <dl class="py method">
  1505. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLParser.feed">
  1506. <span class="sig-name descname"><span class="pre">feed</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.feed" title="Link to this definition">¶</a></dt>
  1507. <dd><p>Feeds data to the parser. <em>data</em> is encoded data.</p>
  1508. </dd></dl>
  1509. <dl class="py method">
  1510. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLParser.flush">
  1511. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLParser.flush" title="Link to this definition">¶</a></dt>
  1512. <dd><p>Triggers parsing of any previously fed unparsed data, which can be
  1513. used to ensure more immediate feedback, in particular with Expat &gt;=2.6.0.
  1514. The implementation of <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.flush" title="xml.etree.ElementTree.XMLParser.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> temporarily disables reparse deferral
  1515. with Expat (if currently enabled) and triggers a reparse.
  1516. Disabling reparse deferral has security consequences; please see
  1517. <a class="reference internal" href="pyexpat.html#xml.parsers.expat.xmlparser.SetReparseDeferralEnabled" title="xml.parsers.expat.xmlparser.SetReparseDeferralEnabled"><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.parsers.expat.xmlparser.SetReparseDeferralEnabled()</span></code></a> for details.</p>
  1518. <p>Note that <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.flush" title="xml.etree.ElementTree.XMLParser.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> has been backported to some prior releases of
  1519. CPython as a security fix. Check for availability of <a class="reference internal" href="#xml.etree.ElementTree.XMLParser.flush" title="xml.etree.ElementTree.XMLParser.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a>
  1520. using <a class="reference internal" href="functions.html#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a> if used in code running across a variety of Python
  1521. versions.</p>
  1522. <div class="versionadded">
  1523. <p><span class="versionmodified added">New in version 3.12.3.</span></p>
  1524. </div>
  1525. </dd></dl>
  1526. <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="docutils literal notranslate"><span class="pre">start(tag,</span> <span class="pre">attrs_dict)</span></code> method
  1527. for each opening tag, its <code class="docutils literal notranslate"><span class="pre">end(tag)</span></code> method for each closing tag, and data
  1528. is processed by method <code class="docutils literal notranslate"><span class="pre">data(data)</span></code>. For further supported callback
  1529. methods, see the <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. <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
  1530. <em>target</em>'s method <code class="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
  1531. building a tree structure. This is an example of counting the maximum depth
  1532. of an XML file:</p>
  1533. <div class="highlight-python3 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="kn">import</span> <span class="n">XMLParser</span>
  1534. <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>
  1535. <span class="gp">... </span> <span class="n">maxDepth</span> <span class="o">=</span> <span class="mi">0</span>
  1536. <span class="gp">... </span> <span class="n">depth</span> <span class="o">=</span> <span class="mi">0</span>
  1537. <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>
  1538. <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>
  1539. <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>
  1540. <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>
  1541. <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>
  1542. <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>
  1543. <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>
  1544. <span class="gp">... </span> <span class="k">pass</span> <span class="c1"># We do not need to do anything with data.</span>
  1545. <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>
  1546. <span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxDepth</span>
  1547. <span class="gp">...</span>
  1548. <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>
  1549. <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>
  1550. <span class="gp">&gt;&gt;&gt; </span><span class="n">exampleXml</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
  1551. <span class="gp">... </span><span class="s2">&lt;a&gt;</span>
  1552. <span class="gp">... </span><span class="s2"> &lt;b&gt;</span>
  1553. <span class="gp">... </span><span class="s2"> &lt;/b&gt;</span>
  1554. <span class="gp">... </span><span class="s2"> &lt;b&gt;</span>
  1555. <span class="gp">... </span><span class="s2"> &lt;c&gt;</span>
  1556. <span class="gp">... </span><span class="s2"> &lt;d&gt;</span>
  1557. <span class="gp">... </span><span class="s2"> &lt;/d&gt;</span>
  1558. <span class="gp">... </span><span class="s2"> &lt;/c&gt;</span>
  1559. <span class="gp">... </span><span class="s2"> &lt;/b&gt;</span>
  1560. <span class="gp">... </span><span class="s2">&lt;/a&gt;&quot;&quot;&quot;</span>
  1561. <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>
  1562. <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>
  1563. <span class="go">4</span>
  1564. </pre></div>
  1565. </div>
  1566. </dd></dl>
  1567. </section>
  1568. <section id="xmlpullparser-objects">
  1569. <span id="elementtree-xmlpullparser-objects"></span><h3>XMLPullParser Objects<a class="headerlink" href="#xmlpullparser-objects" title="Link to this heading">¶</a></h3>
  1570. <dl class="py class">
  1571. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLPullParser">
  1572. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">XMLPullParser</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">events</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser" title="Link to this definition">¶</a></dt>
  1573. <dd><p>A pull parser suitable for non-blocking applications. Its input-side API is
  1574. similar to that of <a class="reference internal" href="#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a>, but instead of pushing calls to a
  1575. callback target, <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLPullParser</span></code></a> collects an internal list of parsing
  1576. events and lets the user read from it. <em>events</em> is a sequence of events to
  1577. report back. The supported events are the strings <code class="docutils literal notranslate"><span class="pre">&quot;start&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;end&quot;</span></code>,
  1578. <code class="docutils literal notranslate"><span class="pre">&quot;comment&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;pi&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;start-ns&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;end-ns&quot;</span></code> (the “ns” events
  1579. are used to get detailed namespace information). If <em>events</em> is omitted,
  1580. only <code class="docutils literal notranslate"><span class="pre">&quot;end&quot;</span></code> events are reported.</p>
  1581. <dl class="py method">
  1582. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLPullParser.feed">
  1583. <span class="sig-name descname"><span class="pre">feed</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.feed" title="Link to this definition">¶</a></dt>
  1584. <dd><p>Feed the given bytes data to the parser.</p>
  1585. </dd></dl>
  1586. <dl class="py method">
  1587. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLPullParser.flush">
  1588. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.flush" title="Link to this definition">¶</a></dt>
  1589. <dd><p>Triggers parsing of any previously fed unparsed data, which can be
  1590. used to ensure more immediate feedback, in particular with Expat &gt;=2.6.0.
  1591. The implementation of <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.flush" title="xml.etree.ElementTree.XMLPullParser.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> temporarily disables reparse deferral
  1592. with Expat (if currently enabled) and triggers a reparse.
  1593. Disabling reparse deferral has security consequences; please see
  1594. <a class="reference internal" href="pyexpat.html#xml.parsers.expat.xmlparser.SetReparseDeferralEnabled" title="xml.parsers.expat.xmlparser.SetReparseDeferralEnabled"><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.parsers.expat.xmlparser.SetReparseDeferralEnabled()</span></code></a> for details.</p>
  1595. <p>Note that <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.flush" title="xml.etree.ElementTree.XMLPullParser.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> has been backported to some prior releases of
  1596. CPython as a security fix. Check for availability of <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.flush" title="xml.etree.ElementTree.XMLPullParser.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a>
  1597. using <a class="reference internal" href="functions.html#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a> if used in code running across a variety of Python
  1598. versions.</p>
  1599. <div class="versionadded">
  1600. <p><span class="versionmodified added">New in version 3.12.3.</span></p>
  1601. </div>
  1602. </dd></dl>
  1603. <dl class="py method">
  1604. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLPullParser.close">
  1605. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.close" title="Link to this definition">¶</a></dt>
  1606. <dd><p>Signal the parser that the data stream is terminated. Unlike
  1607. <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>, this method always returns <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.
  1608. Any events not yet retrieved when the parser is closed can still be
  1609. read with <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read_events()</span></code></a>.</p>
  1610. </dd></dl>
  1611. <dl class="py method">
  1612. <dt class="sig sig-object py" id="xml.etree.ElementTree.XMLPullParser.read_events">
  1613. <span class="sig-name descname"><span class="pre">read_events</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="Link to this definition">¶</a></dt>
  1614. <dd><p>Return an iterator over the events which have been encountered in the
  1615. data fed to the
  1616. parser. The iterator yields <code class="docutils literal notranslate"><span class="pre">(event,</span> <span class="pre">elem)</span></code> pairs, where <em>event</em> is a
  1617. string representing the type of event (e.g. <code class="docutils literal notranslate"><span class="pre">&quot;end&quot;</span></code>) and <em>elem</em> is the
  1618. encountered <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, or other context value as follows.</p>
  1619. <ul class="simple">
  1620. <li><p><code class="docutils literal notranslate"><span class="pre">start</span></code>, <code class="docutils literal notranslate"><span class="pre">end</span></code>: the current Element.</p></li>
  1621. <li><p><code class="docutils literal notranslate"><span class="pre">comment</span></code>, <code class="docutils literal notranslate"><span class="pre">pi</span></code>: the current comment / processing instruction</p></li>
  1622. <li><p><code class="docutils literal notranslate"><span class="pre">start-ns</span></code>: a tuple <code class="docutils literal notranslate"><span class="pre">(prefix,</span> <span class="pre">uri)</span></code> naming the declared namespace
  1623. mapping.</p></li>
  1624. <li><p><code class="docutils literal notranslate"><span class="pre">end-ns</span></code>: <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> (this may change in a future version)</p></li>
  1625. </ul>
  1626. <p>Events provided in a previous call to <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read_events()</span></code></a> will not be
  1627. yielded again. Events are consumed from the internal queue only when
  1628. they are retrieved from the iterator, so multiple readers iterating in
  1629. parallel over iterators obtained from <a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser.read_events" title="xml.etree.ElementTree.XMLPullParser.read_events"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read_events()</span></code></a> will have
  1630. unpredictable results.</p>
  1631. </dd></dl>
  1632. <div class="admonition note">
  1633. <p class="admonition-title">Note</p>
  1634. <p><a class="reference internal" href="#xml.etree.ElementTree.XMLPullParser" title="xml.etree.ElementTree.XMLPullParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLPullParser</span></code></a> only guarantees that it has seen the “&gt;”
  1635. character of a starting tag when it emits a “start” event, so the
  1636. attributes are defined, but the contents of the text and tail attributes
  1637. are undefined at that point. The same applies to the element children;
  1638. they may or may not be present.</p>
  1639. <p>If you need a fully populated element, look for “end” events instead.</p>
  1640. </div>
  1641. <div class="versionadded">
  1642. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1643. </div>
  1644. <div class="versionchanged">
  1645. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <code class="docutils literal notranslate"><span class="pre">comment</span></code> and <code class="docutils literal notranslate"><span class="pre">pi</span></code> events were added.</p>
  1646. </div>
  1647. </dd></dl>
  1648. </section>
  1649. <section id="exceptions">
  1650. <h3>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h3>
  1651. <dl class="py class">
  1652. <dt class="sig sig-object py" id="xml.etree.ElementTree.ParseError">
  1653. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.etree.ElementTree.</span></span><span class="sig-name descname"><span class="pre">ParseError</span></span><a class="headerlink" href="#xml.etree.ElementTree.ParseError" title="Link to this definition">¶</a></dt>
  1654. <dd><p>XML parse error, raised by the various parsing methods in this module when
  1655. parsing fails. The string representation of an instance of this exception
  1656. will contain a user-friendly error message. In addition, it will have
  1657. the following attributes available:</p>
  1658. <dl class="py attribute">
  1659. <dt class="sig sig-object py" id="xml.etree.ElementTree.ParseError.code">
  1660. <span class="sig-name descname"><span class="pre">code</span></span><a class="headerlink" href="#xml.etree.ElementTree.ParseError.code" title="Link to this definition">¶</a></dt>
  1661. <dd><p>A numeric error code from the expat parser. See the documentation of
  1662. <a class="reference internal" href="pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.parsers.expat</span></code></a> for the list of error codes and their meanings.</p>
  1663. </dd></dl>
  1664. <dl class="py attribute">
  1665. <dt class="sig sig-object py" id="xml.etree.ElementTree.ParseError.position">
  1666. <span class="sig-name descname"><span class="pre">position</span></span><a class="headerlink" href="#xml.etree.ElementTree.ParseError.position" title="Link to this definition">¶</a></dt>
  1667. <dd><p>A tuple of <em>line</em>, <em>column</em> numbers, specifying where the error occurred.</p>
  1668. </dd></dl>
  1669. </dd></dl>
  1670. <p class="rubric">Footnotes</p>
  1671. <aside class="footnote-list brackets">
  1672. <aside class="footnote brackets" id="id9" role="doc-footnote">
  1673. <span class="label"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></span>
  1674. <span class="backrefs">(<a role="doc-backlink" href="#id1">1</a>,<a role="doc-backlink" href="#id2">2</a>,<a role="doc-backlink" href="#id6">3</a>,<a role="doc-backlink" href="#id8">4</a>)</span>
  1675. <p>The encoding string included in XML output should conform to the
  1676. appropriate standards. For example, “UTF-8” is valid, but “UTF8” is
  1677. 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>
  1678. 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>.</p>
  1679. </aside>
  1680. </aside>
  1681. </section>
  1682. </section>
  1683. </section>
  1684. <div class="clearer"></div>
  1685. </div>
  1686. </div>
  1687. </div>
  1688. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1689. <div class="sphinxsidebarwrapper">
  1690. <div>
  1691. <h3><a href="../contents.html">Table of Contents</a></h3>
  1692. <ul>
  1693. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a><ul>
  1694. <li><a class="reference internal" href="#tutorial">Tutorial</a><ul>
  1695. <li><a class="reference internal" href="#xml-tree-and-elements">XML tree and elements</a></li>
  1696. <li><a class="reference internal" href="#parsing-xml">Parsing XML</a></li>
  1697. <li><a class="reference internal" href="#pull-api-for-non-blocking-parsing">Pull API for non-blocking parsing</a></li>
  1698. <li><a class="reference internal" href="#finding-interesting-elements">Finding interesting elements</a></li>
  1699. <li><a class="reference internal" href="#modifying-an-xml-file">Modifying an XML File</a></li>
  1700. <li><a class="reference internal" href="#building-xml-documents">Building XML documents</a></li>
  1701. <li><a class="reference internal" href="#parsing-xml-with-namespaces">Parsing XML with Namespaces</a></li>
  1702. </ul>
  1703. </li>
  1704. <li><a class="reference internal" href="#xpath-support">XPath support</a><ul>
  1705. <li><a class="reference internal" href="#example">Example</a></li>
  1706. <li><a class="reference internal" href="#supported-xpath-syntax">Supported XPath syntax</a></li>
  1707. </ul>
  1708. </li>
  1709. <li><a class="reference internal" href="#reference">Reference</a><ul>
  1710. <li><a class="reference internal" href="#functions">Functions</a></li>
  1711. </ul>
  1712. </li>
  1713. <li><a class="reference internal" href="#xinclude-support">XInclude support</a><ul>
  1714. <li><a class="reference internal" href="#id3">Example</a></li>
  1715. </ul>
  1716. </li>
  1717. <li><a class="reference internal" href="#id4">Reference</a><ul>
  1718. <li><a class="reference internal" href="#elementinclude-functions">Functions</a></li>
  1719. <li><a class="reference internal" href="#element-objects">Element Objects</a></li>
  1720. <li><a class="reference internal" href="#elementtree-objects">ElementTree Objects</a></li>
  1721. <li><a class="reference internal" href="#qname-objects">QName Objects</a></li>
  1722. <li><a class="reference internal" href="#treebuilder-objects">TreeBuilder Objects</a></li>
  1723. <li><a class="reference internal" href="#xmlparser-objects">XMLParser Objects</a></li>
  1724. <li><a class="reference internal" href="#xmlpullparser-objects">XMLPullParser Objects</a></li>
  1725. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  1726. </ul>
  1727. </li>
  1728. </ul>
  1729. </li>
  1730. </ul>
  1731. </div>
  1732. <div>
  1733. <h4>Previous topic</h4>
  1734. <p class="topless"><a href="xml.html"
  1735. title="previous chapter">XML Processing Modules</a></p>
  1736. </div>
  1737. <div>
  1738. <h4>Next topic</h4>
  1739. <p class="topless"><a href="xml.dom.html"
  1740. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom</span></code> — The Document Object Model API</a></p>
  1741. </div>
  1742. <div role="note" aria-label="source link">
  1743. <h3>This Page</h3>
  1744. <ul class="this-page-menu">
  1745. <li><a href="../bugs.html">Report a Bug</a></li>
  1746. <li>
  1747. <a href="https://github.com/python/cpython/blob/main/Doc/library/xml.etree.elementtree.rst"
  1748. rel="nofollow">Show Source
  1749. </a>
  1750. </li>
  1751. </ul>
  1752. </div>
  1753. </div>
  1754. <div id="sidebarbutton" title="Collapse sidebar">
  1755. <span>«</span>
  1756. </div>
  1757. </div>
  1758. <div class="clearer"></div>
  1759. </div>
  1760. <div class="related" role="navigation" aria-label="related navigation">
  1761. <h3>Navigation</h3>
  1762. <ul>
  1763. <li class="right" style="margin-right: 10px">
  1764. <a href="../genindex.html" title="General Index"
  1765. >index</a></li>
  1766. <li class="right" >
  1767. <a href="../py-modindex.html" title="Python Module Index"
  1768. >modules</a> |</li>
  1769. <li class="right" >
  1770. <a href="xml.dom.html" title="xml.dom — The Document Object Model API"
  1771. >next</a> |</li>
  1772. <li class="right" >
  1773. <a href="xml.html" title="XML Processing Modules"
  1774. >previous</a> |</li>
  1775. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1776. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1777. <li class="switchers">
  1778. <div class="language_switcher_placeholder"></div>
  1779. <div class="version_switcher_placeholder"></div>
  1780. </li>
  1781. <li>
  1782. </li>
  1783. <li id="cpython-language-and-version">
  1784. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1785. </li>
  1786. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1787. <li class="nav-item nav-item-2"><a href="markup.html" >Structured Markup Processing Tools</a> &#187;</li>
  1788. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code> — The ElementTree XML API</a></li>
  1789. <li class="right">
  1790. <div class="inline-search" role="search">
  1791. <form class="inline-search" action="../search.html" method="get">
  1792. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1793. <input type="submit" value="Go" />
  1794. </form>
  1795. </div>
  1796. |
  1797. </li>
  1798. <li class="right">
  1799. <label class="theme-selector-label">
  1800. Theme
  1801. <select class="theme-selector" oninput="activateTheme(this.value)">
  1802. <option value="auto" selected>Auto</option>
  1803. <option value="light">Light</option>
  1804. <option value="dark">Dark</option>
  1805. </select>
  1806. </label> |</li>
  1807. </ul>
  1808. </div>
  1809. <div class="footer">
  1810. &copy;
  1811. <a href="../copyright.html">
  1812. Copyright
  1813. </a>
  1814. 2001-2024, Python Software Foundation.
  1815. <br />
  1816. This page is licensed under the Python Software Foundation License Version 2.
  1817. <br />
  1818. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1819. <br />
  1820. See <a href="/license.html">History and License</a> for more information.<br />
  1821. <br />
  1822. The Python Software Foundation is a non-profit corporation.
  1823. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1824. <br />
  1825. <br />
  1826. Last updated on Apr 09, 2024 (13:47 UTC).
  1827. <a href="/bugs.html">Found a bug</a>?
  1828. <br />
  1829. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1830. </div>
  1831. </body>
  1832. </html>
上海开阖软件有限公司 沪ICP备12045867号-1