blob: 0585a9bfe1ee41f1670988280fb0fc8fbc204b33 [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
24 replaced with its corresponding value.
25
26
27.. function:: unescape(data[, entities])
28
29 Unescape ``'&amp;'``, ``'&lt;'``, and ``'&gt;'`` in a string of data.
30
31 You can unescape other strings of data by passing a dictionary as the optional
32 *entities* parameter. The keys and values must all be strings; each key will be
33 replaced with its corresponding value.
34
35 .. versionadded:: 2.3
36
37
38.. function:: quoteattr(data[, entities])
39
40 Similar to :func:`escape`, but also prepares *data* to be used as an
41 attribute value. The return value is a quoted version of *data* with any
42 additional required replacements. :func:`quoteattr` will select a quote
43 character based on the content of *data*, attempting to avoid encoding any
44 quote characters in the string. If both single- and double-quote characters
45 are already in *data*, the double-quote characters will be encoded and *data*
46 will be wrapped in double-quotes. The resulting string can be used directly
47 as an attribute value::
48
49 >>> print "<element attr=%s>" % quoteattr("ab ' cd \" ef")
50 <element attr="ab ' cd &quot; ef">
51
52 This function is useful when generating attribute values for HTML or any SGML
53 using the reference concrete syntax.
54
55 .. versionadded:: 2.2
56
57
58.. class:: XMLGenerator([out[, encoding]])
59
60 This class implements the :class:`ContentHandler` interface 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
66
67.. class:: XMLFilterBase(base)
68
69 This class is designed to sit between an :class:`XMLReader` and the client
70 application's event handlers. By default, it does nothing but pass requests up
71 to the reader and events on to the handlers unmodified, but subclasses can
72 override specific methods to modify the event stream or the configuration
73 requests as they pass through.
74
75
76.. function:: prepare_input_source(source[, base])
77
78 This function takes an input source and an optional base URL and returns a fully
79 resolved :class:`InputSource` object ready for reading. The input source can be
80 given as a string, a file-like object, or an :class:`InputSource` object;
81 parsers will use this function to implement the polymorphic *source* argument to
82 their :meth:`parse` method.
83