blob: 31ca260f7ce7f2fd28eacee9752df9a9b213460f [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
Serhiy Storchaka61de0872015-04-02 21:00:13 +0300103 can be discarded or reset.
104
105 .. versionchanged:: 3.5
106 Added support of character streams.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
109.. method:: XMLReader.getContentHandler()
110
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300111 Return the current :class:`~xml.sax.handler.ContentHandler`.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
114.. method:: XMLReader.setContentHandler(handler)
115
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300116 Set the current :class:`~xml.sax.handler.ContentHandler`. If no
117 :class:`~xml.sax.handler.ContentHandler` is set, content events will be
118 discarded.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120
121.. method:: XMLReader.getDTDHandler()
122
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300123 Return the current :class:`~xml.sax.handler.DTDHandler`.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. method:: XMLReader.setDTDHandler(handler)
127
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300128 Set the current :class:`~xml.sax.handler.DTDHandler`. If no
129 :class:`~xml.sax.handler.DTDHandler` is set, DTD
Georg Brandl116aa622007-08-15 14:28:22 +0000130 events will be discarded.
131
132
133.. method:: XMLReader.getEntityResolver()
134
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300135 Return the current :class:`~xml.sax.handler.EntityResolver`.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137
138.. method:: XMLReader.setEntityResolver(handler)
139
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300140 Set the current :class:`~xml.sax.handler.EntityResolver`. If no
141 :class:`~xml.sax.handler.EntityResolver` is set,
Georg Brandl116aa622007-08-15 14:28:22 +0000142 attempts to resolve an external entity will result in opening the system
143 identifier for the entity, and fail if it is not available.
144
145
146.. method:: XMLReader.getErrorHandler()
147
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300148 Return the current :class:`~xml.sax.handler.ErrorHandler`.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150
151.. method:: XMLReader.setErrorHandler(handler)
152
Serhiy Storchaka15e65902013-08-29 10:28:44 +0300153 Set the current error handler. If no :class:`~xml.sax.handler.ErrorHandler`
154 is set, errors will be raised as exceptions, and warnings will be printed.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156
157.. method:: XMLReader.setLocale(locale)
158
159 Allow an application to set the locale for errors and warnings.
160
161 SAX parsers are not required to provide localization for errors and warnings; if
Georg Brandl7cb13192010-08-03 12:06:29 +0000162 they cannot support the requested locale, however, they must raise a SAX
Georg Brandl116aa622007-08-15 14:28:22 +0000163 exception. Applications may request a locale change in the middle of a parse.
164
165
166.. method:: XMLReader.getFeature(featurename)
167
168 Return the current setting for feature *featurename*. If the feature is not
169 recognized, :exc:`SAXNotRecognizedException` is raised. The well-known
170 featurenames are listed in the module :mod:`xml.sax.handler`.
171
172
173.. method:: XMLReader.setFeature(featurename, value)
174
175 Set the *featurename* to *value*. If the feature is not recognized,
176 :exc:`SAXNotRecognizedException` is raised. If the feature or its setting is not
177 supported by the parser, *SAXNotSupportedException* is raised.
178
179
180.. method:: XMLReader.getProperty(propertyname)
181
182 Return the current setting for property *propertyname*. If the property is not
183 recognized, a :exc:`SAXNotRecognizedException` is raised. The well-known
184 propertynames are listed in the module :mod:`xml.sax.handler`.
185
186
187.. method:: XMLReader.setProperty(propertyname, value)
188
189 Set the *propertyname* to *value*. If the property is not recognized,
190 :exc:`SAXNotRecognizedException` is raised. If the property or its setting is
191 not supported by the parser, *SAXNotSupportedException* is raised.
192
193
194.. _incremental-parser-objects:
195
196IncrementalParser Objects
197-------------------------
198
199Instances of :class:`IncrementalParser` offer the following additional methods:
200
201
202.. method:: IncrementalParser.feed(data)
203
204 Process a chunk of *data*.
205
206
207.. method:: IncrementalParser.close()
208
209 Assume the end of the document. That will check well-formedness conditions that
210 can be checked only at the end, invoke handlers, and may clean up resources
211 allocated during parsing.
212
213
214.. method:: IncrementalParser.reset()
215
216 This method is called after close has been called to reset the parser so that it
217 is ready to parse new documents. The results of calling parse or feed after
218 close without calling reset are undefined.
219
220
221.. _locator-objects:
222
223Locator Objects
224---------------
225
226Instances of :class:`Locator` provide these methods:
227
228
229.. method:: Locator.getColumnNumber()
230
231 Return the column number where the current event ends.
232
233
234.. method:: Locator.getLineNumber()
235
236 Return the line number where the current event ends.
237
238
239.. method:: Locator.getPublicId()
240
241 Return the public identifier for the current event.
242
243
244.. method:: Locator.getSystemId()
245
246 Return the system identifier for the current event.
247
248
249.. _input-source-objects:
250
251InputSource Objects
252-------------------
253
254
255.. method:: InputSource.setPublicId(id)
256
257 Sets the public identifier of this :class:`InputSource`.
258
259
260.. method:: InputSource.getPublicId()
261
262 Returns the public identifier of this :class:`InputSource`.
263
264
265.. method:: InputSource.setSystemId(id)
266
267 Sets the system identifier of this :class:`InputSource`.
268
269
270.. method:: InputSource.getSystemId()
271
272 Returns the system identifier of this :class:`InputSource`.
273
274
275.. method:: InputSource.setEncoding(encoding)
276
277 Sets the character encoding of this :class:`InputSource`.
278
279 The encoding must be a string acceptable for an XML encoding declaration (see
280 section 4.3.3 of the XML recommendation).
281
282 The encoding attribute of the :class:`InputSource` is ignored if the
283 :class:`InputSource` also contains a character stream.
284
285
286.. method:: InputSource.getEncoding()
287
288 Get the character encoding of this InputSource.
289
290
291.. method:: InputSource.setByteStream(bytefile)
292
Serhiy Storchaka61de0872015-04-02 21:00:13 +0300293 Set the byte stream (a :term:`binary file`) for this input source.
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295 The SAX parser will ignore this if there is also a character stream specified,
296 but it will use a byte stream in preference to opening a URI connection itself.
297
298 If the application knows the character encoding of the byte stream, it should
299 set it with the setEncoding method.
300
301
302.. method:: InputSource.getByteStream()
303
304 Get the byte stream for this input source.
305
306 The getEncoding method will return the character encoding for this byte stream,
307 or None if unknown.
308
309
310.. method:: InputSource.setCharacterStream(charfile)
311
Serhiy Storchaka61de0872015-04-02 21:00:13 +0300312 Set the character stream (a :term:`text file`) for this input source.
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