blob: 3ab60636c5ecb28aa56bc3735d4ca855ded7e105 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`xml.sax.xmlreader` --- Interface for XML parsers
2======================================================
3
4.. module:: xml.sax.xmlreader
5 :synopsis: Interface which SAX-compliant XML parsers must implement.
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 +000010SAX parsers implement the :class:`XMLReader` interface. They are implemented in
11a Python module, which must provide a function :func:`create_parser`. This
12function is invoked by :func:`xml.sax.make_parser` with no arguments to create
13a new parser object.
14
15
16.. class:: XMLReader()
17
18 Base class which can be inherited by SAX parsers.
19
20
21.. class:: IncrementalParser()
22
23 In some cases, it is desirable not to parse an input source at once, but to feed
24 chunks of the document as they get available. Note that the reader will normally
25 not read the entire file, but read it in chunks as well; still :meth:`parse`
26 won't return until the entire document is processed. So these interfaces should
27 be used if the blocking behaviour of :meth:`parse` is not desirable.
28
29 When the parser is instantiated it is ready to begin accepting data from the
30 feed method immediately. After parsing has been finished with a call to close
31 the reset method must be called to make the parser ready to accept new data,
32 either from feed or using the parse method.
33
34 Note that these methods must *not* be called during parsing, that is, after
35 parse has been called and before it returns.
36
37 By default, the class also implements the parse method of the XMLReader
38 interface using the feed, close and reset methods of the IncrementalParser
39 interface as a convenience to SAX 2.0 driver writers.
40
41
42.. class:: Locator()
43
44 Interface for associating a SAX event with a document location. A locator object
45 will return valid results only during calls to DocumentHandler methods; at any
46 other time, the results are unpredictable. If information is not available,
47 methods may return ``None``.
48
49
Georg Brandl7f01a132009-09-16 15:58:14 +000050.. class:: InputSource(system_id=None)
Georg Brandl116aa622007-08-15 14:28:22 +000051
52 Encapsulation of the information needed by the :class:`XMLReader` to read
53 entities.
54
55 This class may include information about the public identifier, system
56 identifier, byte stream (possibly with character encoding information) and/or
57 the character stream of an entity.
58
59 Applications will create objects of this class for use in the
60 :meth:`XMLReader.parse` method and for returning from
61 EntityResolver.resolveEntity.
62
63 An :class:`InputSource` belongs to the application, the :class:`XMLReader` is
64 not allowed to modify :class:`InputSource` objects passed to it from the
65 application, although it may make copies and modify those.
66
67
68.. class:: AttributesImpl(attrs)
69
70 This is an implementation of the :class:`Attributes` interface (see section
71 :ref:`attributes-objects`). This is a dictionary-like object which
72 represents the element attributes in a :meth:`startElement` call. In addition
73 to the most useful dictionary operations, it supports a number of other
74 methods as described by the interface. Objects of this class should be
75 instantiated by readers; *attrs* must be a dictionary-like object containing
76 a mapping from attribute names to attribute values.
77
78
79.. class:: AttributesNSImpl(attrs, qnames)
80
81 Namespace-aware variant of :class:`AttributesImpl`, which will be passed to
82 :meth:`startElementNS`. It is derived from :class:`AttributesImpl`, but
83 understands attribute names as two-tuples of *namespaceURI* and
84 *localname*. In addition, it provides a number of methods expecting qualified
85 names as they appear in the original document. This class implements the
86 :class:`AttributesNS` interface (see section :ref:`attributes-ns-objects`).
87
88
89.. _xmlreader-objects:
90
91XMLReader Objects
92-----------------
93
94The :class:`XMLReader` interface supports the following methods:
95
96
97.. method:: XMLReader.parse(source)
98
99 Process an input source, producing SAX events. The *source* object can be a
100 system identifier (a string identifying the input source -- typically a file
101 name or an URL), a file-like object, or an :class:`InputSource` object. When
102 :meth:`parse` returns, the input is completely processed, and the parser object
103 can be discarded or reset. As a limitation, the current implementation only
104 accepts byte streams; processing of character streams is for further study.
105
106
107.. method:: XMLReader.getContentHandler()
108
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300109 Return the current :class:`~xml.sax.handler.ContentHandler`.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111
112.. method:: XMLReader.setContentHandler(handler)
113
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300114 Set the current :class:`~xml.sax.handler.ContentHandler`. If no
115 :class:`~xml.sax.handler.ContentHandler` is set, content events will be
116 discarded.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
119.. method:: XMLReader.getDTDHandler()
120
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300121 Return the current :class:`~xml.sax.handler.DTDHandler`.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
124.. method:: XMLReader.setDTDHandler(handler)
125
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300126 Set the current :class:`~xml.sax.handler.DTDHandler`. If no
127 :class:`~xml.sax.handler.DTDHandler` is set, DTD
Georg Brandl116aa622007-08-15 14:28:22 +0000128 events will be discarded.
129
130
131.. method:: XMLReader.getEntityResolver()
132
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300133 Return the current :class:`~xml.sax.handler.EntityResolver`.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
136.. method:: XMLReader.setEntityResolver(handler)
137
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300138 Set the current :class:`~xml.sax.handler.EntityResolver`. If no
139 :class:`~xml.sax.handler.EntityResolver` is set,
Georg Brandl116aa622007-08-15 14:28:22 +0000140 attempts to resolve an external entity will result in opening the system
141 identifier for the entity, and fail if it is not available.
142
143
144.. method:: XMLReader.getErrorHandler()
145
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300146 Return the current :class:`~xml.sax.handler.ErrorHandler`.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148
149.. method:: XMLReader.setErrorHandler(handler)
150
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300151 Set the current error handler. If no :class:`~xml.sax.handler.ErrorHandler`
152 is set, errors will be raised as exceptions, and warnings will be printed.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154
155.. method:: XMLReader.setLocale(locale)
156
157 Allow an application to set the locale for errors and warnings.
158
159 SAX parsers are not required to provide localization for errors and warnings; if
Georg Brandl7cb13192010-08-03 12:06:29 +0000160 they cannot support the requested locale, however, they must raise a SAX
Georg Brandl116aa622007-08-15 14:28:22 +0000161 exception. Applications may request a locale change in the middle of a parse.
162
163
164.. method:: XMLReader.getFeature(featurename)
165
166 Return the current setting for feature *featurename*. If the feature is not
167 recognized, :exc:`SAXNotRecognizedException` is raised. The well-known
168 featurenames are listed in the module :mod:`xml.sax.handler`.
169
170
171.. method:: XMLReader.setFeature(featurename, value)
172
173 Set the *featurename* to *value*. If the feature is not recognized,
174 :exc:`SAXNotRecognizedException` is raised. If the feature or its setting is not
175 supported by the parser, *SAXNotSupportedException* is raised.
176
177
178.. method:: XMLReader.getProperty(propertyname)
179
180 Return the current setting for property *propertyname*. If the property is not
181 recognized, a :exc:`SAXNotRecognizedException` is raised. The well-known
182 propertynames are listed in the module :mod:`xml.sax.handler`.
183
184
185.. method:: XMLReader.setProperty(propertyname, value)
186
187 Set the *propertyname* to *value*. If the property is not recognized,
188 :exc:`SAXNotRecognizedException` is raised. If the property or its setting is
189 not supported by the parser, *SAXNotSupportedException* is raised.
190
191
192.. _incremental-parser-objects:
193
194IncrementalParser Objects
195-------------------------
196
197Instances of :class:`IncrementalParser` offer the following additional methods:
198
199
200.. method:: IncrementalParser.feed(data)
201
202 Process a chunk of *data*.
203
204
205.. method:: IncrementalParser.close()
206
207 Assume the end of the document. That will check well-formedness conditions that
208 can be checked only at the end, invoke handlers, and may clean up resources
209 allocated during parsing.
210
211
212.. method:: IncrementalParser.reset()
213
214 This method is called after close has been called to reset the parser so that it
215 is ready to parse new documents. The results of calling parse or feed after
216 close without calling reset are undefined.
217
218
219.. _locator-objects:
220
221Locator Objects
222---------------
223
224Instances of :class:`Locator` provide these methods:
225
226
227.. method:: Locator.getColumnNumber()
228
229 Return the column number where the current event ends.
230
231
232.. method:: Locator.getLineNumber()
233
234 Return the line number where the current event ends.
235
236
237.. method:: Locator.getPublicId()
238
239 Return the public identifier for the current event.
240
241
242.. method:: Locator.getSystemId()
243
244 Return the system identifier for the current event.
245
246
247.. _input-source-objects:
248
249InputSource Objects
250-------------------
251
252
253.. method:: InputSource.setPublicId(id)
254
255 Sets the public identifier of this :class:`InputSource`.
256
257
258.. method:: InputSource.getPublicId()
259
260 Returns the public identifier of this :class:`InputSource`.
261
262
263.. method:: InputSource.setSystemId(id)
264
265 Sets the system identifier of this :class:`InputSource`.
266
267
268.. method:: InputSource.getSystemId()
269
270 Returns the system identifier of this :class:`InputSource`.
271
272
273.. method:: InputSource.setEncoding(encoding)
274
275 Sets the character encoding of this :class:`InputSource`.
276
277 The encoding must be a string acceptable for an XML encoding declaration (see
278 section 4.3.3 of the XML recommendation).
279
280 The encoding attribute of the :class:`InputSource` is ignored if the
281 :class:`InputSource` also contains a character stream.
282
283
284.. method:: InputSource.getEncoding()
285
286 Get the character encoding of this InputSource.
287
288
289.. method:: InputSource.setByteStream(bytefile)
290
291 Set the byte stream (a Python file-like object which does not perform
292 byte-to-character conversion) for this input source.
293
294 The SAX parser will ignore this if there is also a character stream specified,
295 but it will use a byte stream in preference to opening a URI connection itself.
296
297 If the application knows the character encoding of the byte stream, it should
298 set it with the setEncoding method.
299
300
301.. method:: InputSource.getByteStream()
302
303 Get the byte stream for this input source.
304
305 The getEncoding method will return the character encoding for this byte stream,
306 or None if unknown.
307
308
309.. method:: InputSource.setCharacterStream(charfile)
310
311 Set the character stream for this input source. (The stream must be a Python 1.6
Georg Brandlf6945182008-02-01 11:56:49 +0000312 Unicode-wrapped file-like that performs conversion to strings.)
Georg Brandl116aa622007-08-15 14:28:22 +0000313
314 If there is a character stream specified, the SAX parser will ignore any byte
315 stream and will not attempt to open a URI connection to the system identifier.
316
317
318.. method:: InputSource.getCharacterStream()
319
320 Get the character stream for this input source.
321
322
323.. _attributes-objects:
324
325The :class:`Attributes` Interface
326---------------------------------
327
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300328:class:`Attributes` objects implement a portion of the :term:`mapping protocol
329<mapping>`, including the methods :meth:`~collections.abc.Mapping.copy`,
330:meth:`~collections.abc.Mapping.get`, :meth:`~object.__contains__`,
331:meth:`~collections.abc.Mapping.items`, :meth:`~collections.abc.Mapping.keys`,
332and :meth:`~collections.abc.Mapping.values`. The following methods
Collin Winterf6b81212007-09-10 00:03:41 +0000333are also provided:
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335
336.. method:: Attributes.getLength()
337
338 Return the number of attributes.
339
340
341.. method:: Attributes.getNames()
342
343 Return the names of the attributes.
344
345
346.. method:: Attributes.getType(name)
347
348 Returns the type of the attribute *name*, which is normally ``'CDATA'``.
349
350
351.. method:: Attributes.getValue(name)
352
353 Return the value of attribute *name*.
354
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000355.. getValueByQName, getNameByQName, getQNameByName, getQNames available
356.. here already, but documented only for derived class.
Georg Brandl116aa622007-08-15 14:28:22 +0000357
358
359.. _attributes-ns-objects:
360
361The :class:`AttributesNS` Interface
362-----------------------------------
363
364This interface is a subtype of the :class:`Attributes` interface (see section
365:ref:`attributes-objects`). All methods supported by that interface are also
366available on :class:`AttributesNS` objects.
367
368The following methods are also available:
369
370
371.. method:: AttributesNS.getValueByQName(name)
372
373 Return the value for a qualified name.
374
375
376.. method:: AttributesNS.getNameByQName(name)
377
378 Return the ``(namespace, localname)`` pair for a qualified *name*.
379
380
381.. method:: AttributesNS.getQNameByName(name)
382
383 Return the qualified name for a ``(namespace, localname)`` pair.
384
385
386.. method:: AttributesNS.getQNames()
387
388 Return the qualified names of all attributes.
389