blob: cd16348a3b6cf97bb39f510edea91271bc3206f4 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl116aa622007-08-15 14:28:22 +000011The module :mod:`xml.sax.saxutils` contains a number of classes and functions
12that are commonly useful when creating SAX applications, either in direct use,
13or as base classes.
14
15
16.. function:: escape(data[, entities])
17
18 Escape ``'&'``, ``'<'``, and ``'>'`` in a string of data.
19
20 You can escape other strings of data by passing a dictionary as the optional
21 *entities* parameter. The keys and values must all be strings; each key will be
22 replaced with its corresponding value.
23
24
25.. function:: unescape(data[, entities])
26
27 Unescape ``'&amp;'``, ``'&lt;'``, and ``'&gt;'`` in a string of data.
28
29 You can unescape other strings of data by passing a dictionary as the optional
30 *entities* parameter. The keys and values must all be strings; each key will be
31 replaced with its corresponding value.
32
Georg Brandl116aa622007-08-15 14:28:22 +000033
34.. function:: quoteattr(data[, entities])
35
36 Similar to :func:`escape`, but also prepares *data* to be used as an
37 attribute value. The return value is a quoted version of *data* with any
38 additional required replacements. :func:`quoteattr` will select a quote
39 character based on the content of *data*, attempting to avoid encoding any
40 quote characters in the string. If both single- and double-quote characters
41 are already in *data*, the double-quote characters will be encoded and *data*
42 will be wrapped in double-quotes. The resulting string can be used directly
43 as an attribute value::
44
Collin Winterc79461b2007-09-01 23:34:30 +000045 >>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef"))
Georg Brandl116aa622007-08-15 14:28:22 +000046 <element attr="ab ' cd &quot; ef">
47
48 This function is useful when generating attribute values for HTML or any SGML
49 using the reference concrete syntax.
50
Georg Brandl116aa622007-08-15 14:28:22 +000051
52.. class:: XMLGenerator([out[, encoding]])
53
54 This class implements the :class:`ContentHandler` interface by writing SAX
55 events back into an XML document. In other words, using an :class:`XMLGenerator`
56 as the content handler will reproduce the original document being parsed. *out*
57 should be a file-like object which will default to *sys.stdout*. *encoding* is
58 the encoding of the output stream which defaults to ``'iso-8859-1'``.
59
60
61.. class:: XMLFilterBase(base)
62
63 This class is designed to sit between an :class:`XMLReader` and the client
64 application's event handlers. By default, it does nothing but pass requests up
65 to the reader and events on to the handlers unmodified, but subclasses can
66 override specific methods to modify the event stream or the configuration
67 requests as they pass through.
68
69
70.. function:: prepare_input_source(source[, base])
71
72 This function takes an input source and an optional base URL and returns a fully
73 resolved :class:`InputSource` object ready for reading. The input source can be
74 given as a string, a file-like object, or an :class:`InputSource` object;
75 parsers will use this function to implement the polymorphic *source* argument to
76 their :meth:`parse` method.
77