blob: 23f429e07323e33673269118e1ecca7bc625bc07 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`xml.sax.handler` --- Base classes for SAX handlers
3========================================================
4
5.. module:: xml.sax.handler
6 :synopsis: Base classes for SAX event handlers.
7.. moduleauthor:: Lars Marius Garshol <larsga@garshol.priv.no>
8.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
9
10
11.. versionadded:: 2.0
12
13The SAX API defines four kinds of handlers: content handlers, DTD handlers,
14error handlers, and entity resolvers. Applications normally only need to
15implement those interfaces whose events they are interested in; they can
16implement the interfaces in a single object or in multiple objects. Handler
17implementations should inherit from the base classes provided in the module
18:mod:`xml.sax.handler`, so that all methods get default implementations.
19
20
21.. class:: ContentHandler
22
23 This is the main callback interface in SAX, and the one most important to
24 applications. The order of events in this interface mirrors the order of the
25 information in the document.
26
27
28.. class:: DTDHandler
29
30 Handle DTD events.
31
32 This interface specifies only those DTD events required for basic parsing
33 (unparsed entities and attributes).
34
35
36.. class:: EntityResolver
37
38 Basic interface for resolving entities. If you create an object implementing
39 this interface, then register the object with your Parser, the parser will call
40 the method in your object to resolve all external entities.
41
42
43.. class:: ErrorHandler
44
45 Interface used by the parser to present error and warning messages to the
46 application. The methods of this object control whether errors are immediately
47 converted to exceptions or are handled in some other way.
48
49In addition to these classes, :mod:`xml.sax.handler` provides symbolic constants
50for the feature and property names.
51
52
53.. data:: feature_namespaces
54
Georg Brandl3b85b9b2010-11-26 08:20:18 +000055 | value: ``"http://xml.org/sax/features/namespaces"``
56 | true: Perform Namespace processing.
57 | false: Optionally do not perform Namespace processing (implies
58 namespace-prefixes; default).
59 | access: (parsing) read-only; (not parsing) read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
61
62.. data:: feature_namespace_prefixes
63
Georg Brandl3b85b9b2010-11-26 08:20:18 +000064 | value: ``"http://xml.org/sax/features/namespace-prefixes"``
65 | true: Report the original prefixed names and attributes used for Namespace
66 declarations.
67 | false: Do not report attributes used for Namespace declarations, and
68 optionally do not report original prefixed names (default).
69 | access: (parsing) read-only; (not parsing) read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
71
72.. data:: feature_string_interning
73
Georg Brandl3b85b9b2010-11-26 08:20:18 +000074 | value: ``"http://xml.org/sax/features/string-interning"``
75 | true: All element names, prefixes, attribute names, Namespace URIs, and
76 local names are interned using the built-in intern function.
77 | false: Names are not necessarily interned, although they may be (default).
78 | access: (parsing) read-only; (not parsing) read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80
81.. data:: feature_validation
82
Georg Brandl3b85b9b2010-11-26 08:20:18 +000083 | value: ``"http://xml.org/sax/features/validation"``
84 | true: Report all validation errors (implies external-general-entities and
85 external-parameter-entities).
86 | false: Do not report validation errors.
87 | access: (parsing) read-only; (not parsing) read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
89
90.. data:: feature_external_ges
91
Georg Brandl3b85b9b2010-11-26 08:20:18 +000092 | value: ``"http://xml.org/sax/features/external-general-entities"``
93 | true: Include all external general (text) entities.
94 | false: Do not include external general entities.
95 | access: (parsing) read-only; (not parsing) read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
97
98.. data:: feature_external_pes
99
Georg Brandl3b85b9b2010-11-26 08:20:18 +0000100 | value: ``"http://xml.org/sax/features/external-parameter-entities"``
101 | true: Include all external parameter entities, including the external DTD
102 subset.
103 | false: Do not include any external parameter entities, even the external
104 DTD subset.
105 | access: (parsing) read-only; (not parsing) read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +0000106
107
108.. data:: all_features
109
110 List of all features.
111
112
113.. data:: property_lexical_handler
114
Georg Brandl3b85b9b2010-11-26 08:20:18 +0000115 | value: ``"http://xml.org/sax/properties/lexical-handler"``
116 | data type: xml.sax.sax2lib.LexicalHandler (not supported in Python 2)
117 | description: An optional extension handler for lexical events like
118 comments.
119 | access: read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
121
122.. data:: property_declaration_handler
123
Georg Brandl3b85b9b2010-11-26 08:20:18 +0000124 | value: ``"http://xml.org/sax/properties/declaration-handler"``
125 | data type: xml.sax.sax2lib.DeclHandler (not supported in Python 2)
126 | description: An optional extension handler for DTD-related events other
127 than notations and unparsed entities.
128 | access: read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +0000129
130
131.. data:: property_dom_node
132
Georg Brandl3b85b9b2010-11-26 08:20:18 +0000133 | value: ``"http://xml.org/sax/properties/dom-node"``
134 | data type: org.w3c.dom.Node (not supported in Python 2)
135 | description: When parsing, the current DOM node being visited if this is
136 a DOM iterator; when not parsing, the root DOM node for iteration.
137 | access: (parsing) read-only; (not parsing) read/write
Georg Brandl8ec7f652007-08-15 14:28:01 +0000138
139
140.. data:: property_xml_string
141
Georg Brandl3b85b9b2010-11-26 08:20:18 +0000142 | value: ``"http://xml.org/sax/properties/xml-string"``
143 | data type: String
144 | description: The literal string of characters that was the source for the
145 current event.
146 | access: read-only
Georg Brandl8ec7f652007-08-15 14:28:01 +0000147
148
149.. data:: all_properties
150
151 List of all known property names.
152
153
154.. _content-handler-objects:
155
156ContentHandler Objects
157----------------------
158
159Users are expected to subclass :class:`ContentHandler` to support their
160application. The following methods are called by the parser on the appropriate
161events in the input document:
162
163
164.. method:: ContentHandler.setDocumentLocator(locator)
165
166 Called by the parser to give the application a locator for locating the origin
167 of document events.
168
169 SAX parsers are strongly encouraged (though not absolutely required) to supply a
170 locator: if it does so, it must supply the locator to the application by
171 invoking this method before invoking any of the other methods in the
172 DocumentHandler interface.
173
174 The locator allows the application to determine the end position of any
175 document-related event, even if the parser is not reporting an error. Typically,
176 the application will use this information for reporting its own errors (such as
177 character content that does not match an application's business rules). The
178 information returned by the locator is probably not sufficient for use with a
179 search engine.
180
181 Note that the locator will return correct information only during the invocation
182 of the events in this interface. The application should not attempt to use it at
183 any other time.
184
185
186.. method:: ContentHandler.startDocument()
187
188 Receive notification of the beginning of a document.
189
190 The SAX parser will invoke this method only once, before any other methods in
191 this interface or in DTDHandler (except for :meth:`setDocumentLocator`).
192
193
194.. method:: ContentHandler.endDocument()
195
196 Receive notification of the end of a document.
197
198 The SAX parser will invoke this method only once, and it will be the last method
199 invoked during the parse. The parser shall not invoke this method until it has
200 either abandoned parsing (because of an unrecoverable error) or reached the end
201 of input.
202
203
204.. method:: ContentHandler.startPrefixMapping(prefix, uri)
205
206 Begin the scope of a prefix-URI Namespace mapping.
207
208 The information from this event is not necessary for normal Namespace
209 processing: the SAX XML reader will automatically replace prefixes for element
210 and attribute names when the ``feature_namespaces`` feature is enabled (the
211 default).
212
213 There are cases, however, when applications need to use prefixes in character
214 data or in attribute values, where they cannot safely be expanded automatically;
215 the :meth:`startPrefixMapping` and :meth:`endPrefixMapping` events supply the
216 information to the application to expand prefixes in those contexts itself, if
217 necessary.
218
Georg Brandlb19be572007-12-29 10:57:00 +0000219 .. XXX This is not really the default, is it? MvL
Georg Brandl8ec7f652007-08-15 14:28:01 +0000220
221 Note that :meth:`startPrefixMapping` and :meth:`endPrefixMapping` events are not
222 guaranteed to be properly nested relative to each-other: all
223 :meth:`startPrefixMapping` events will occur before the corresponding
224 :meth:`startElement` event, and all :meth:`endPrefixMapping` events will occur
225 after the corresponding :meth:`endElement` event, but their order is not
226 guaranteed.
227
228
229.. method:: ContentHandler.endPrefixMapping(prefix)
230
231 End the scope of a prefix-URI mapping.
232
233 See :meth:`startPrefixMapping` for details. This event will always occur after
234 the corresponding :meth:`endElement` event, but the order of
235 :meth:`endPrefixMapping` events is not otherwise guaranteed.
236
237
238.. method:: ContentHandler.startElement(name, attrs)
239
240 Signals the start of an element in non-namespace mode.
241
242 The *name* parameter contains the raw XML 1.0 name of the element type as a
243 string and the *attrs* parameter holds an object of the :class:`Attributes`
244 interface (see :ref:`attributes-objects`) containing the attributes of
245 the element. The object passed as *attrs* may be re-used by the parser; holding
246 on to a reference to it is not a reliable way to keep a copy of the attributes.
247 To keep a copy of the attributes, use the :meth:`copy` method of the *attrs*
248 object.
249
250
251.. method:: ContentHandler.endElement(name)
252
253 Signals the end of an element in non-namespace mode.
254
255 The *name* parameter contains the name of the element type, just as with the
256 :meth:`startElement` event.
257
258
259.. method:: ContentHandler.startElementNS(name, qname, attrs)
260
261 Signals the start of an element in namespace mode.
262
263 The *name* parameter contains the name of the element type as a ``(uri,
264 localname)`` tuple, the *qname* parameter contains the raw XML 1.0 name used in
265 the source document, and the *attrs* parameter holds an instance of the
266 :class:`AttributesNS` interface (see :ref:`attributes-ns-objects`)
267 containing the attributes of the element. If no namespace is associated with
268 the element, the *uri* component of *name* will be ``None``. The object passed
269 as *attrs* may be re-used by the parser; holding on to a reference to it is not
270 a reliable way to keep a copy of the attributes. To keep a copy of the
271 attributes, use the :meth:`copy` method of the *attrs* object.
272
273 Parsers may set the *qname* parameter to ``None``, unless the
274 ``feature_namespace_prefixes`` feature is activated.
275
276
277.. method:: ContentHandler.endElementNS(name, qname)
278
279 Signals the end of an element in namespace mode.
280
281 The *name* parameter contains the name of the element type, just as with the
282 :meth:`startElementNS` method, likewise the *qname* parameter.
283
284
285.. method:: ContentHandler.characters(content)
286
287 Receive notification of character data.
288
289 The Parser will call this method to report each chunk of character data. SAX
290 parsers may return all contiguous character data in a single chunk, or they may
291 split it into several chunks; however, all of the characters in any single event
292 must come from the same external entity so that the Locator provides useful
293 information.
294
295 *content* may be a Unicode string or a byte string; the ``expat`` reader module
296 produces always Unicode strings.
297
298 .. note::
299
300 The earlier SAX 1 interface provided by the Python XML Special Interest Group
301 used a more Java-like interface for this method. Since most parsers used from
302 Python did not take advantage of the older interface, the simpler signature was
303 chosen to replace it. To convert old code to the new interface, use *content*
304 instead of slicing content with the old *offset* and *length* parameters.
305
306
307.. method:: ContentHandler.ignorableWhitespace(whitespace)
308
309 Receive notification of ignorable whitespace in element content.
310
311 Validating Parsers must use this method to report each chunk of ignorable
312 whitespace (see the W3C XML 1.0 recommendation, section 2.10): non-validating
313 parsers may also use this method if they are capable of parsing and using
314 content models.
315
316 SAX parsers may return all contiguous whitespace in a single chunk, or they may
317 split it into several chunks; however, all of the characters in any single event
318 must come from the same external entity, so that the Locator provides useful
319 information.
320
321
322.. method:: ContentHandler.processingInstruction(target, data)
323
324 Receive notification of a processing instruction.
325
326 The Parser will invoke this method once for each processing instruction found:
327 note that processing instructions may occur before or after the main document
328 element.
329
330 A SAX parser should never report an XML declaration (XML 1.0, section 2.8) or a
331 text declaration (XML 1.0, section 4.3.1) using this method.
332
333
334.. method:: ContentHandler.skippedEntity(name)
335
336 Receive notification of a skipped entity.
337
338 The Parser will invoke this method once for each entity skipped. Non-validating
339 processors may skip entities if they have not seen the declarations (because,
340 for example, the entity was declared in an external DTD subset). All processors
341 may skip external entities, depending on the values of the
342 ``feature_external_ges`` and the ``feature_external_pes`` properties.
343
344
345.. _dtd-handler-objects:
346
347DTDHandler Objects
348------------------
349
350:class:`DTDHandler` instances provide the following methods:
351
352
353.. method:: DTDHandler.notationDecl(name, publicId, systemId)
354
355 Handle a notation declaration event.
356
357
358.. method:: DTDHandler.unparsedEntityDecl(name, publicId, systemId, ndata)
359
360 Handle an unparsed entity declaration event.
361
362
363.. _entity-resolver-objects:
364
365EntityResolver Objects
366----------------------
367
368
369.. method:: EntityResolver.resolveEntity(publicId, systemId)
370
371 Resolve the system identifier of an entity and return either the system
372 identifier to read from as a string, or an InputSource to read from. The default
373 implementation returns *systemId*.
374
375
376.. _sax-error-handler:
377
378ErrorHandler Objects
379--------------------
380
381Objects with this interface are used to receive error and warning information
382from the :class:`XMLReader`. If you create an object that implements this
383interface, then register the object with your :class:`XMLReader`, the parser
384will call the methods in your object to report all warnings and errors. There
385are three levels of errors available: warnings, (possibly) recoverable errors,
386and unrecoverable errors. All methods take a :exc:`SAXParseException` as the
387only parameter. Errors and warnings may be converted to an exception by raising
388the passed-in exception object.
389
390
391.. method:: ErrorHandler.error(exception)
392
393 Called when the parser encounters a recoverable error. If this method does not
394 raise an exception, parsing may continue, but further document information
395 should not be expected by the application. Allowing the parser to continue may
396 allow additional errors to be discovered in the input document.
397
398
399.. method:: ErrorHandler.fatalError(exception)
400
401 Called when the parser encounters an error it cannot recover from; parsing is
402 expected to terminate when this method returns.
403
404
405.. method:: ErrorHandler.warning(exception)
406
407 Called when the parser presents minor warning information to the application.
408 Parsing is expected to continue when this method returns, and document
409 information will continue to be passed to the application. Raising an exception
410 in this method will cause parsing to end.
411