gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

476 lignes
33KB

  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.dom.pulldom — Support for building partial DOM trees" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/xml.dom.pulldom.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/xml/dom/pulldom.py The xml.dom.pulldom module provides a “pull parser” which can also be asked to produce DOM-accessible fragments of the document where necessary. The basic concep..." />
  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/dom/pulldom.py The xml.dom.pulldom module provides a “pull parser” which can also be asked to produce DOM-accessible fragments of the document where necessary. The basic concep..." />
  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.dom.pulldom — Support for building partial DOM trees &#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.sax — Support for SAX2 parsers" href="xml.sax.html" />
  33. <link rel="prev" title="xml.dom.minidom — Minimal DOM implementation" href="xml.dom.minidom.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/xml.dom.pulldom.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.dom.pulldom</span></code> — Support for building partial DOM trees</a><ul>
  86. <li><a class="reference internal" href="#domeventstream-objects">DOMEventStream Objects</a></li>
  87. </ul>
  88. </li>
  89. </ul>
  90. </div>
  91. <div>
  92. <h4>Previous topic</h4>
  93. <p class="topless"><a href="xml.dom.minidom.html"
  94. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.minidom</span></code> — Minimal DOM implementation</a></p>
  95. </div>
  96. <div>
  97. <h4>Next topic</h4>
  98. <p class="topless"><a href="xml.sax.html"
  99. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax</span></code> — Support for SAX2 parsers</a></p>
  100. </div>
  101. <div role="note" aria-label="source link">
  102. <h3>This Page</h3>
  103. <ul class="this-page-menu">
  104. <li><a href="../bugs.html">Report a Bug</a></li>
  105. <li>
  106. <a href="https://github.com/python/cpython/blob/main/Doc/library/xml.dom.pulldom.rst"
  107. rel="nofollow">Show Source
  108. </a>
  109. </li>
  110. </ul>
  111. </div>
  112. </nav>
  113. </div>
  114. </div>
  115. <div class="related" role="navigation" aria-label="related navigation">
  116. <h3>Navigation</h3>
  117. <ul>
  118. <li class="right" style="margin-right: 10px">
  119. <a href="../genindex.html" title="General Index"
  120. accesskey="I">index</a></li>
  121. <li class="right" >
  122. <a href="../py-modindex.html" title="Python Module Index"
  123. >modules</a> |</li>
  124. <li class="right" >
  125. <a href="xml.sax.html" title="xml.sax — Support for SAX2 parsers"
  126. accesskey="N">next</a> |</li>
  127. <li class="right" >
  128. <a href="xml.dom.minidom.html" title="xml.dom.minidom — Minimal DOM implementation"
  129. accesskey="P">previous</a> |</li>
  130. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  131. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  132. <li class="switchers">
  133. <div class="language_switcher_placeholder"></div>
  134. <div class="version_switcher_placeholder"></div>
  135. </li>
  136. <li>
  137. </li>
  138. <li id="cpython-language-and-version">
  139. <a href="../index.html">3.12.3 Documentation</a> &#187;
  140. </li>
  141. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  142. <li class="nav-item nav-item-2"><a href="markup.html" accesskey="U">Structured Markup Processing Tools</a> &#187;</li>
  143. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.pulldom</span></code> — Support for building partial DOM trees</a></li>
  144. <li class="right">
  145. <div class="inline-search" role="search">
  146. <form class="inline-search" action="../search.html" method="get">
  147. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  148. <input type="submit" value="Go" />
  149. </form>
  150. </div>
  151. |
  152. </li>
  153. <li class="right">
  154. <label class="theme-selector-label">
  155. Theme
  156. <select class="theme-selector" oninput="activateTheme(this.value)">
  157. <option value="auto" selected>Auto</option>
  158. <option value="light">Light</option>
  159. <option value="dark">Dark</option>
  160. </select>
  161. </label> |</li>
  162. </ul>
  163. </div>
  164. <div class="document">
  165. <div class="documentwrapper">
  166. <div class="bodywrapper">
  167. <div class="body" role="main">
  168. <section id="module-xml.dom.pulldom">
  169. <span id="xml-dom-pulldom-support-for-building-partial-dom-trees"></span><h1><a class="reference internal" href="#module-xml.dom.pulldom" title="xml.dom.pulldom: Support for building partial DOM trees from SAX events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.pulldom</span></code></a> — Support for building partial DOM trees<a class="headerlink" href="#module-xml.dom.pulldom" title="Link to this heading">¶</a></h1>
  170. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/xml/dom/pulldom.py">Lib/xml/dom/pulldom.py</a></p>
  171. <hr class="docutils" />
  172. <p>The <a class="reference internal" href="#module-xml.dom.pulldom" title="xml.dom.pulldom: Support for building partial DOM trees from SAX events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.pulldom</span></code></a> module provides a “pull parser” which can also be
  173. asked to produce DOM-accessible fragments of the document where necessary. The
  174. basic concept involves pulling “events” from a stream of incoming XML and
  175. processing them. In contrast to SAX which also employs an event-driven
  176. processing model together with callbacks, the user of a pull parser is
  177. responsible for explicitly pulling events from the stream, looping over those
  178. events until either processing is finished or an error condition occurs.</p>
  179. <div class="admonition warning">
  180. <p class="admonition-title">Warning</p>
  181. <p>The <a class="reference internal" href="#module-xml.dom.pulldom" title="xml.dom.pulldom: Support for building partial DOM trees from SAX events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.pulldom</span></code></a> module is not secure against
  182. maliciously constructed data. If you need to parse untrusted or
  183. unauthenticated data see <a class="reference internal" href="xml.html#xml-vulnerabilities"><span class="std std-ref">XML vulnerabilities</span></a>.</p>
  184. </div>
  185. <div class="versionchanged">
  186. <p><span class="versionmodified changed">Changed in version 3.7.1: </span>The SAX parser no longer processes general external entities by default to
  187. increase security by default. To enable processing of external entities,
  188. pass a custom parser instance in:</p>
  189. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml.dom.pulldom</span> <span class="kn">import</span> <span class="n">parse</span>
  190. <span class="kn">from</span> <span class="nn">xml.sax</span> <span class="kn">import</span> <span class="n">make_parser</span>
  191. <span class="kn">from</span> <span class="nn">xml.sax.handler</span> <span class="kn">import</span> <span class="n">feature_external_ges</span>
  192. <span class="n">parser</span> <span class="o">=</span> <span class="n">make_parser</span><span class="p">()</span>
  193. <span class="n">parser</span><span class="o">.</span><span class="n">setFeature</span><span class="p">(</span><span class="n">feature_external_ges</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
  194. <span class="n">parse</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">parser</span><span class="o">=</span><span class="n">parser</span><span class="p">)</span>
  195. </pre></div>
  196. </div>
  197. </div>
  198. <p>Example:</p>
  199. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml.dom</span> <span class="kn">import</span> <span class="n">pulldom</span>
  200. <span class="n">doc</span> <span class="o">=</span> <span class="n">pulldom</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;sales_items.xml&#39;</span><span class="p">)</span>
  201. <span class="k">for</span> <span class="n">event</span><span class="p">,</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">doc</span><span class="p">:</span>
  202. <span class="k">if</span> <span class="n">event</span> <span class="o">==</span> <span class="n">pulldom</span><span class="o">.</span><span class="n">START_ELEMENT</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">tagName</span> <span class="o">==</span> <span class="s1">&#39;item&#39;</span><span class="p">:</span>
  203. <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">getAttribute</span><span class="p">(</span><span class="s1">&#39;price&#39;</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mi">50</span><span class="p">:</span>
  204. <span class="n">doc</span><span class="o">.</span><span class="n">expandNode</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
  205. <span class="nb">print</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">toxml</span><span class="p">())</span>
  206. </pre></div>
  207. </div>
  208. <p><code class="docutils literal notranslate"><span class="pre">event</span></code> is a constant and can be one of:</p>
  209. <ul class="simple">
  210. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">START_ELEMENT</span></code></p></li>
  211. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">END_ELEMENT</span></code></p></li>
  212. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">COMMENT</span></code></p></li>
  213. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">START_DOCUMENT</span></code></p></li>
  214. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">END_DOCUMENT</span></code></p></li>
  215. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">CHARACTERS</span></code></p></li>
  216. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">PROCESSING_INSTRUCTION</span></code></p></li>
  217. <li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">IGNORABLE_WHITESPACE</span></code></p></li>
  218. </ul>
  219. <p><code class="docutils literal notranslate"><span class="pre">node</span></code> is an object of type <code class="xref py py-class docutils literal notranslate"><span class="pre">xml.dom.minidom.Document</span></code>,
  220. <code class="xref py py-class docutils literal notranslate"><span class="pre">xml.dom.minidom.Element</span></code> or <code class="xref py py-class docutils literal notranslate"><span class="pre">xml.dom.minidom.Text</span></code>.</p>
  221. <p>Since the document is treated as a “flat” stream of events, the document “tree”
  222. is implicitly traversed and the desired elements are found regardless of their
  223. depth in the tree. In other words, one does not need to consider hierarchical
  224. issues such as recursive searching of the document nodes, although if the
  225. context of elements were important, one would either need to maintain some
  226. context-related state (i.e. remembering where one is in the document at any
  227. given point) or to make use of the <a class="reference internal" href="#xml.dom.pulldom.DOMEventStream.expandNode" title="xml.dom.pulldom.DOMEventStream.expandNode"><code class="xref py py-func docutils literal notranslate"><span class="pre">DOMEventStream.expandNode()</span></code></a> method
  228. and switch to DOM-related processing.</p>
  229. <dl class="py class">
  230. <dt class="sig sig-object py" id="xml.dom.pulldom.PullDom">
  231. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.dom.pulldom.</span></span><span class="sig-name descname"><span class="pre">PullDom</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">documentFactory</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.dom.pulldom.PullDom" title="Link to this definition">¶</a></dt>
  232. <dd><p>Subclass of <a class="reference internal" href="xml.sax.handler.html#xml.sax.handler.ContentHandler" title="xml.sax.handler.ContentHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">xml.sax.handler.ContentHandler</span></code></a>.</p>
  233. </dd></dl>
  234. <dl class="py class">
  235. <dt class="sig sig-object py" id="xml.dom.pulldom.SAX2DOM">
  236. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.dom.pulldom.</span></span><span class="sig-name descname"><span class="pre">SAX2DOM</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">documentFactory</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.dom.pulldom.SAX2DOM" title="Link to this definition">¶</a></dt>
  237. <dd><p>Subclass of <a class="reference internal" href="xml.sax.handler.html#xml.sax.handler.ContentHandler" title="xml.sax.handler.ContentHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">xml.sax.handler.ContentHandler</span></code></a>.</p>
  238. </dd></dl>
  239. <dl class="py function">
  240. <dt class="sig sig-object py" id="xml.dom.pulldom.parse">
  241. <span class="sig-prename descclassname"><span class="pre">xml.dom.pulldom.</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">stream_or_string</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>, <em class="sig-param"><span class="n"><span class="pre">bufsize</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.dom.pulldom.parse" title="Link to this definition">¶</a></dt>
  242. <dd><p>Return a <a class="reference internal" href="#xml.dom.pulldom.DOMEventStream" title="xml.dom.pulldom.DOMEventStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">DOMEventStream</span></code></a> from the given input. <em>stream_or_string</em> may be
  243. either a file name, or a file-like object. <em>parser</em>, if given, must be an
  244. <a class="reference internal" href="xml.sax.reader.html#xml.sax.xmlreader.XMLReader" title="xml.sax.xmlreader.XMLReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLReader</span></code></a> object. This function will change the
  245. document handler of the
  246. parser and activate namespace support; other parser configuration (like
  247. setting an entity resolver) must have been done in advance.</p>
  248. </dd></dl>
  249. <p>If you have XML in a string, you can use the <a class="reference internal" href="#xml.dom.pulldom.parseString" title="xml.dom.pulldom.parseString"><code class="xref py py-func docutils literal notranslate"><span class="pre">parseString()</span></code></a> function instead:</p>
  250. <dl class="py function">
  251. <dt class="sig sig-object py" id="xml.dom.pulldom.parseString">
  252. <span class="sig-prename descclassname"><span class="pre">xml.dom.pulldom.</span></span><span class="sig-name descname"><span class="pre">parseString</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</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.dom.pulldom.parseString" title="Link to this definition">¶</a></dt>
  253. <dd><p>Return a <a class="reference internal" href="#xml.dom.pulldom.DOMEventStream" title="xml.dom.pulldom.DOMEventStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">DOMEventStream</span></code></a> that represents the (Unicode) <em>string</em>.</p>
  254. </dd></dl>
  255. <dl class="py data">
  256. <dt class="sig sig-object py" id="xml.dom.pulldom.default_bufsize">
  257. <span class="sig-prename descclassname"><span class="pre">xml.dom.pulldom.</span></span><span class="sig-name descname"><span class="pre">default_bufsize</span></span><a class="headerlink" href="#xml.dom.pulldom.default_bufsize" title="Link to this definition">¶</a></dt>
  258. <dd><p>Default value for the <em>bufsize</em> parameter to <a class="reference internal" href="#xml.dom.pulldom.parse" title="xml.dom.pulldom.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">parse()</span></code></a>.</p>
  259. <p>The value of this variable can be changed before calling <a class="reference internal" href="#xml.dom.pulldom.parse" title="xml.dom.pulldom.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">parse()</span></code></a> and
  260. the new value will take effect.</p>
  261. </dd></dl>
  262. <section id="domeventstream-objects">
  263. <span id="id1"></span><h2>DOMEventStream Objects<a class="headerlink" href="#domeventstream-objects" title="Link to this heading">¶</a></h2>
  264. <dl class="py class">
  265. <dt class="sig sig-object py" id="xml.dom.pulldom.DOMEventStream">
  266. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">xml.dom.pulldom.</span></span><span class="sig-name descname"><span class="pre">DOMEventStream</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stream</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bufsize</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.dom.pulldom.DOMEventStream" title="Link to this definition">¶</a></dt>
  267. <dd><div class="versionchanged">
  268. <p><span class="versionmodified changed">Changed in version 3.11: </span>Support for <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> method has been removed.</p>
  269. </div>
  270. <dl class="py method">
  271. <dt class="sig sig-object py" id="xml.dom.pulldom.DOMEventStream.getEvent">
  272. <span class="sig-name descname"><span class="pre">getEvent</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.dom.pulldom.DOMEventStream.getEvent" title="Link to this definition">¶</a></dt>
  273. <dd><p>Return a tuple containing <em>event</em> and the current <em>node</em> as
  274. <code class="xref py py-class docutils literal notranslate"><span class="pre">xml.dom.minidom.Document</span></code> if event equals <code class="xref py py-data docutils literal notranslate"><span class="pre">START_DOCUMENT</span></code>,
  275. <code class="xref py py-class docutils literal notranslate"><span class="pre">xml.dom.minidom.Element</span></code> if event equals <code class="xref py py-data docutils literal notranslate"><span class="pre">START_ELEMENT</span></code> or
  276. <code class="xref py py-data docutils literal notranslate"><span class="pre">END_ELEMENT</span></code> or <code class="xref py py-class docutils literal notranslate"><span class="pre">xml.dom.minidom.Text</span></code> if event equals
  277. <code class="xref py py-data docutils literal notranslate"><span class="pre">CHARACTERS</span></code>.
  278. The current node does not contain information about its children, unless
  279. <a class="reference internal" href="#xml.dom.pulldom.DOMEventStream.expandNode" title="xml.dom.pulldom.DOMEventStream.expandNode"><code class="xref py py-func docutils literal notranslate"><span class="pre">expandNode()</span></code></a> is called.</p>
  280. </dd></dl>
  281. <dl class="py method">
  282. <dt class="sig sig-object py" id="xml.dom.pulldom.DOMEventStream.expandNode">
  283. <span class="sig-name descname"><span class="pre">expandNode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">node</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#xml.dom.pulldom.DOMEventStream.expandNode" title="Link to this definition">¶</a></dt>
  284. <dd><p>Expands all children of <em>node</em> into <em>node</em>. Example:</p>
  285. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml.dom</span> <span class="kn">import</span> <span class="n">pulldom</span>
  286. <span class="n">xml</span> <span class="o">=</span> <span class="s1">&#39;&lt;html&gt;&lt;title&gt;Foo&lt;/title&gt; &lt;p&gt;Some text &lt;div&gt;and more&lt;/div&gt;&lt;/p&gt; &lt;/html&gt;&#39;</span>
  287. <span class="n">doc</span> <span class="o">=</span> <span class="n">pulldom</span><span class="o">.</span><span class="n">parseString</span><span class="p">(</span><span class="n">xml</span><span class="p">)</span>
  288. <span class="k">for</span> <span class="n">event</span><span class="p">,</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">doc</span><span class="p">:</span>
  289. <span class="k">if</span> <span class="n">event</span> <span class="o">==</span> <span class="n">pulldom</span><span class="o">.</span><span class="n">START_ELEMENT</span> <span class="ow">and</span> <span class="n">node</span><span class="o">.</span><span class="n">tagName</span> <span class="o">==</span> <span class="s1">&#39;p&#39;</span><span class="p">:</span>
  290. <span class="c1"># Following statement only prints &#39;&lt;p/&gt;&#39;</span>
  291. <span class="nb">print</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">toxml</span><span class="p">())</span>
  292. <span class="n">doc</span><span class="o">.</span><span class="n">expandNode</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
  293. <span class="c1"># Following statement prints node with all its children &#39;&lt;p&gt;Some text &lt;div&gt;and more&lt;/div&gt;&lt;/p&gt;&#39;</span>
  294. <span class="nb">print</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">toxml</span><span class="p">())</span>
  295. </pre></div>
  296. </div>
  297. </dd></dl>
  298. <dl class="py method">
  299. <dt class="sig sig-object py" id="xml.dom.pulldom.DOMEventStream.reset">
  300. <span class="sig-name descname"><span class="pre">reset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#xml.dom.pulldom.DOMEventStream.reset" title="Link to this definition">¶</a></dt>
  301. <dd></dd></dl>
  302. </dd></dl>
  303. </section>
  304. </section>
  305. <div class="clearer"></div>
  306. </div>
  307. </div>
  308. </div>
  309. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  310. <div class="sphinxsidebarwrapper">
  311. <div>
  312. <h3><a href="../contents.html">Table of Contents</a></h3>
  313. <ul>
  314. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.pulldom</span></code> — Support for building partial DOM trees</a><ul>
  315. <li><a class="reference internal" href="#domeventstream-objects">DOMEventStream Objects</a></li>
  316. </ul>
  317. </li>
  318. </ul>
  319. </div>
  320. <div>
  321. <h4>Previous topic</h4>
  322. <p class="topless"><a href="xml.dom.minidom.html"
  323. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.minidom</span></code> — Minimal DOM implementation</a></p>
  324. </div>
  325. <div>
  326. <h4>Next topic</h4>
  327. <p class="topless"><a href="xml.sax.html"
  328. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax</span></code> — Support for SAX2 parsers</a></p>
  329. </div>
  330. <div role="note" aria-label="source link">
  331. <h3>This Page</h3>
  332. <ul class="this-page-menu">
  333. <li><a href="../bugs.html">Report a Bug</a></li>
  334. <li>
  335. <a href="https://github.com/python/cpython/blob/main/Doc/library/xml.dom.pulldom.rst"
  336. rel="nofollow">Show Source
  337. </a>
  338. </li>
  339. </ul>
  340. </div>
  341. </div>
  342. <div id="sidebarbutton" title="Collapse sidebar">
  343. <span>«</span>
  344. </div>
  345. </div>
  346. <div class="clearer"></div>
  347. </div>
  348. <div class="related" role="navigation" aria-label="related navigation">
  349. <h3>Navigation</h3>
  350. <ul>
  351. <li class="right" style="margin-right: 10px">
  352. <a href="../genindex.html" title="General Index"
  353. >index</a></li>
  354. <li class="right" >
  355. <a href="../py-modindex.html" title="Python Module Index"
  356. >modules</a> |</li>
  357. <li class="right" >
  358. <a href="xml.sax.html" title="xml.sax — Support for SAX2 parsers"
  359. >next</a> |</li>
  360. <li class="right" >
  361. <a href="xml.dom.minidom.html" title="xml.dom.minidom — Minimal DOM implementation"
  362. >previous</a> |</li>
  363. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  364. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  365. <li class="switchers">
  366. <div class="language_switcher_placeholder"></div>
  367. <div class="version_switcher_placeholder"></div>
  368. </li>
  369. <li>
  370. </li>
  371. <li id="cpython-language-and-version">
  372. <a href="../index.html">3.12.3 Documentation</a> &#187;
  373. </li>
  374. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  375. <li class="nav-item nav-item-2"><a href="markup.html" >Structured Markup Processing Tools</a> &#187;</li>
  376. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.pulldom</span></code> — Support for building partial DOM trees</a></li>
  377. <li class="right">
  378. <div class="inline-search" role="search">
  379. <form class="inline-search" action="../search.html" method="get">
  380. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  381. <input type="submit" value="Go" />
  382. </form>
  383. </div>
  384. |
  385. </li>
  386. <li class="right">
  387. <label class="theme-selector-label">
  388. Theme
  389. <select class="theme-selector" oninput="activateTheme(this.value)">
  390. <option value="auto" selected>Auto</option>
  391. <option value="light">Light</option>
  392. <option value="dark">Dark</option>
  393. </select>
  394. </label> |</li>
  395. </ul>
  396. </div>
  397. <div class="footer">
  398. &copy;
  399. <a href="../copyright.html">
  400. Copyright
  401. </a>
  402. 2001-2024, Python Software Foundation.
  403. <br />
  404. This page is licensed under the Python Software Foundation License Version 2.
  405. <br />
  406. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  407. <br />
  408. See <a href="/license.html">History and License</a> for more information.<br />
  409. <br />
  410. The Python Software Foundation is a non-profit corporation.
  411. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  412. <br />
  413. <br />
  414. Last updated on Apr 09, 2024 (13:47 UTC).
  415. <a href="/bugs.html">Found a bug</a>?
  416. <br />
  417. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  418. </div>
  419. </body>
  420. </html>
上海开阖软件有限公司 沪ICP备12045867号-1