Fred Drake | 014f0e3 | 2000-10-12 20:05:09 +0000 | [diff] [blame] | 1 | \section{\module{xml.sax.saxutils} --- |
| 2 | SAX Utilities} |
| 3 | |
| 4 | \declaremodule{standard}{xml.sax.saxutils} |
| 5 | \modulesynopsis{Convenience functions and classes for use with SAX.} |
Martin v. Löwis | 338bcbc | 2003-04-18 22:04:34 +0000 | [diff] [blame] | 6 | \sectionauthor{Martin v. L\"owis}{martin@v.loewis.de} |
Fred Drake | 014f0e3 | 2000-10-12 20:05:09 +0000 | [diff] [blame] | 7 | \moduleauthor{Lars Marius Garshol}{larsga@garshol.priv.no} |
| 8 | |
| 9 | \versionadded{2.0} |
| 10 | |
| 11 | |
| 12 | The module \module{xml.sax.saxutils} contains a number of classes and |
| 13 | functions that are commonly useful when creating SAX applications, |
| 14 | either in direct use, or as base classes. |
| 15 | |
| 16 | \begin{funcdesc}{escape}{data\optional{, entities}} |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 17 | Escape \character{\&}, \character{<}, and \character{>} in a string |
| 18 | of data. |
Fred Drake | 014f0e3 | 2000-10-12 20:05:09 +0000 | [diff] [blame] | 19 | |
| 20 | You can escape other strings of data by passing a dictionary as the |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 21 | optional \var{entities} parameter. The keys and values must all be |
Fred Drake | 014f0e3 | 2000-10-12 20:05:09 +0000 | [diff] [blame] | 22 | strings; each key will be replaced with its corresponding value. |
| 23 | \end{funcdesc} |
| 24 | |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 25 | \begin{funcdesc}{unescape}{data\optional{, entities}} |
| 26 | Unescape \character{\&}, \character{\<}, and \character{\>} |
| 27 | in a string of data. |
| 28 | |
| 29 | You can unescape other strings of data by passing a dictionary as the |
| 30 | optional \var{entities} parameter. The keys and values must all be |
| 31 | strings; each key will be replaced with its corresponding value. |
| 32 | |
| 33 | \versionadded{2.3} |
| 34 | \end{funcdesc} |
| 35 | |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 36 | \begin{funcdesc}{quoteattr}{data\optional{, entities}} |
| 37 | Similar to \function{escape()}, but also prepares \var{data} to be |
| 38 | used as an attribute value. The return value is a quoted version of |
| 39 | \var{data} with any additional required replacements. |
| 40 | \function{quoteattr()} will select a quote character based on the |
| 41 | content of \var{data}, attempting to avoid encoding any quote |
| 42 | characters in the string. If both single- and double-quote |
| 43 | characters are already in \var{data}, the double-quote characters |
| 44 | will be encoded and \var{data} will be wrapped in doule-quotes. The |
| 45 | resulting string can be used directly as an attribute value: |
| 46 | |
| 47 | \begin{verbatim} |
| 48 | >>> print "<element attr=%s>" % quoteattr("ab ' cd \" ef") |
| 49 | <element attr="ab ' cd " ef"> |
| 50 | \end{verbatim} |
| 51 | |
Fred Drake | 1cf0f17 | 2001-08-10 22:14:17 +0000 | [diff] [blame] | 52 | This function is useful when generating attribute values for HTML or |
| 53 | any SGML using the reference concrete syntax. |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 54 | \versionadded{2.2} |
| 55 | \end{funcdesc} |
| 56 | |
Fred Drake | 014f0e3 | 2000-10-12 20:05:09 +0000 | [diff] [blame] | 57 | \begin{classdesc}{XMLGenerator}{\optional{out\optional{, encoding}}} |
| 58 | This class implements the \class{ContentHandler} interface by |
| 59 | writing SAX events back into an XML document. In other words, using |
| 60 | an \class{XMLGenerator} as the content handler will reproduce the |
| 61 | original document being parsed. \var{out} should be a file-like |
| 62 | object which will default to \var{sys.stdout}. \var{encoding} is the |
| 63 | encoding of the output stream which defaults to \code{'iso-8859-1'}. |
| 64 | \end{classdesc} |
| 65 | |
| 66 | \begin{classdesc}{XMLFilterBase}{base} |
| 67 | This class is designed to sit between an \class{XMLReader} and the |
| 68 | client application's event handlers. By default, it does nothing |
| 69 | but pass requests up to the reader and events on to the handlers |
| 70 | unmodified, but subclasses can override specific methods to modify |
| 71 | the event stream or the configuration requests as they pass through. |
| 72 | \end{classdesc} |
| 73 | |
| 74 | \begin{funcdesc}{prepare_input_source}{source\optional{, base}} |
| 75 | This function takes an input source and an optional base URL and |
| 76 | returns a fully resolved \class{InputSource} object ready for |
| 77 | reading. The input source can be given as a string, a file-like |
| 78 | object, or an \class{InputSource} object; parsers will use this |
| 79 | function to implement the polymorphic \var{source} argument to their |
| 80 | \method{parse()} method. |
| 81 | \end{funcdesc} |