blob: c3f78f14f87682967eb78d4419cd583bb3255b25 [file] [log] [blame]
Fred Drake7fbc85c2000-09-23 04:47:56 +00001\section{\module{xml.parsers.expat} ---
Fred Drakeefffe8e2000-10-29 05:10:30 +00002 Fast XML parsing using Expat}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +00003
Fred Drake5ed1dac2001-02-08 15:40:33 +00004% Markup notes:
5%
6% Many of the attributes of the XMLParser objects are callbacks.
7% Since signature information must be presented, these are described
8% using the methoddesc environment. Since they are attributes which
9% are set by client code, in-text references to these attributes
10% should be marked using the \member macro and should not include the
11% parentheses used when marking functions and methods.
12
Fred Drake7fbc85c2000-09-23 04:47:56 +000013\declaremodule{standard}{xml.parsers.expat}
14\modulesynopsis{An interface to the Expat non-validating XML parser.}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000015\moduleauthor{Paul Prescod}{paul@prescod.net}
16\sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
17
Fred Drake7fbc85c2000-09-23 04:47:56 +000018\versionadded{2.0}
19
Fred Drakeefffe8e2000-10-29 05:10:30 +000020The \module{xml.parsers.expat} module is a Python interface to the
21Expat\index{Expat} non-validating XML parser.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000022The module provides a single extension type, \class{xmlparser}, that
23represents the current state of an XML parser. After an
24\class{xmlparser} object has been created, various attributes of the object
25can be set to handler functions. When an XML document is then fed to
26the parser, the handler functions are called for the character data
27and markup in the XML document.
Fred Drake7fbc85c2000-09-23 04:47:56 +000028
29This module uses the \module{pyexpat}\refbimodindex{pyexpat} module to
30provide access to the Expat parser. Direct use of the
31\module{pyexpat} module is deprecated.
Fred Drakeefffe8e2000-10-29 05:10:30 +000032
33This module provides one exception and one type object:
34
Fred Drake1d8ad2b2001-02-14 18:54:32 +000035\begin{excdesc}{ExpatError}
Fred Drakee0af35e2001-09-20 20:43:28 +000036 The exception raised when Expat reports an error. See section
37 \ref{expaterror-objects}, ``ExpatError Exceptions,'' for more
38 information on interpreting Expat errors.
Fred Drakeefffe8e2000-10-29 05:10:30 +000039\end{excdesc}
40
Fred Drake1d8ad2b2001-02-14 18:54:32 +000041\begin{excdesc}{error}
42 Alias for \exception{ExpatError}.
43\end{excdesc}
44
Fred Drakeefffe8e2000-10-29 05:10:30 +000045\begin{datadesc}{XMLParserType}
46 The type of the return values from the \function{ParserCreate()}
47 function.
48\end{datadesc}
49
50
Fred Drake7fbc85c2000-09-23 04:47:56 +000051The \module{xml.parsers.expat} module contains two functions:
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000052
53\begin{funcdesc}{ErrorString}{errno}
54Returns an explanatory string for a given error number \var{errno}.
55\end{funcdesc}
56
Fred Drakeefffe8e2000-10-29 05:10:30 +000057\begin{funcdesc}{ParserCreate}{\optional{encoding\optional{,
58 namespace_separator}}}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000059Creates and returns a new \class{xmlparser} object.
60\var{encoding}, if specified, must be a string naming the encoding
61used by the XML data. Expat doesn't support as many encodings as
62Python does, and its repertoire of encodings can't be extended; it
Fred Drake5ed1dac2001-02-08 15:40:33 +000063supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. If
64\var{encoding} is given it will override the implicit or explicit
65encoding of the document.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000066
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000067Expat can optionally do XML namespace processing for you, enabled by
Fred Drakeefffe8e2000-10-29 05:10:30 +000068providing a value for \var{namespace_separator}. The value must be a
69one-character string; a \exception{ValueError} will be raised if the
70string has an illegal length (\code{None} is considered the same as
71omission). When namespace processing is enabled, element type names
72and attribute names that belong to a namespace will be expanded. The
73element name passed to the element handlers
Fred Drake5ed1dac2001-02-08 15:40:33 +000074\member{StartElementHandler} and \member{EndElementHandler}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000075will be the concatenation of the namespace URI, the namespace
76separator character, and the local part of the name. If the namespace
Fred Drakeefffe8e2000-10-29 05:10:30 +000077separator is a zero byte (\code{chr(0)}) then the namespace URI and
Fred Drake5ed1dac2001-02-08 15:40:33 +000078the local part will be concatenated without any separator.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000079
Fred Drake2fef3ab2000-11-28 06:38:22 +000080For example, if \var{namespace_separator} is set to a space character
81(\character{ }) and the following document is parsed:
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000082
83\begin{verbatim}
84<?xml version="1.0"?>
85<root xmlns = "http://default-namespace.org/"
86 xmlns:py = "http://www.python.org/ns/">
87 <py:elem1 />
88 <elem2 xmlns="" />
89</root>
90\end{verbatim}
91
Fred Drake5ed1dac2001-02-08 15:40:33 +000092\member{StartElementHandler} will receive the following strings
Fred Draked79c33a2000-09-25 14:14:30 +000093for each element:
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000094
95\begin{verbatim}
96http://default-namespace.org/ root
97http://www.python.org/ns/ elem1
98elem2
99\end{verbatim}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000100\end{funcdesc}
101
Fred Drakef08cbb12000-12-23 22:19:05 +0000102
103\subsection{XMLParser Objects \label{xmlparser-objects}}
104
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000105\class{xmlparser} objects have the following methods:
106
Fred Drake2fef3ab2000-11-28 06:38:22 +0000107\begin{methoddesc}[xmlparser]{Parse}{data\optional{, isfinal}}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000108Parses the contents of the string \var{data}, calling the appropriate
109handler functions to process the parsed data. \var{isfinal} must be
Fred Drakef08cbb12000-12-23 22:19:05 +0000110true on the final call to this method. \var{data} can be the empty
Fred Drakec05cbb02000-07-05 02:03:34 +0000111string at any time.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000112\end{methoddesc}
113
Fred Drakeefffe8e2000-10-29 05:10:30 +0000114\begin{methoddesc}[xmlparser]{ParseFile}{file}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000115Parse XML data reading from the object \var{file}. \var{file} only
116needs to provide the \method{read(\var{nbytes})} method, returning the
117empty string when there's no more data.
118\end{methoddesc}
119
Fred Drakeefffe8e2000-10-29 05:10:30 +0000120\begin{methoddesc}[xmlparser]{SetBase}{base}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000121Sets the base to be used for resolving relative URIs in system
122identifiers in declarations. Resolving relative identifiers is left
123to the application: this value will be passed through as the
124\var{base} argument to the \function{ExternalEntityRefHandler},
125\function{NotationDeclHandler}, and
126\function{UnparsedEntityDeclHandler} functions.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000127\end{methoddesc}
128
Fred Drakeefffe8e2000-10-29 05:10:30 +0000129\begin{methoddesc}[xmlparser]{GetBase}{}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000130Returns a string containing the base set by a previous call to
131\method{SetBase()}, or \code{None} if
132\method{SetBase()} hasn't been called.
133\end{methoddesc}
134
Fred Drake1d8ad2b2001-02-14 18:54:32 +0000135\begin{methoddesc}[xmlparser]{GetInputContext}{}
136Returns the input data that generated the current event as a string.
137The data is in the encoding of the entity which contains the text.
138When called while an event handler is not active, the return value is
139\code{None}.
140\versionadded{2.1}
141\end{methoddesc}
142
Fred Drakef08cbb12000-12-23 22:19:05 +0000143\begin{methoddesc}[xmlparser]{ExternalEntityParserCreate}{context\optional{,
144 encoding}}
145Create a ``child'' parser which can be used to parse an external
146parsed entity referred to by content parsed by the parent parser. The
Fred Drakeb162d182001-01-04 05:48:08 +0000147\var{context} parameter should be the string passed to the
Fred Drakef08cbb12000-12-23 22:19:05 +0000148\method{ExternalEntityRefHandler()} handler function, described below.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000149The child parser is created with the \member{ordered_attributes},
150\member{returns_unicode} and \member{specified_attributes} set to the
151values of this parser.
Fred Drakef08cbb12000-12-23 22:19:05 +0000152\end{methoddesc}
153
Fred Drakeefffe8e2000-10-29 05:10:30 +0000154
Fred Draked79c33a2000-09-25 14:14:30 +0000155\class{xmlparser} objects have the following attributes:
Andrew M. Kuchling0690c862000-08-17 23:15:21 +0000156
Fred Drake5ed1dac2001-02-08 15:40:33 +0000157\begin{memberdesc}[xmlparser]{ordered_attributes}
158Setting this attribute to a non-zero integer causes the attributes to
159be reported as a list rather than a dictionary. The attributes are
160presented in the order found in the document text. For each
161attribute, two list entries are presented: the attribute name and the
162attribute value. (Older versions of this module also used this
163format.) By default, this attribute is false; it may be changed at
164any time.
165\versionadded{2.1}
166\end{memberdesc}
167
Fred Drakeefffe8e2000-10-29 05:10:30 +0000168\begin{memberdesc}[xmlparser]{returns_unicode}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000169If this attribute is set to a non-zero integer, the handler functions
170will be passed Unicode strings. If \member{returns_unicode} is 0,
1718-bit strings containing UTF-8 encoded data will be passed to the
172handlers.
Fred Drakeb62966c2000-12-07 00:00:21 +0000173\versionchanged[Can be changed at any time to affect the result
Fred Drakee0af35e2001-09-20 20:43:28 +0000174 type]{1.6}
Fred Drakeefffe8e2000-10-29 05:10:30 +0000175\end{memberdesc}
Andrew M. Kuchling0690c862000-08-17 23:15:21 +0000176
Fred Drake5ed1dac2001-02-08 15:40:33 +0000177\begin{memberdesc}[xmlparser]{specified_attributes}
178If set to a non-zero integer, the parser will report only those
179attributes which were specified in the document instance and not those
180which were derived from attribute declarations. Applications which
181set this need to be especially careful to use what additional
182information is available from the declarations as needed to comply
183with the standards for the behavior of XML processors. By default,
184this attribute is false; it may be changed at any time.
185\versionadded{2.1}
186\end{memberdesc}
187
Andrew M. Kuchling0690c862000-08-17 23:15:21 +0000188The following attributes contain values relating to the most recent
189error encountered by an \class{xmlparser} object, and will only have
190correct values once a call to \method{Parse()} or \method{ParseFile()}
Fred Drake523ec572001-02-15 05:37:51 +0000191has raised a \exception{xml.parsers.expat.ExpatError} exception.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000192
Fred Drakeefffe8e2000-10-29 05:10:30 +0000193\begin{memberdesc}[xmlparser]{ErrorByteIndex}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000194Byte index at which an error occurred.
Fred Drakeefffe8e2000-10-29 05:10:30 +0000195\end{memberdesc}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000196
Fred Drakeefffe8e2000-10-29 05:10:30 +0000197\begin{memberdesc}[xmlparser]{ErrorCode}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000198Numeric code specifying the problem. This value can be passed to the
199\function{ErrorString()} function, or compared to one of the constants
Fred Drake523ec572001-02-15 05:37:51 +0000200defined in the \code{errors} object.
Fred Drakeefffe8e2000-10-29 05:10:30 +0000201\end{memberdesc}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000202
Fred Drakeefffe8e2000-10-29 05:10:30 +0000203\begin{memberdesc}[xmlparser]{ErrorColumnNumber}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000204Column number at which an error occurred.
Fred Drakeefffe8e2000-10-29 05:10:30 +0000205\end{memberdesc}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000206
Fred Drakeefffe8e2000-10-29 05:10:30 +0000207\begin{memberdesc}[xmlparser]{ErrorLineNumber}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000208Line number at which an error occurred.
Fred Drakeefffe8e2000-10-29 05:10:30 +0000209\end{memberdesc}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000210
211Here is the list of handlers that can be set. To set a handler on an
Fred Drakec05cbb02000-07-05 02:03:34 +0000212\class{xmlparser} object \var{o}, use
213\code{\var{o}.\var{handlername} = \var{func}}. \var{handlername} must
214be taken from the following list, and \var{func} must be a callable
215object accepting the correct number of arguments. The arguments are
216all strings, unless otherwise stated.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000217
Fred Drake5ed1dac2001-02-08 15:40:33 +0000218\begin{methoddesc}[xmlparser]{XmlDeclHandler}{version, encoding, standalone}
219Called when the XML declaration is parsed. The XML declaration is the
220(optional) declaration of the applicable version of the XML
221recommendation, the encoding of the document text, and an optional
222``standalone'' declaration. \var{version} and \var{encoding} will be
223strings of the type dictated by the \member{returns_unicode}
224attribute, and \var{standalone} will be \code{1} if the document is
225declared standalone, \code{0} if it is declared not to be standalone,
226or \code{-1} if the standalone clause was omitted.
227This is only available with Expat version 1.95.0 or newer.
228\versionadded{2.1}
229\end{methoddesc}
230
231\begin{methoddesc}[xmlparser]{StartDoctypeDeclHandler}{doctypeName,
232 systemId, publicId,
233 has_internal_subset}
234Called when Expat begins parsing the document type declaration
235(\code{<!DOCTYPE \ldots}). The \var{doctypeName} is provided exactly
236as presented. The \var{systemId} and \var{publicId} parameters give
237the system and public identifiers if specified, or \code{None} if
238omitted. \var{has_internal_subset} will be true if the document
239contains and internal document declaration subset.
240This requires Expat version 1.2 or newer.
241\end{methoddesc}
242
243\begin{methoddesc}[xmlparser]{EndDoctypeDeclHandler}{}
244Called when Expat is done parsing the document type delaration.
245This requires Expat version 1.2 or newer.
246\end{methoddesc}
247
248\begin{methoddesc}[xmlparser]{ElementDeclHandler}{name, model}
249Called once for each element type declaration. \var{name} is the name
250of the element type, and \var{model} is a representation of the
251content model.
252\end{methoddesc}
253
254\begin{methoddesc}[xmlparser]{AttlistDeclHandler}{elname, attname,
255 type, default, required}
256Called for each declared attribute for an element type. If an
257attribute list declaration declares three attributes, this handler is
258called three times, once for each attribute. \var{elname} is the name
259of the element to which the declaration applies and \var{attname} is
260the name of the attribute declared. The attribute type is a string
261passed as \var{type}; the possible values are \code{'CDATA'},
262\code{'ID'}, \code{'IDREF'}, ...
263\var{default} gives the default value for the attribute used when the
264attribute is not specified by the document instance, or \code{None} if
265there is no default value (\code{\#IMPLIED} values). If the attribute
266is required to be given in the document instance, \var{required} will
267be true.
268This requires Expat version 1.95.0 or newer.
269\end{methoddesc}
270
Fred Drakeefffe8e2000-10-29 05:10:30 +0000271\begin{methoddesc}[xmlparser]{StartElementHandler}{name, attributes}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000272Called for the start of every element. \var{name} is a string
273containing the element name, and \var{attributes} is a dictionary
274mapping attribute names to their values.
275\end{methoddesc}
276
Fred Drakeefffe8e2000-10-29 05:10:30 +0000277\begin{methoddesc}[xmlparser]{EndElementHandler}{name}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000278Called for the end of every element.
279\end{methoddesc}
280
Fred Drakeefffe8e2000-10-29 05:10:30 +0000281\begin{methoddesc}[xmlparser]{ProcessingInstructionHandler}{target, data}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000282Called for every processing instruction.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000283\end{methoddesc}
284
Fred Drakeefffe8e2000-10-29 05:10:30 +0000285\begin{methoddesc}[xmlparser]{CharacterDataHandler}{data}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000286Called for character data. This will be called for normal character
287data, CDATA marked content, and ignorable whitespace. Applications
288which must distinguish these cases can use the
289\member{StartCdataSectionHandler}, \member{EndCdataSectionHandler},
290and \member{ElementDeclHandler} callbacks to collect the required
291information.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000292\end{methoddesc}
293
Fred Drakeefffe8e2000-10-29 05:10:30 +0000294\begin{methoddesc}[xmlparser]{UnparsedEntityDeclHandler}{entityName, base,
295 systemId, publicId,
296 notationName}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000297Called for unparsed (NDATA) entity declarations. This is only present
298for version 1.2 of the Expat library; for more recent versions, use
299\member{EntityDeclHandler} instead. (The underlying function in the
300Expat library has been declared obsolete.)
301\end{methoddesc}
302
303\begin{methoddesc}[xmlparser]{EntityDeclHandler}{entityName,
304 is_parameter_entity, value,
305 base, systemId,
306 publicId,
307 notationName}
308Called for all entity declarations. For parameter and internal
309entities, \var{value} will be a string giving the declared contents
310of the entity; this will be \code{None} for external entities. The
311\var{notationName} parameter will be \code{None} for parsed entities,
312and the name of the notation for unparsed entities.
313\var{is_parameter_entity} will be true if the entity is a paremeter
314entity or false for general entities (most applications only need to
315be concerned with general entities).
316This is only available starting with version 1.95.0 of the Expat
317library.
318\versionadded{2.1}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000319\end{methoddesc}
320
Fred Drakeefffe8e2000-10-29 05:10:30 +0000321\begin{methoddesc}[xmlparser]{NotationDeclHandler}{notationName, base,
322 systemId, publicId}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000323Called for notation declarations. \var{notationName}, \var{base}, and
324\var{systemId}, and \var{publicId} are strings if given. If the
325public identifier is omitted, \var{publicId} will be \code{None}.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000326\end{methoddesc}
327
Fred Drakeefffe8e2000-10-29 05:10:30 +0000328\begin{methoddesc}[xmlparser]{StartNamespaceDeclHandler}{prefix, uri}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000329Called when an element contains a namespace declaration. Namespace
330declarations are processed before the \member{StartElementHandler} is
331called for the element on which declarations are placed.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000332\end{methoddesc}
333
Fred Drakeefffe8e2000-10-29 05:10:30 +0000334\begin{methoddesc}[xmlparser]{EndNamespaceDeclHandler}{prefix}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000335Called when the closing tag is reached for an element
Fred Drake5ed1dac2001-02-08 15:40:33 +0000336that contained a namespace declaration. This is called once for each
337namespace declaration on the element in the reverse of the order for
338which the \member{StartNamespaceDeclHandler} was called to indicate
339the start of each namespace declaration's scope. Calls to this
340handler are made after the corresponding \member{EndElementHandler}
341for the end of the element.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000342\end{methoddesc}
343
Fred Drakeefffe8e2000-10-29 05:10:30 +0000344\begin{methoddesc}[xmlparser]{CommentHandler}{data}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000345Called for comments. \var{data} is the text of the comment, excluding
Fred Drake523ec572001-02-15 05:37:51 +0000346the leading `\code{<!-}\code{-}' and trailing `\code{-}\code{->}'.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000347\end{methoddesc}
348
Fred Drakeefffe8e2000-10-29 05:10:30 +0000349\begin{methoddesc}[xmlparser]{StartCdataSectionHandler}{}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000350Called at the start of a CDATA section. This and
351\member{StartCdataSectionHandler} are needed to be able to identify
352the syntactical start and end for CDATA sections.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000353\end{methoddesc}
354
Fred Drakeefffe8e2000-10-29 05:10:30 +0000355\begin{methoddesc}[xmlparser]{EndCdataSectionHandler}{}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000356Called at the end of a CDATA section.
357\end{methoddesc}
358
Fred Drakeefffe8e2000-10-29 05:10:30 +0000359\begin{methoddesc}[xmlparser]{DefaultHandler}{data}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000360Called for any characters in the XML document for
361which no applicable handler has been specified. This means
362characters that are part of a construct which could be reported, but
363for which no handler has been supplied.
364\end{methoddesc}
365
Fred Drakeefffe8e2000-10-29 05:10:30 +0000366\begin{methoddesc}[xmlparser]{DefaultHandlerExpand}{data}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000367This is the same as the \function{DefaultHandler},
368but doesn't inhibit expansion of internal entities.
369The entity reference will not be passed to the default handler.
370\end{methoddesc}
371
Fred Drake5ed1dac2001-02-08 15:40:33 +0000372\begin{methoddesc}[xmlparser]{NotStandaloneHandler}{} Called if the
373XML document hasn't been declared as being a standalone document.
374This happens when there is an external subset or a reference to a
375parameter entity, but the XML declaration does not set standalone to
376\code{yes} in an XML declaration. If this handler returns \code{0},
377then the parser will throw an \constant{XML_ERROR_NOT_STANDALONE}
378error. If this handler is not set, no exception is raised by the
379parser for this condition.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000380\end{methoddesc}
381
Fred Drakeefffe8e2000-10-29 05:10:30 +0000382\begin{methoddesc}[xmlparser]{ExternalEntityRefHandler}{context, base,
383 systemId, publicId}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000384Called for references to external entities. \var{base} is the current
385base, as set by a previous call to \method{SetBase()}. The public and
386system identifiers, \var{systemId} and \var{publicId}, are strings if
387given; if the public identifier is not given, \var{publicId} will be
Fred Drake523ec572001-02-15 05:37:51 +0000388\code{None}. The \var{context} value is opaque and should only be
389used as described below.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000390
391For external entities to be parsed, this handler must be implemented.
392It is responsible for creating the sub-parser using
Fred Drake523ec572001-02-15 05:37:51 +0000393\code{ExternalEntityParserCreate(\var{context})}, initializing it with
394the appropriate callbacks, and parsing the entity. This handler
395should return an integer; if it returns \code{0}, the parser will
396throw an \constant{XML_ERROR_EXTERNAL_ENTITY_HANDLING} error,
397otherwise parsing will continue.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000398
399If this handler is not provided, external entities are reported by the
400\member{DefaultHandler} callback, if provided.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000401\end{methoddesc}
402
403
Fred Drake1d8ad2b2001-02-14 18:54:32 +0000404\subsection{ExpatError Exceptions \label{expaterror-objects}}
405\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
406
407\exception{ExpatError} exceptions have a number of interesting
408attributes:
409
410\begin{memberdesc}[ExpatError]{code}
411 Expat's internal error number for the specific error. This will
412 match one of the constants defined in the \code{errors} object from
413 this module.
414 \versionadded{2.1}
415\end{memberdesc}
416
417\begin{memberdesc}[ExpatError]{lineno}
418 Line number on which the error was detected. The first line is
419 numbered \code{1}.
420 \versionadded{2.1}
421\end{memberdesc}
422
423\begin{memberdesc}[ExpatError]{offset}
424 Character offset into the line where the error occurred. The first
425 column is numbered \code{0}.
426 \versionadded{2.1}
427\end{memberdesc}
428
429
Fred Drake7fbc85c2000-09-23 04:47:56 +0000430\subsection{Example \label{expat-example}}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000431
Fred Drakec05cbb02000-07-05 02:03:34 +0000432The following program defines three handlers that just print out their
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000433arguments.
434
435\begin{verbatim}
Fred Drake7fbc85c2000-09-23 04:47:56 +0000436import xml.parsers.expat
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000437
438# 3 handler functions
439def start_element(name, attrs):
440 print 'Start element:', name, attrs
441def end_element(name):
442 print 'End element:', name
443def char_data(data):
444 print 'Character data:', repr(data)
445
Fred Drake7fbc85c2000-09-23 04:47:56 +0000446p = xml.parsers.expat.ParserCreate()
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000447
448p.StartElementHandler = start_element
Fred Drake7fbc85c2000-09-23 04:47:56 +0000449p.EndElementHandler = end_element
450p.CharacterDataHandler = char_data
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000451
452p.Parse("""<?xml version="1.0"?>
453<parent id="top"><child1 name="paul">Text goes here</child1>
454<child2 name="fred">More text</child2>
455</parent>""")
456\end{verbatim}
457
458The output from this program is:
459
460\begin{verbatim}
461Start element: parent {'id': 'top'}
462Start element: child1 {'name': 'paul'}
463Character data: 'Text goes here'
464End element: child1
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +0000465Character data: '\n'
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000466Start element: child2 {'name': 'fred'}
467Character data: 'More text'
468End element: child2
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +0000469Character data: '\n'
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000470End element: parent
471\end{verbatim}
Fred Drakec05cbb02000-07-05 02:03:34 +0000472
473
Fred Drake5ed1dac2001-02-08 15:40:33 +0000474\subsection{Content Model Descriptions \label{expat-content-models}}
475\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
476
477Content modules are described using nested tuples. Each tuple
478contains four values: the type, the quantifier, the name, and a tuple
479of children. Children are simply additional content module
480descriptions.
481
482The values of the first two fields are constants defined in the
483\code{model} object of the \module{xml.parsers.expat} module. These
484constants can be collected in two groups: the model type group and the
485quantifier group.
486
487The constants in the model type group are:
488
489\begin{datadescni}{XML_CTYPE_ANY}
490The element named by the model name was declared to have a content
491model of \code{ANY}.
492\end{datadescni}
493
494\begin{datadescni}{XML_CTYPE_CHOICE}
495The named element allows a choice from a number of options; this is
496used for content models such as \code{(A | B | C)}.
497\end{datadescni}
498
499\begin{datadescni}{XML_CTYPE_EMPTY}
500Elements which are declared to be \code{EMPTY} have this model type.
501\end{datadescni}
502
503\begin{datadescni}{XML_CTYPE_MIXED}
504\end{datadescni}
505
506\begin{datadescni}{XML_CTYPE_NAME}
507\end{datadescni}
508
509\begin{datadescni}{XML_CTYPE_SEQ}
510Models which represent a series of models which follow one after the
511other are indicated with this model type. This is used for models
512such as \code{(A, B, C)}.
513\end{datadescni}
514
515
516The constants in the quantifier group are:
517
518\begin{datadescni}{XML_CQUANT_NONE}
Fred Drakee0af35e2001-09-20 20:43:28 +0000519No modifier is given, so it can appear exactly once, as for \code{A}.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000520\end{datadescni}
521
522\begin{datadescni}{XML_CQUANT_OPT}
Fred Drakee0af35e2001-09-20 20:43:28 +0000523The model is optional: it can appear once or not at all, as for
Fred Drake5ed1dac2001-02-08 15:40:33 +0000524\code{A?}.
525\end{datadescni}
526
527\begin{datadescni}{XML_CQUANT_PLUS}
Fred Drakee0af35e2001-09-20 20:43:28 +0000528The model must occur one or more times (like \code{A+}).
Fred Drake5ed1dac2001-02-08 15:40:33 +0000529\end{datadescni}
530
531\begin{datadescni}{XML_CQUANT_REP}
532The model must occur zero or more times, as for \code{A*}.
533\end{datadescni}
534
535
Fred Drake7fbc85c2000-09-23 04:47:56 +0000536\subsection{Expat error constants \label{expat-errors}}
Fred Drakec05cbb02000-07-05 02:03:34 +0000537\sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
538
Fred Drake1d8ad2b2001-02-14 18:54:32 +0000539The following constants are provided in the \code{errors} object of
540the \refmodule{xml.parsers.expat} module. These constants are useful
541in interpreting some of the attributes of the \exception{ExpatError}
542exception objects raised when an error has occurred.
Fred Drakec05cbb02000-07-05 02:03:34 +0000543
Fred Drake7fbc85c2000-09-23 04:47:56 +0000544The \code{errors} object has the following attributes:
Fred Drakec05cbb02000-07-05 02:03:34 +0000545
Fred Drake5ed1dac2001-02-08 15:40:33 +0000546\begin{datadescni}{XML_ERROR_ASYNC_ENTITY}
547\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000548
Fred Drake5ed1dac2001-02-08 15:40:33 +0000549\begin{datadescni}{XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF}
550An entity reference in an attribute value referred to an external
551entity instead of an internal entity.
552\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000553
Fred Drake5ed1dac2001-02-08 15:40:33 +0000554\begin{datadescni}{XML_ERROR_BAD_CHAR_REF}
Fred Drakee0af35e2001-09-20 20:43:28 +0000555A character reference referred to a character which is illegal in XML
556(for example, character \code{0}, or `\code{\&\#0;}'.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000557\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000558
Fred Drake5ed1dac2001-02-08 15:40:33 +0000559\begin{datadescni}{XML_ERROR_BINARY_ENTITY_REF}
Fred Drakee0af35e2001-09-20 20:43:28 +0000560An entity reference referred to an entity which was declared with a
561notation, so cannot be parsed.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000562\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000563
Fred Drake5ed1dac2001-02-08 15:40:33 +0000564\begin{datadescni}{XML_ERROR_DUPLICATE_ATTRIBUTE}
Fred Drakeacab3d62000-07-11 16:30:30 +0000565An attribute was used more than once in a start tag.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000566\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000567
Fred Drake5ed1dac2001-02-08 15:40:33 +0000568\begin{datadescni}{XML_ERROR_INCORRECT_ENCODING}
569\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000570
Fred Drake5ed1dac2001-02-08 15:40:33 +0000571\begin{datadescni}{XML_ERROR_INVALID_TOKEN}
Fred Drakee0af35e2001-09-20 20:43:28 +0000572Raised when an input byte could not properly be assigned to a
573character; for example, a NUL byte (value \code{0}) in a UTF-8 input
574stream.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000575\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000576
Fred Drake5ed1dac2001-02-08 15:40:33 +0000577\begin{datadescni}{XML_ERROR_JUNK_AFTER_DOC_ELEMENT}
Fred Drakeacab3d62000-07-11 16:30:30 +0000578Something other than whitespace occurred after the document element.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000579\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000580
Fred Drake5ed1dac2001-02-08 15:40:33 +0000581\begin{datadescni}{XML_ERROR_MISPLACED_XML_PI}
Fred Drakee0af35e2001-09-20 20:43:28 +0000582An XML declaration was found somewhere other than the start of the
583input data.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000584\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000585
Fred Drake5ed1dac2001-02-08 15:40:33 +0000586\begin{datadescni}{XML_ERROR_NO_ELEMENTS}
Fred Drakee0af35e2001-09-20 20:43:28 +0000587The document contains no elements (XML requires all documents to
588contain exactly one top-level element)..
Fred Drake5ed1dac2001-02-08 15:40:33 +0000589\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000590
Fred Drake5ed1dac2001-02-08 15:40:33 +0000591\begin{datadescni}{XML_ERROR_NO_MEMORY}
Fred Drakeacab3d62000-07-11 16:30:30 +0000592Expat was not able to allocate memory internally.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000593\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000594
Fred Drake5ed1dac2001-02-08 15:40:33 +0000595\begin{datadescni}{XML_ERROR_PARAM_ENTITY_REF}
Fred Drakee0af35e2001-09-20 20:43:28 +0000596A parameter entity reference was found where it was not allowed.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000597\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000598
Fred Drake5ed1dac2001-02-08 15:40:33 +0000599\begin{datadescni}{XML_ERROR_PARTIAL_CHAR}
Fred Drakee0af35e2001-09-20 20:43:28 +0000600
Fred Drake5ed1dac2001-02-08 15:40:33 +0000601\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000602
Fred Drake5ed1dac2001-02-08 15:40:33 +0000603\begin{datadescni}{XML_ERROR_RECURSIVE_ENTITY_REF}
Fred Drakee0af35e2001-09-20 20:43:28 +0000604An entity reference contained another reference to the same entity;
605possibly via a different name, and possibly indirectly.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000606\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000607
Fred Drake5ed1dac2001-02-08 15:40:33 +0000608\begin{datadescni}{XML_ERROR_SYNTAX}
Fred Drakeacab3d62000-07-11 16:30:30 +0000609Some unspecified syntax error was encountered.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000610\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000611
Fred Drake5ed1dac2001-02-08 15:40:33 +0000612\begin{datadescni}{XML_ERROR_TAG_MISMATCH}
Fred Drakeacab3d62000-07-11 16:30:30 +0000613An end tag did not match the innermost open start tag.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000614\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000615
Fred Drake5ed1dac2001-02-08 15:40:33 +0000616\begin{datadescni}{XML_ERROR_UNCLOSED_TOKEN}
Fred Drakee0af35e2001-09-20 20:43:28 +0000617Some token (such as a start tag) was not closed before the end of the
618stream or the next token was encountered.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000619\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000620
Fred Drake5ed1dac2001-02-08 15:40:33 +0000621\begin{datadescni}{XML_ERROR_UNDEFINED_ENTITY}
Fred Drakeacab3d62000-07-11 16:30:30 +0000622A reference was made to a entity which was not defined.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000623\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000624
Fred Drake5ed1dac2001-02-08 15:40:33 +0000625\begin{datadescni}{XML_ERROR_UNKNOWN_ENCODING}
Fred Drakeacab3d62000-07-11 16:30:30 +0000626The document encoding is not supported by Expat.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000627\end{datadescni}