ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](xml.sax.reader.xhtml "xml.sax.xmlreader --- Interface for XML parsers") | - [上一页](xml.sax.handler.xhtml "xml.sax.handler --- Base classes for SAX handlers") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [结构化标记处理工具](markup.xhtml) » - $('.inline-search').show(0); | # [`xml.sax.saxutils`](#module-xml.sax.saxutils "xml.sax.saxutils: Convenience functions and classes for use with SAX.") --- SAX Utilities **Source code:** [Lib/xml/sax/saxutils.py](https://github.com/python/cpython/tree/3.7/Lib/xml/sax/saxutils.py) \[https://github.com/python/cpython/tree/3.7/Lib/xml/sax/saxutils.py\] - - - - - - The module [`xml.sax.saxutils`](#module-xml.sax.saxutils "xml.sax.saxutils: Convenience functions and classes for use with SAX.") contains a number of classes and functions that are commonly useful when creating SAX applications, either in direct use, or as base classes. `xml.sax.saxutils.``escape`(*data*, *entities={}*)Escape `'&'`, `'<'`, and `'>'` in a string of data. You can escape other strings of data by passing a dictionary as the optional *entities* parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. The characters `'&'`, `'<'` and `'>'` are always escaped, even if *entities* is provided. `xml.sax.saxutils.``unescape`(*data*, *entities={}*)Unescape `'&'`, `'<'`, and `'>'` in a string of data. You can unescape other strings of data by passing a dictionary as the optional *entities* parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. `'&amp'`, `'<'`, and `'>'`are always unescaped, even if *entities* is provided. `xml.sax.saxutils.``quoteattr`(*data*, *entities={}*)Similar to [`escape()`](#xml.sax.saxutils.escape "xml.sax.saxutils.escape"), but also prepares *data* to be used as an attribute value. The return value is a quoted version of *data* with any additional required replacements. [`quoteattr()`](#xml.sax.saxutils.quoteattr "xml.sax.saxutils.quoteattr") will select a quote character based on the content of *data*, attempting to avoid encoding any quote characters in the string. If both single- and double-quote characters are already in *data*, the double-quote characters will be encoded and *data*will be wrapped in double-quotes. The resulting string can be used directly as an attribute value: ``` >>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef")) <element attr="ab ' cd " ef"> ``` This function is useful when generating attribute values for HTML or any SGML using the reference concrete syntax. *class* `xml.sax.saxutils.``XMLGenerator`(*out=None*, *encoding='iso-8859-1'*, *short\_empty\_elements=False*)This class implements the [`ContentHandler`](xml.sax.handler.xhtml#xml.sax.handler.ContentHandler "xml.sax.handler.ContentHandler") interface by writing SAX events back into an XML document. In other words, using an [`XMLGenerator`](#xml.sax.saxutils.XMLGenerator "xml.sax.saxutils.XMLGenerator")as the content handler will reproduce the original document being parsed. *out*should be a file-like object which will default to *sys.stdout*. *encoding* is the encoding of the output stream which defaults to `'iso-8859-1'`. *short\_empty\_elements* controls the formatting of elements that contain no content: if `False` (the default) they are emitted as a pair of start/end tags, if set to `True` they are emitted as a single self-closed tag. 3\.2 新版功能: The *short\_empty\_elements* parameter. *class* `xml.sax.saxutils.``XMLFilterBase`(*base*)This class is designed to sit between an [`XMLReader`](xml.sax.reader.xhtml#xml.sax.xmlreader.XMLReader "xml.sax.xmlreader.XMLReader") 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. `xml.sax.saxutils.``prepare_input_source`(*source*, *base=''*)This function takes an input source and an optional base URL and returns a fully resolved [`InputSource`](xml.sax.reader.xhtml#xml.sax.xmlreader.InputSource "xml.sax.xmlreader.InputSource") object ready for reading. The input source can be given as a string, a file-like object, or an [`InputSource`](xml.sax.reader.xhtml#xml.sax.xmlreader.InputSource "xml.sax.xmlreader.InputSource") object; parsers will use this function to implement the polymorphic *source* argument to their `parse()` method. ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](xml.sax.reader.xhtml "xml.sax.xmlreader --- Interface for XML parsers") | - [上一页](xml.sax.handler.xhtml "xml.sax.handler --- Base classes for SAX handlers") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [结构化标记处理工具](markup.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。