blob: 1bf55b4ee034c6040354d4028b295628eeba6958 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`xml.sax` --- Support for SAX2 parsers
2===========================================
3
4.. module:: xml.sax
5 :synopsis: Package containing SAX2 base classes and convenience functions.
6.. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no>
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8.. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011The :mod:`xml.sax` package provides a number of modules which implement the
12Simple API for XML (SAX) interface for Python. The package itself provides the
13SAX exceptions and the convenience functions which will be most used by users of
14the SAX API.
15
16The convenience functions are:
17
18
Georg Brandl7f01a132009-09-16 15:58:14 +000019.. function:: make_parser(parser_list=[])
Georg Brandl116aa622007-08-15 14:28:22 +000020
21 Create and return a SAX :class:`XMLReader` object. The first parser found will
22 be used. If *parser_list* is provided, it must be a sequence of strings which
23 name modules that have a function named :func:`create_parser`. Modules listed
24 in *parser_list* will be used before modules in the default list of parsers.
25
26
Georg Brandl7f01a132009-09-16 15:58:14 +000027.. function:: parse(filename_or_stream, handler, error_handler=handler.ErrorHandler())
Georg Brandl116aa622007-08-15 14:28:22 +000028
29 Create a SAX parser and use it to parse a document. The document, passed in as
30 *filename_or_stream*, can be a filename or a file object. The *handler*
31 parameter needs to be a SAX :class:`ContentHandler` instance. If
32 *error_handler* is given, it must be a SAX :class:`ErrorHandler` instance; if
33 omitted, :exc:`SAXParseException` will be raised on all errors. There is no
34 return value; all work must be done by the *handler* passed in.
35
36
Georg Brandl7f01a132009-09-16 15:58:14 +000037.. function:: parseString(string, handler, error_handler=handler.ErrorHandler())
Georg Brandl116aa622007-08-15 14:28:22 +000038
39 Similar to :func:`parse`, but parses from a buffer *string* received as a
40 parameter.
41
42A typical SAX application uses three kinds of objects: readers, handlers and
43input sources. "Reader" in this context is another term for parser, i.e. some
44piece of code that reads the bytes or characters from the input source, and
45produces a sequence of events. The events then get distributed to the handler
46objects, i.e. the reader invokes a method on the handler. A SAX application
47must therefore obtain a reader object, create or open the input sources, create
48the handlers, and connect these objects all together. As the final step of
49preparation, the reader is called to parse the input. During parsing, methods on
50the handler objects are called based on structural and syntactic events from the
51input data.
52
53For these objects, only the interfaces are relevant; they are normally not
54instantiated by the application itself. Since Python does not have an explicit
55notion of interface, they are formally introduced as classes, but applications
56may use implementations which do not inherit from the provided classes. The
57:class:`InputSource`, :class:`Locator`, :class:`Attributes`,
58:class:`AttributesNS`, and :class:`XMLReader` interfaces are defined in the
59module :mod:`xml.sax.xmlreader`. The handler interfaces are defined in
60:mod:`xml.sax.handler`. For convenience, :class:`InputSource` (which is often
61instantiated directly) and the handler classes are also available from
62:mod:`xml.sax`. These interfaces are described below.
63
64In addition to these classes, :mod:`xml.sax` provides the following exception
65classes.
66
67
Georg Brandl7f01a132009-09-16 15:58:14 +000068.. exception:: SAXException(msg, exception=None)
Georg Brandl116aa622007-08-15 14:28:22 +000069
70 Encapsulate an XML error or warning. This class can contain basic error or
71 warning information from either the XML parser or the application: it can be
72 subclassed to provide additional functionality or to add localization. Note
73 that although the handlers defined in the :class:`ErrorHandler` interface
74 receive instances of this exception, it is not required to actually raise the
75 exception --- it is also useful as a container for information.
76
77 When instantiated, *msg* should be a human-readable description of the error.
78 The optional *exception* parameter, if given, should be ``None`` or an exception
79 that was caught by the parsing code and is being passed along as information.
80
81 This is the base class for the other SAX exception classes.
82
83
84.. exception:: SAXParseException(msg, exception, locator)
85
86 Subclass of :exc:`SAXException` raised on parse errors. Instances of this class
87 are passed to the methods of the SAX :class:`ErrorHandler` interface to provide
88 information about the parse error. This class supports the SAX :class:`Locator`
89 interface as well as the :class:`SAXException` interface.
90
91
Georg Brandl7f01a132009-09-16 15:58:14 +000092.. exception:: SAXNotRecognizedException(msg, exception=None)
Georg Brandl116aa622007-08-15 14:28:22 +000093
94 Subclass of :exc:`SAXException` raised when a SAX :class:`XMLReader` is
95 confronted with an unrecognized feature or property. SAX applications and
96 extensions may use this class for similar purposes.
97
98
Georg Brandl7f01a132009-09-16 15:58:14 +000099.. exception:: SAXNotSupportedException(msg, exception=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101 Subclass of :exc:`SAXException` raised when a SAX :class:`XMLReader` is asked to
102 enable a feature that is not supported, or to set a property to a value that the
103 implementation does not support. SAX applications and extensions may use this
104 class for similar purposes.
105
106
107.. seealso::
108
109 `SAX: The Simple API for XML <http://www.saxproject.org/>`_
110 This site is the focal point for the definition of the SAX API. It provides a
111 Java implementation and online documentation. Links to implementations and
112 historical information are also available.
113
114 Module :mod:`xml.sax.handler`
115 Definitions of the interfaces for application-provided objects.
116
117 Module :mod:`xml.sax.saxutils`
118 Convenience functions for use in SAX applications.
119
120 Module :mod:`xml.sax.xmlreader`
121 Definitions of the interfaces for parser-provided objects.
122
123
124.. _sax-exception-objects:
125
126SAXException Objects
127--------------------
128
129The :class:`SAXException` exception class supports the following methods:
130
131
132.. method:: SAXException.getMessage()
133
134 Return a human-readable message describing the error condition.
135
136
137.. method:: SAXException.getException()
138
139 Return an encapsulated exception object, or ``None``.
140