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