blob: 04ca27e813c8dbcd4d0467c2eb68f6e6a42f0177 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`xmllib` --- A parser for XML documents
3============================================
4
5.. module:: xmllib
6 :synopsis: A parser for XML documents.
7.. moduleauthor:: Sjoerd Mullender <Sjoerd.Mullender@cwi.nl>
8.. sectionauthor:: Sjoerd Mullender <Sjoerd.Mullender@cwi.nl>
9
10
11.. index::
12 single: XML
13 single: Extensible Markup Language
14
15.. deprecated:: 2.0
16 Use :mod:`xml.sax` instead. The newer XML package includes full support for XML
17 1.0.
18
19.. versionchanged:: 1.5.2
20 Added namespace support.
21
22This module defines a class :class:`XMLParser` which serves as the basis for
23parsing text files formatted in XML (Extensible Markup Language).
24
25
26.. class:: XMLParser()
27
28 The :class:`XMLParser` class must be instantiated without arguments. [#]_
29
30This class provides the following interface methods and instance variables:
31
32
33.. attribute:: XMLParser.attributes
34
35 A mapping of element names to mappings. The latter mapping maps attribute names
36 that are valid for the element to the default value of the attribute, or if
37 there is no default to ``None``. The default value is the empty dictionary.
38 This variable is meant to be overridden, not extended since the default is
39 shared by all instances of :class:`XMLParser`.
40
41
42.. attribute:: XMLParser.elements
43
44 A mapping of element names to tuples. The tuples contain a function for
45 handling the start and end tag respectively of the element, or ``None`` if the
46 method :meth:`unknown_starttag` or :meth:`unknown_endtag` is to be called. The
47 default value is the empty dictionary. This variable is meant to be overridden,
48 not extended since the default is shared by all instances of :class:`XMLParser`.
49
50
51.. attribute:: XMLParser.entitydefs
52
53 A mapping of entitynames to their values. The default value contains
54 definitions for ``'lt'``, ``'gt'``, ``'amp'``, ``'quot'``, and ``'apos'``.
55
56
57.. method:: XMLParser.reset()
58
59 Reset the instance. Loses all unprocessed data. This is called implicitly at
60 the instantiation time.
61
62
63.. method:: XMLParser.setnomoretags()
64
65 Stop processing tags. Treat all following input as literal input (CDATA).
66
67
68.. method:: XMLParser.setliteral()
69
70 Enter literal mode (CDATA mode). This mode is automatically exited when the
71 close tag matching the last unclosed open tag is encountered.
72
73
74.. method:: XMLParser.feed(data)
75
76 Feed some text to the parser. It is processed insofar as it consists of
77 complete tags; incomplete data is buffered until more data is fed or
78 :meth:`close` is called.
79
80
81.. method:: XMLParser.close()
82
83 Force processing of all buffered data as if it were followed by an end-of-file
84 mark. This method may be redefined by a derived class to define additional
85 processing at the end of the input, but the redefined version should always call
86 :meth:`close`.
87
88
89.. method:: XMLParser.translate_references(data)
90
91 Translate all entity and character references in *data* and return the
92 translated string.
93
94
95.. method:: XMLParser.getnamespace()
96
97 Return a mapping of namespace abbreviations to namespace URIs that are currently
98 in effect.
99
100
101.. method:: XMLParser.handle_xml(encoding, standalone)
102
103 This method is called when the ``<?xml ...?>`` tag is processed. The arguments
104 are the values of the encoding and standalone attributes in the tag. Both
105 encoding and standalone are optional. The values passed to :meth:`handle_xml`
106 default to ``None`` and the string ``'no'`` respectively.
107
108
109.. method:: XMLParser.handle_doctype(tag, pubid, syslit, data)
110
111 .. index::
112 single: DOCTYPE declaration
113 single: Formal Public Identifier
114
115 This method is called when the ``<!DOCTYPE...>`` declaration is processed. The
116 arguments are the tag name of the root element, the Formal Public Identifier (or
117 ``None`` if not specified), the system identifier, and the uninterpreted
118 contents of the internal DTD subset as a string (or ``None`` if not present).
119
120
121.. method:: XMLParser.handle_starttag(tag, method, attributes)
122
123 This method is called to handle start tags for which a start tag handler is
124 defined in the instance variable :attr:`elements`. The *tag* argument is the
125 name of the tag, and the *method* argument is the function (method) which should
126 be used to support semantic interpretation of the start tag. The *attributes*
127 argument is a dictionary of attributes, the key being the *name* and the value
128 being the *value* of the attribute found inside the tag's ``<>`` brackets.
129 Character and entity references in the *value* have been interpreted. For
130 instance, for the start tag ``<A HREF="http://www.cwi.nl/">``, this method would
131 be called as ``handle_starttag('A', self.elements['A'][0], {'HREF':
132 'http://www.cwi.nl/'})``. The base implementation simply calls *method* with
133 *attributes* as the only argument.
134
135
136.. method:: XMLParser.handle_endtag(tag, method)
137
138 This method is called to handle endtags for which an end tag handler is defined
139 in the instance variable :attr:`elements`. The *tag* argument is the name of
140 the tag, and the *method* argument is the function (method) which should be used
141 to support semantic interpretation of the end tag. For instance, for the endtag
142 ``</A>``, this method would be called as ``handle_endtag('A',
143 self.elements['A'][1])``. The base implementation simply calls *method*.
144
145
146.. method:: XMLParser.handle_data(data)
147
148 This method is called to process arbitrary data. It is intended to be
149 overridden by a derived class; the base class implementation does nothing.
150
151
152.. method:: XMLParser.handle_charref(ref)
153
154 This method is called to process a character reference of the form ``&#ref;``.
155 *ref* can either be a decimal number, or a hexadecimal number when preceded by
156 an ``'x'``. In the base implementation, *ref* must be a number in the range
157 0-255. It translates the character to ASCII and calls the method
158 :meth:`handle_data` with the character as argument. If *ref* is invalid or out
159 of range, the method ``unknown_charref(ref)`` is called to handle the error. A
160 subclass must override this method to provide support for character references
161 outside of the ASCII range.
162
163
164.. method:: XMLParser.handle_comment(comment)
165
166 This method is called when a comment is encountered. The *comment* argument is
167 a string containing the text between the ``<!--`` and ``-->`` delimiters, but
168 not the delimiters themselves. For example, the comment ``<!--text-->`` will
169 cause this method to be called with the argument ``'text'``. The default method
170 does nothing.
171
172
173.. method:: XMLParser.handle_cdata(data)
174
175 This method is called when a CDATA element is encountered. The *data* argument
176 is a string containing the text between the ``<![CDATA[`` and ``]]>``
177 delimiters, but not the delimiters themselves. For example, the entity
178 ``<![CDATA[text]]>`` will cause this method to be called with the argument
179 ``'text'``. The default method does nothing, and is intended to be overridden.
180
181
182.. method:: XMLParser.handle_proc(name, data)
183
184 This method is called when a processing instruction (PI) is encountered. The
185 *name* is the PI target, and the *data* argument is a string containing the text
186 between the PI target and the closing delimiter, but not the delimiter itself.
187 For example, the instruction ``<?XML text?>`` will cause this method to be
188 called with the arguments ``'XML'`` and ``'text'``. The default method does
189 nothing. Note that if a document starts with ``<?xml ..?>``, :meth:`handle_xml`
190 is called to handle it.
191
192
193.. method:: XMLParser.handle_special(data)
194
195 .. index:: single: ENTITY declaration
196
197 This method is called when a declaration is encountered. The *data* argument is
198 a string containing the text between the ``<!`` and ``>`` delimiters, but not
199 the delimiters themselves. For example, the entity declaration ``<!ENTITY
200 text>`` will cause this method to be called with the argument ``'ENTITY text'``.
201 The default method does nothing. Note that ``<!DOCTYPE ...>`` is handled
202 separately if it is located at the start of the document.
203
204
205.. method:: XMLParser.syntax_error(message)
206
207 This method is called when a syntax error is encountered. The *message* is a
208 description of what was wrong. The default method raises a :exc:`RuntimeError`
209 exception. If this method is overridden, it is permissible for it to return.
210 This method is only called when the error can be recovered from. Unrecoverable
211 errors raise a :exc:`RuntimeError` without first calling :meth:`syntax_error`.
212
213
214.. method:: XMLParser.unknown_starttag(tag, attributes)
215
216 This method is called to process an unknown start tag. It is intended to be
217 overridden by a derived class; the base class implementation does nothing.
218
219
220.. method:: XMLParser.unknown_endtag(tag)
221
222 This method is called to process an unknown end tag. It is intended to be
223 overridden by a derived class; the base class implementation does nothing.
224
225
226.. method:: XMLParser.unknown_charref(ref)
227
228 This method is called to process unresolvable numeric character references. It
229 is intended to be overridden by a derived class; the base class implementation
230 does nothing.
231
232
233.. method:: XMLParser.unknown_entityref(ref)
234
235 This method is called to process an unknown entity reference. It is intended to
236 be overridden by a derived class; the base class implementation calls
237 :meth:`syntax_error` to signal an error.
238
239
240.. seealso::
241
242 `Extensible Markup Language (XML) 1.0 <http://www.w3.org/TR/REC-xml>`_
243 The XML specification, published by the World Wide Web Consortium (W3C), defines
244 the syntax and processor requirements for XML. References to additional
245 material on XML, including translations of the specification, are available at
246 http://www.w3.org/XML/.
247
248 `Python and XML Processing <http://www.python.org/topics/xml/>`_
249 The Python XML Topic Guide provides a great deal of information on using XML
250 from Python and links to other sources of information on XML.
251
252 `SIG for XML Processing in Python <http://www.python.org/sigs/xml-sig/>`_
253 The Python XML Special Interest Group is developing substantial support for
254 processing XML from Python.
255
256
257.. _xml-namespace:
258
259XML Namespaces
260--------------
261
262.. index:: pair: XML; namespaces
263
264This module has support for XML namespaces as defined in the XML Namespaces
265proposed recommendation.
266
267Tag and attribute names that are defined in an XML namespace are handled as if
268the name of the tag or element consisted of the namespace (the URL that defines
269the namespace) followed by a space and the name of the tag or attribute. For
270instance, the tag ``<html xmlns='http://www.w3.org/TR/REC-html40'>`` is treated
271as if the tag name was ``'http://www.w3.org/TR/REC-html40 html'``, and the tag
272``<html:a href='http://frob.com'>`` inside the above mentioned element is
273treated as if the tag name were ``'http://www.w3.org/TR/REC-html40 a'`` and the
274attribute name as if it were ``'http://www.w3.org/TR/REC-html40 href'``.
275
276An older draft of the XML Namespaces proposal is also recognized, but triggers a
277warning.
278
279
280.. seealso::
281
282 `Namespaces in XML <http://www.w3.org/TR/REC-xml-names/>`_
283 This World Wide Web Consortium recommendation describes the proper syntax and
284 processing requirements for namespaces in XML.
285
286.. rubric:: Footnotes
287
288.. [#] Actually, a number of keyword arguments are recognized which influence the
289 parser to accept certain non-standard constructs. The following keyword
290 arguments are currently recognized. The defaults for all of these is ``0``
291 (false) except for the last one for which the default is ``1`` (true).
292 *accept_unquoted_attributes* (accept certain attribute values without requiring
293 quotes), *accept_missing_endtag_name* (accept end tags that look like ``</>``),
294 *map_case* (map upper case to lower case in tags and attributes), *accept_utf8*
295 (allow UTF-8 characters in input; this is required according to the XML
296 standard, but Python does not as yet deal properly with these characters, so
297 this is not the default), *translate_attribute_references* (don't attempt to
298 translate character and entity references in attribute values).
299