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