Fred Drake | e10ef74 | 2000-09-20 02:52:20 +0000 | [diff] [blame] | 1 | \section{\module{xml.sax} --- |
| 2 | Support for SAX2 parsers} |
| 3 | |
| 4 | \declaremodule{standard}{xml.sax} |
| 5 | \modulesynopsis{Package containing SAX2 base classes and convenience |
| 6 | functions.} |
| 7 | \moduleauthor{Lars Marius Garshol}{larsga@garshol.priv.no} |
| 8 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 9 | |
| 10 | \versionadded{2.0} |
| 11 | |
| 12 | |
| 13 | The \module{xml.sax} package provides a number of modules which |
| 14 | implement the Simple API for XML (SAX) interface for Python. The |
| 15 | package itself provides the SAX exceptions and the convenience |
| 16 | functions which will be most used by users of the SAX API. |
| 17 | |
| 18 | The convenience functions are: |
| 19 | |
| 20 | \begin{funcdesc}{make_parser}{\optional{parser_list}} |
| 21 | Create and return a SAX \class{XMLReader} object. The first parser |
| 22 | found will be used. If \var{parser_list} is provided, it must be a |
| 23 | sequence of strings which name modules that have a function named |
| 24 | \function{create_parser()}. Modules listed in \var{parser_list} |
| 25 | will be used before modules in the default list of parsers. |
| 26 | \end{funcdesc} |
| 27 | |
| 28 | \begin{funcdesc}{parse}{filename_or_stream, handler\optional{, error_handler}} |
| 29 | Create a SAX parser and use it to parse a document. The document, |
| 30 | passed in as \var{filename_or_stream}, can be a filename or a file |
| 31 | object. The \var{handler} parameter needs to be a SAX |
| 32 | \class{ContentHandler} instance. If \var{error_handler} is given, |
| 33 | it must be a SAX \class{ErrorHandler} instance; if omitted, |
| 34 | \exception{SAXParseException} will be raised on all errors. There |
| 35 | is no return value; all work must be done by the \var{handler} |
| 36 | passed in. |
| 37 | \end{funcdesc} |
| 38 | |
| 39 | \begin{funcdesc}{parseString}{string, handler\optional{, error_handler}} |
| 40 | Similar to \function{parse()}, but parses from a buffer \var{string} |
| 41 | received as a parameter. |
| 42 | \end{funcdesc} |
| 43 | |
| 44 | |
| 45 | The SAX exceptions are also provided here: |
| 46 | |
| 47 | \begin{excclassdesc}{SAXException}{msg\optional{, exception}} |
| 48 | Encapsulate an XML error or warning. This class can contain basic |
| 49 | error or warning information from either the XML parser or the |
| 50 | application: it can be subclassed to provide additional |
| 51 | functionality or to add localization. Note that although the |
| 52 | handlers defined in the \class{ErrorHandler} interface receive |
| 53 | instances of this exception, it is not required to actually raise |
| 54 | the exception --- it is also useful as a container for information. |
| 55 | |
| 56 | When instantiated, \var{msg} should be a human-readable description |
| 57 | of the error. The optional \var{exception} parameter, if given, |
| 58 | should be \code{None} or an exception that was caught by the parsing |
| 59 | code and is being passed along as information. |
| 60 | |
| 61 | This is the base class for the other SAX exception classes. |
| 62 | \end{excclassdesc} |
| 63 | |
| 64 | \begin{excclassdesc}{SAXParseException}{msg, exception, locator} |
| 65 | Subclass of \exception{SAXException} raised on parse errors. |
| 66 | Instances of this class are passed to the methods of the SAX |
| 67 | \class{ErrorHandler} interface to provide information about the |
| 68 | parse error. This class supports the SAX \class{Locator} interface |
| 69 | as well as the \class{SAXException} interface. |
| 70 | \end{excclassdesc} |
| 71 | |
| 72 | \begin{excclassdesc}{SAXNotRecognizedException}{msg\optional{, exception}} |
| 73 | Subclass of \exception{SAXException} raised when a SAX |
| 74 | \class{XMLReader} is confronted with an unrecognized feature or |
| 75 | property. SAX applications and extensions may use this class for |
| 76 | similar purposes. |
| 77 | \end{excclassdesc} |
| 78 | |
| 79 | \begin{excclassdesc}{SAXNotSupportedException}{msg\optional{, exception}} |
| 80 | Subclass of \exception{SAXException} raised when a SAX |
| 81 | \class{XMLReader} is asked to enable a feature that is not |
| 82 | supported, or to set a property to a value that the implementation |
| 83 | does not support. SAX applications and extensions may use this |
| 84 | class for similar purposes. |
| 85 | \end{excclassdesc} |
| 86 | |
| 87 | |
| 88 | \begin{seealso} |
| 89 | \seetitle[http://www.megginson.com/SAX/]{SAX: The Simple API for |
| 90 | XML}{This site is the focal point for the definition of |
| 91 | the SAX API. It provides a Java implementation and online |
| 92 | documentation. Links to implementations and historical |
| 93 | information are also available.} |
| 94 | \end{seealso} |
| 95 | |
| 96 | |
| 97 | \subsection{SAXException Objects \label{saxexception-objects}} |
| 98 | |
| 99 | The \class{SAXException} exception class supports the following |
| 100 | methods: |
| 101 | |
| 102 | \begin{methoddesc}[SAXException]{getMessage}{} |
| 103 | Return a human-readable message describing the error condition. |
| 104 | \end{methoddesc} |
| 105 | |
| 106 | \begin{methoddesc}[SAXException]{getException}{} |
| 107 | Return an encapsulated exception object, or \code{None}. |
| 108 | \end{methoddesc} |