• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`xml.sax.saxutils` --- SAX Utilities
2=========================================
3
4.. module:: xml.sax.saxutils
5   :synopsis: Convenience functions and classes for use with SAX.
6
7.. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no>
8.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
9
10**Source code:** :source:`Lib/xml/sax/saxutils.py`
11
12--------------
13
14The module :mod:`xml.sax.saxutils` contains a number of classes and functions
15that are commonly useful when creating SAX applications, either in direct use,
16or as base classes.
17
18
19.. function:: escape(data, entities={})
20
21   Escape ``'&'``, ``'<'``, and ``'>'`` in a string of data.
22
23   You can escape other strings of data by passing a dictionary as the optional
24   *entities* parameter.  The keys and values must all be strings; each key will be
25   replaced with its corresponding value.  The characters ``'&'``, ``'<'`` and
26   ``'>'`` are always escaped, even if *entities* is provided.
27
28
29.. function:: unescape(data, entities={})
30
31   Unescape ``'&amp;'``, ``'&lt;'``, and ``'&gt;'`` in a string of data.
32
33   You can unescape other strings of data by passing a dictionary as the optional
34   *entities* parameter.  The keys and values must all be strings; each key will be
35   replaced with its corresponding value.  ``'&amp'``, ``'&lt;'``, and ``'&gt;'``
36   are always unescaped, even if *entities* is provided.
37
38
39.. function:: quoteattr(data, entities={})
40
41   Similar to :func:`escape`, but also prepares *data* to be used as an
42   attribute value.  The return value is a quoted version of *data* with any
43   additional required replacements. :func:`quoteattr` will select a quote
44   character based on the content of *data*, attempting to avoid encoding any
45   quote characters in the string.  If both single- and double-quote characters
46   are already in *data*, the double-quote characters will be encoded and *data*
47   will be wrapped in double-quotes.  The resulting string can be used directly
48   as an attribute value::
49
50      >>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef"))
51      <element attr="ab ' cd &quot; ef">
52
53   This function is useful when generating attribute values for HTML or any SGML
54   using the reference concrete syntax.
55
56
57.. class:: XMLGenerator(out=None, encoding='iso-8859-1', short_empty_elements=False)
58
59   This class implements the :class:`~xml.sax.handler.ContentHandler` interface
60   by writing SAX
61   events back into an XML document. In other words, using an :class:`XMLGenerator`
62   as the content handler will reproduce the original document being parsed. *out*
63   should be a file-like object which will default to *sys.stdout*. *encoding* is
64   the encoding of the output stream which defaults to ``'iso-8859-1'``.
65   *short_empty_elements* controls the formatting of elements that contain no
66   content:  if ``False`` (the default) they are emitted as a pair of start/end
67   tags, if set to ``True`` they are emitted as a single self-closed tag.
68
69   .. versionadded:: 3.2
70      The *short_empty_elements* parameter.
71
72
73.. class:: XMLFilterBase(base)
74
75   This class is designed to sit between an
76   :class:`~xml.sax.xmlreader.XMLReader` and the client
77   application's event handlers.  By default, it does nothing but pass requests up
78   to the reader and events on to the handlers unmodified, but subclasses can
79   override specific methods to modify the event stream or the configuration
80   requests as they pass through.
81
82
83.. function:: prepare_input_source(source, base='')
84
85   This function takes an input source and an optional base URL and returns a
86   fully resolved :class:`~xml.sax.xmlreader.InputSource` object ready for
87   reading.  The input source can be given as a string, a file-like object, or
88   an :class:`~xml.sax.xmlreader.InputSource` object; parsers will use this
89   function to implement the polymorphic *source* argument to their
90   :meth:`parse` method.
91
92