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