summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fxml.sax.utils.html
blob: f438507b017f98f4053384d038a6f57a085555c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 <span id="xml-sax-saxutils-sax-utilities"></span><h1>xml.sax.saxutils — SAX Utilities</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/xml/sax/saxutils.py">Lib/xml/sax/saxutils.py</a></p>  <p>The module <a class="reference internal" href="#module-xml.sax.saxutils" title="xml.sax.saxutils: Convenience functions and classes for use with SAX."><code>xml.sax.saxutils</code></a> contains a number of classes and functions that are commonly useful when creating SAX applications, either in direct use, or as base classes.</p> <dl class="py function"> <dt class="sig sig-object py" id="xml.sax.saxutils.escape">
<code>xml.sax.saxutils.escape(data, entities={})</code> </dt> <dd>
<p>Escape <code>'&amp;'</code>, <code>'&lt;'</code>, and <code>'&gt;'</code> in a string of data.</p> <p>You can escape other strings of data by passing a dictionary as the optional <em>entities</em> parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. The characters <code>'&amp;'</code>, <code>'&lt;'</code> and <code>'&gt;'</code> are always escaped, even if <em>entities</em> is provided.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function should only be used to escape characters that can’t be used directly in XML. Do not use this function as a general string translation function.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.sax.saxutils.unescape">
<code>xml.sax.saxutils.unescape(data, entities={})</code> </dt> <dd>
<p>Unescape <code>'&amp;amp;'</code>, <code>'&amp;lt;'</code>, and <code>'&amp;gt;'</code> in a string of data.</p> <p>You can unescape other strings of data by passing a dictionary as the optional <em>entities</em> parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. <code>'&amp;amp'</code>, <code>'&amp;lt;'</code>, and <code>'&amp;gt;'</code> are always unescaped, even if <em>entities</em> is provided.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.sax.saxutils.quoteattr">
<code>xml.sax.saxutils.quoteattr(data, entities={})</code> </dt> <dd>
<p>Similar to <a class="reference internal" href="#xml.sax.saxutils.escape" title="xml.sax.saxutils.escape"><code>escape()</code></a>, but also prepares <em>data</em> to be used as an attribute value. The return value is a quoted version of <em>data</em> with any additional required replacements. <a class="reference internal" href="#xml.sax.saxutils.quoteattr" title="xml.sax.saxutils.quoteattr"><code>quoteattr()</code></a> will select a quote character based on the content of <em>data</em>, attempting to avoid encoding any quote characters in the string. If both single- and double-quote characters are already in <em>data</em>, the double-quote characters will be encoded and <em>data</em> will be wrapped in double-quotes. The resulting string can be used directly as an attribute value:</p> <pre data-language="python">&gt;&gt;&gt; print("&lt;element attr=%s&gt;" % quoteattr("ab ' cd \" ef"))
&lt;element attr="ab ' cd &amp;quot; ef"&gt;
</pre> <p>This function is useful when generating attribute values for HTML or any SGML using the reference concrete syntax.</p> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="xml.sax.saxutils.XMLGenerator">
<code>class xml.sax.saxutils.XMLGenerator(out=None, encoding='iso-8859-1', short_empty_elements=False)</code> </dt> <dd>
<p>This class implements the <a class="reference internal" href="xml.sax.handler#xml.sax.handler.ContentHandler" title="xml.sax.handler.ContentHandler"><code>ContentHandler</code></a> interface by writing SAX events back into an XML document. In other words, using an <a class="reference internal" href="#xml.sax.saxutils.XMLGenerator" title="xml.sax.saxutils.XMLGenerator"><code>XMLGenerator</code></a> as the content handler will reproduce the original document being parsed. <em>out</em> should be a file-like object which will default to <em>sys.stdout</em>. <em>encoding</em> is the encoding of the output stream which defaults to <code>'iso-8859-1'</code>. <em>short_empty_elements</em> controls the formatting of elements that contain no content: if <code>False</code> (the default) they are emitted as a pair of start/end tags, if set to <code>True</code> they are emitted as a single self-closed tag.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2: </span>The <em>short_empty_elements</em> parameter.</p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="xml.sax.saxutils.XMLFilterBase">
<code>class xml.sax.saxutils.XMLFilterBase(base)</code> </dt> <dd>
<p>This class is designed to sit between an <a class="reference internal" href="xml.sax.reader#xml.sax.xmlreader.XMLReader" title="xml.sax.xmlreader.XMLReader"><code>XMLReader</code></a> and the client application’s event handlers. By default, it does nothing but pass requests up to the reader and events on to the handlers unmodified, but subclasses can override specific methods to modify the event stream or the configuration requests as they pass through.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="xml.sax.saxutils.prepare_input_source">
<code>xml.sax.saxutils.prepare_input_source(source, base='')</code> </dt> <dd>
<p>This function takes an input source and an optional base URL and returns a fully resolved <a class="reference internal" href="xml.sax.reader#xml.sax.xmlreader.InputSource" title="xml.sax.xmlreader.InputSource"><code>InputSource</code></a> object ready for reading. The input source can be given as a string, a file-like object, or an <a class="reference internal" href="xml.sax.reader#xml.sax.xmlreader.InputSource" title="xml.sax.xmlreader.InputSource"><code>InputSource</code></a> object; parsers will use this function to implement the polymorphic <em>source</em> argument to their <a class="reference internal" href="xml.sax.reader#xml.sax.xmlreader.XMLReader.parse" title="xml.sax.xmlreader.XMLReader.parse"><code>parse()</code></a> method.</p> </dd>
</dl> <div class="_attribution">
  <p class="_attribution-p">
    &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
    <a href="https://docs.python.org/3.12/library/xml.sax.utils.html" class="_attribution-link">https://docs.python.org/3.12/library/xml.sax.utils.html</a>
  </p>
</div>