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