blob: 37b36e10a6d72af1d5a27049973d9c732c383820 [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 Drakeefffe8e2000-10-29 05:10:30 +000036 The exception raised when Expat reports an error.
37\end{excdesc}
38
Fred Drake1d8ad2b2001-02-14 18:54:32 +000039\begin{excdesc}{error}
40 Alias for \exception{ExpatError}.
41\end{excdesc}
42
Fred Drakeefffe8e2000-10-29 05:10:30 +000043\begin{datadesc}{XMLParserType}
44 The type of the return values from the \function{ParserCreate()}
45 function.
46\end{datadesc}
47
48
Fred Drake7fbc85c2000-09-23 04:47:56 +000049The \module{xml.parsers.expat} module contains two functions:
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000050
51\begin{funcdesc}{ErrorString}{errno}
52Returns an explanatory string for a given error number \var{errno}.
53\end{funcdesc}
54
Fred Drakeefffe8e2000-10-29 05:10:30 +000055\begin{funcdesc}{ParserCreate}{\optional{encoding\optional{,
56 namespace_separator}}}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000057Creates and returns a new \class{xmlparser} object.
58\var{encoding}, if specified, must be a string naming the encoding
59used by the XML data. Expat doesn't support as many encodings as
60Python does, and its repertoire of encodings can't be extended; it
Fred Drake5ed1dac2001-02-08 15:40:33 +000061supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. If
62\var{encoding} is given it will override the implicit or explicit
63encoding of the document.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000064
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000065Expat can optionally do XML namespace processing for you, enabled by
Fred Drakeefffe8e2000-10-29 05:10:30 +000066providing a value for \var{namespace_separator}. The value must be a
67one-character string; a \exception{ValueError} will be raised if the
68string has an illegal length (\code{None} is considered the same as
69omission). When namespace processing is enabled, element type names
70and attribute names that belong to a namespace will be expanded. The
71element name passed to the element handlers
Fred Drake5ed1dac2001-02-08 15:40:33 +000072\member{StartElementHandler} and \member{EndElementHandler}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000073will be the concatenation of the namespace URI, the namespace
74separator character, and the local part of the name. If the namespace
Fred Drakeefffe8e2000-10-29 05:10:30 +000075separator is a zero byte (\code{chr(0)}) then the namespace URI and
Fred Drake5ed1dac2001-02-08 15:40:33 +000076the local part will be concatenated without any separator.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000077
Fred Drake2fef3ab2000-11-28 06:38:22 +000078For example, if \var{namespace_separator} is set to a space character
79(\character{ }) and the following document is parsed:
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000080
81\begin{verbatim}
82<?xml version="1.0"?>
83<root xmlns = "http://default-namespace.org/"
84 xmlns:py = "http://www.python.org/ns/">
85 <py:elem1 />
86 <elem2 xmlns="" />
87</root>
88\end{verbatim}
89
Fred Drake5ed1dac2001-02-08 15:40:33 +000090\member{StartElementHandler} will receive the following strings
Fred Draked79c33a2000-09-25 14:14:30 +000091for each element:
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000092
93\begin{verbatim}
94http://default-namespace.org/ root
95http://www.python.org/ns/ elem1
96elem2
97\end{verbatim}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +000098\end{funcdesc}
99
Fred Drakef08cbb12000-12-23 22:19:05 +0000100
101\subsection{XMLParser Objects \label{xmlparser-objects}}
102
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000103\class{xmlparser} objects have the following methods:
104
Fred Drake2fef3ab2000-11-28 06:38:22 +0000105\begin{methoddesc}[xmlparser]{Parse}{data\optional{, isfinal}}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000106Parses the contents of the string \var{data}, calling the appropriate
107handler functions to process the parsed data. \var{isfinal} must be
Fred Drakef08cbb12000-12-23 22:19:05 +0000108true on the final call to this method. \var{data} can be the empty
Fred Drakec05cbb02000-07-05 02:03:34 +0000109string at any time.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000110\end{methoddesc}
111
Fred Drakeefffe8e2000-10-29 05:10:30 +0000112\begin{methoddesc}[xmlparser]{ParseFile}{file}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000113Parse XML data reading from the object \var{file}. \var{file} only
114needs to provide the \method{read(\var{nbytes})} method, returning the
115empty string when there's no more data.
116\end{methoddesc}
117
Fred Drakeefffe8e2000-10-29 05:10:30 +0000118\begin{methoddesc}[xmlparser]{SetBase}{base}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000119Sets the base to be used for resolving relative URIs in system
120identifiers in declarations. Resolving relative identifiers is left
121to the application: this value will be passed through as the
122\var{base} argument to the \function{ExternalEntityRefHandler},
123\function{NotationDeclHandler}, and
124\function{UnparsedEntityDeclHandler} functions.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000125\end{methoddesc}
126
Fred Drakeefffe8e2000-10-29 05:10:30 +0000127\begin{methoddesc}[xmlparser]{GetBase}{}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000128Returns a string containing the base set by a previous call to
129\method{SetBase()}, or \code{None} if
130\method{SetBase()} hasn't been called.
131\end{methoddesc}
132
Fred Drake1d8ad2b2001-02-14 18:54:32 +0000133\begin{methoddesc}[xmlparser]{GetInputContext}{}
134Returns the input data that generated the current event as a string.
135The data is in the encoding of the entity which contains the text.
136When called while an event handler is not active, the return value is
137\code{None}.
138\versionadded{2.1}
139\end{methoddesc}
140
Fred Drakef08cbb12000-12-23 22:19:05 +0000141\begin{methoddesc}[xmlparser]{ExternalEntityParserCreate}{context\optional{,
142 encoding}}
143Create a ``child'' parser which can be used to parse an external
144parsed entity referred to by content parsed by the parent parser. The
Fred Drakeb162d182001-01-04 05:48:08 +0000145\var{context} parameter should be the string passed to the
Fred Drakef08cbb12000-12-23 22:19:05 +0000146\method{ExternalEntityRefHandler()} handler function, described below.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000147The child parser is created with the \member{ordered_attributes},
148\member{returns_unicode} and \member{specified_attributes} set to the
149values of this parser.
Fred Drakef08cbb12000-12-23 22:19:05 +0000150\end{methoddesc}
151
Fred Drakeefffe8e2000-10-29 05:10:30 +0000152
Fred Draked79c33a2000-09-25 14:14:30 +0000153\class{xmlparser} objects have the following attributes:
Andrew M. Kuchling0690c862000-08-17 23:15:21 +0000154
Fred Drake5ed1dac2001-02-08 15:40:33 +0000155\begin{memberdesc}[xmlparser]{ordered_attributes}
156Setting this attribute to a non-zero integer causes the attributes to
157be reported as a list rather than a dictionary. The attributes are
158presented in the order found in the document text. For each
159attribute, two list entries are presented: the attribute name and the
160attribute value. (Older versions of this module also used this
161format.) By default, this attribute is false; it may be changed at
162any time.
163\versionadded{2.1}
164\end{memberdesc}
165
Fred Drakeefffe8e2000-10-29 05:10:30 +0000166\begin{memberdesc}[xmlparser]{returns_unicode}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000167If this attribute is set to a non-zero integer, the handler functions
168will be passed Unicode strings. If \member{returns_unicode} is 0,
1698-bit strings containing UTF-8 encoded data will be passed to the
170handlers.
Fred Drakeb62966c2000-12-07 00:00:21 +0000171\versionchanged[Can be changed at any time to affect the result
172 type.]{1.6}
Fred Drakeefffe8e2000-10-29 05:10:30 +0000173\end{memberdesc}
Andrew M. Kuchling0690c862000-08-17 23:15:21 +0000174
Fred Drake5ed1dac2001-02-08 15:40:33 +0000175\begin{memberdesc}[xmlparser]{specified_attributes}
176If set to a non-zero integer, the parser will report only those
177attributes which were specified in the document instance and not those
178which were derived from attribute declarations. Applications which
179set this need to be especially careful to use what additional
180information is available from the declarations as needed to comply
181with the standards for the behavior of XML processors. By default,
182this attribute is false; it may be changed at any time.
183\versionadded{2.1}
184\end{memberdesc}
185
Andrew M. Kuchling0690c862000-08-17 23:15:21 +0000186The following attributes contain values relating to the most recent
187error encountered by an \class{xmlparser} object, and will only have
188correct values once a call to \method{Parse()} or \method{ParseFile()}
Fred Drake523ec572001-02-15 05:37:51 +0000189has raised a \exception{xml.parsers.expat.ExpatError} exception.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000190
Fred Drakeefffe8e2000-10-29 05:10:30 +0000191\begin{memberdesc}[xmlparser]{ErrorByteIndex}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000192Byte index at which an error occurred.
Fred Drakeefffe8e2000-10-29 05:10:30 +0000193\end{memberdesc}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000194
Fred Drakeefffe8e2000-10-29 05:10:30 +0000195\begin{memberdesc}[xmlparser]{ErrorCode}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000196Numeric code specifying the problem. This value can be passed to the
197\function{ErrorString()} function, or compared to one of the constants
Fred Drake523ec572001-02-15 05:37:51 +0000198defined in the \code{errors} object.
Fred Drakeefffe8e2000-10-29 05:10:30 +0000199\end{memberdesc}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000200
Fred Drakeefffe8e2000-10-29 05:10:30 +0000201\begin{memberdesc}[xmlparser]{ErrorColumnNumber}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000202Column number at which an error occurred.
Fred Drakeefffe8e2000-10-29 05:10:30 +0000203\end{memberdesc}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000204
Fred Drakeefffe8e2000-10-29 05:10:30 +0000205\begin{memberdesc}[xmlparser]{ErrorLineNumber}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000206Line number at which an error occurred.
Fred Drakeefffe8e2000-10-29 05:10:30 +0000207\end{memberdesc}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000208
209Here is the list of handlers that can be set. To set a handler on an
Fred Drakec05cbb02000-07-05 02:03:34 +0000210\class{xmlparser} object \var{o}, use
211\code{\var{o}.\var{handlername} = \var{func}}. \var{handlername} must
212be taken from the following list, and \var{func} must be a callable
213object accepting the correct number of arguments. The arguments are
214all strings, unless otherwise stated.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000215
Fred Drake5ed1dac2001-02-08 15:40:33 +0000216\begin{methoddesc}[xmlparser]{XmlDeclHandler}{version, encoding, standalone}
217Called when the XML declaration is parsed. The XML declaration is the
218(optional) declaration of the applicable version of the XML
219recommendation, the encoding of the document text, and an optional
220``standalone'' declaration. \var{version} and \var{encoding} will be
221strings of the type dictated by the \member{returns_unicode}
222attribute, and \var{standalone} will be \code{1} if the document is
223declared standalone, \code{0} if it is declared not to be standalone,
224or \code{-1} if the standalone clause was omitted.
225This is only available with Expat version 1.95.0 or newer.
226\versionadded{2.1}
227\end{methoddesc}
228
229\begin{methoddesc}[xmlparser]{StartDoctypeDeclHandler}{doctypeName,
230 systemId, publicId,
231 has_internal_subset}
232Called when Expat begins parsing the document type declaration
233(\code{<!DOCTYPE \ldots}). The \var{doctypeName} is provided exactly
234as presented. The \var{systemId} and \var{publicId} parameters give
235the system and public identifiers if specified, or \code{None} if
236omitted. \var{has_internal_subset} will be true if the document
237contains and internal document declaration subset.
238This requires Expat version 1.2 or newer.
239\end{methoddesc}
240
241\begin{methoddesc}[xmlparser]{EndDoctypeDeclHandler}{}
242Called when Expat is done parsing the document type delaration.
243This requires Expat version 1.2 or newer.
244\end{methoddesc}
245
246\begin{methoddesc}[xmlparser]{ElementDeclHandler}{name, model}
247Called once for each element type declaration. \var{name} is the name
248of the element type, and \var{model} is a representation of the
249content model.
250\end{methoddesc}
251
252\begin{methoddesc}[xmlparser]{AttlistDeclHandler}{elname, attname,
253 type, default, required}
254Called for each declared attribute for an element type. If an
255attribute list declaration declares three attributes, this handler is
256called three times, once for each attribute. \var{elname} is the name
257of the element to which the declaration applies and \var{attname} is
258the name of the attribute declared. The attribute type is a string
259passed as \var{type}; the possible values are \code{'CDATA'},
260\code{'ID'}, \code{'IDREF'}, ...
261\var{default} gives the default value for the attribute used when the
262attribute is not specified by the document instance, or \code{None} if
263there is no default value (\code{\#IMPLIED} values). If the attribute
264is required to be given in the document instance, \var{required} will
265be true.
266This requires Expat version 1.95.0 or newer.
267\end{methoddesc}
268
Fred Drakeefffe8e2000-10-29 05:10:30 +0000269\begin{methoddesc}[xmlparser]{StartElementHandler}{name, attributes}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000270Called for the start of every element. \var{name} is a string
271containing the element name, and \var{attributes} is a dictionary
272mapping attribute names to their values.
273\end{methoddesc}
274
Fred Drakeefffe8e2000-10-29 05:10:30 +0000275\begin{methoddesc}[xmlparser]{EndElementHandler}{name}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000276Called for the end of every element.
277\end{methoddesc}
278
Fred Drakeefffe8e2000-10-29 05:10:30 +0000279\begin{methoddesc}[xmlparser]{ProcessingInstructionHandler}{target, data}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000280Called for every processing instruction.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000281\end{methoddesc}
282
Fred Drakeefffe8e2000-10-29 05:10:30 +0000283\begin{methoddesc}[xmlparser]{CharacterDataHandler}{data}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000284Called for character data. This will be called for normal character
285data, CDATA marked content, and ignorable whitespace. Applications
286which must distinguish these cases can use the
287\member{StartCdataSectionHandler}, \member{EndCdataSectionHandler},
288and \member{ElementDeclHandler} callbacks to collect the required
289information.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000290\end{methoddesc}
291
Fred Drakeefffe8e2000-10-29 05:10:30 +0000292\begin{methoddesc}[xmlparser]{UnparsedEntityDeclHandler}{entityName, base,
293 systemId, publicId,
294 notationName}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000295Called for unparsed (NDATA) entity declarations. This is only present
296for version 1.2 of the Expat library; for more recent versions, use
297\member{EntityDeclHandler} instead. (The underlying function in the
298Expat library has been declared obsolete.)
299\end{methoddesc}
300
301\begin{methoddesc}[xmlparser]{EntityDeclHandler}{entityName,
302 is_parameter_entity, value,
303 base, systemId,
304 publicId,
305 notationName}
306Called for all entity declarations. For parameter and internal
307entities, \var{value} will be a string giving the declared contents
308of the entity; this will be \code{None} for external entities. The
309\var{notationName} parameter will be \code{None} for parsed entities,
310and the name of the notation for unparsed entities.
311\var{is_parameter_entity} will be true if the entity is a paremeter
312entity or false for general entities (most applications only need to
313be concerned with general entities).
314This is only available starting with version 1.95.0 of the Expat
315library.
316\versionadded{2.1}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000317\end{methoddesc}
318
Fred Drakeefffe8e2000-10-29 05:10:30 +0000319\begin{methoddesc}[xmlparser]{NotationDeclHandler}{notationName, base,
320 systemId, publicId}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000321Called for notation declarations. \var{notationName}, \var{base}, and
322\var{systemId}, and \var{publicId} are strings if given. If the
323public identifier is omitted, \var{publicId} will be \code{None}.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000324\end{methoddesc}
325
Fred Drakeefffe8e2000-10-29 05:10:30 +0000326\begin{methoddesc}[xmlparser]{StartNamespaceDeclHandler}{prefix, uri}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000327Called when an element contains a namespace declaration. Namespace
328declarations are processed before the \member{StartElementHandler} is
329called for the element on which declarations are placed.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000330\end{methoddesc}
331
Fred Drakeefffe8e2000-10-29 05:10:30 +0000332\begin{methoddesc}[xmlparser]{EndNamespaceDeclHandler}{prefix}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000333Called when the closing tag is reached for an element
Fred Drake5ed1dac2001-02-08 15:40:33 +0000334that contained a namespace declaration. This is called once for each
335namespace declaration on the element in the reverse of the order for
336which the \member{StartNamespaceDeclHandler} was called to indicate
337the start of each namespace declaration's scope. Calls to this
338handler are made after the corresponding \member{EndElementHandler}
339for the end of the element.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000340\end{methoddesc}
341
Fred Drakeefffe8e2000-10-29 05:10:30 +0000342\begin{methoddesc}[xmlparser]{CommentHandler}{data}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000343Called for comments. \var{data} is the text of the comment, excluding
Fred Drake523ec572001-02-15 05:37:51 +0000344the leading `\code{<!-}\code{-}' and trailing `\code{-}\code{->}'.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000345\end{methoddesc}
346
Fred Drakeefffe8e2000-10-29 05:10:30 +0000347\begin{methoddesc}[xmlparser]{StartCdataSectionHandler}{}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000348Called at the start of a CDATA section. This and
349\member{StartCdataSectionHandler} are needed to be able to identify
350the syntactical start and end for CDATA sections.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000351\end{methoddesc}
352
Fred Drakeefffe8e2000-10-29 05:10:30 +0000353\begin{methoddesc}[xmlparser]{EndCdataSectionHandler}{}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000354Called at the end of a CDATA section.
355\end{methoddesc}
356
Fred Drakeefffe8e2000-10-29 05:10:30 +0000357\begin{methoddesc}[xmlparser]{DefaultHandler}{data}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000358Called for any characters in the XML document for
359which no applicable handler has been specified. This means
360characters that are part of a construct which could be reported, but
361for which no handler has been supplied.
362\end{methoddesc}
363
Fred Drakeefffe8e2000-10-29 05:10:30 +0000364\begin{methoddesc}[xmlparser]{DefaultHandlerExpand}{data}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000365This is the same as the \function{DefaultHandler},
366but doesn't inhibit expansion of internal entities.
367The entity reference will not be passed to the default handler.
368\end{methoddesc}
369
Fred Drake5ed1dac2001-02-08 15:40:33 +0000370\begin{methoddesc}[xmlparser]{NotStandaloneHandler}{} Called if the
371XML document hasn't been declared as being a standalone document.
372This happens when there is an external subset or a reference to a
373parameter entity, but the XML declaration does not set standalone to
374\code{yes} in an XML declaration. If this handler returns \code{0},
375then the parser will throw an \constant{XML_ERROR_NOT_STANDALONE}
376error. If this handler is not set, no exception is raised by the
377parser for this condition.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000378\end{methoddesc}
379
Fred Drakeefffe8e2000-10-29 05:10:30 +0000380\begin{methoddesc}[xmlparser]{ExternalEntityRefHandler}{context, base,
381 systemId, publicId}
Fred Drake5ed1dac2001-02-08 15:40:33 +0000382Called for references to external entities. \var{base} is the current
383base, as set by a previous call to \method{SetBase()}. The public and
384system identifiers, \var{systemId} and \var{publicId}, are strings if
385given; if the public identifier is not given, \var{publicId} will be
Fred Drake523ec572001-02-15 05:37:51 +0000386\code{None}. The \var{context} value is opaque and should only be
387used as described below.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000388
389For external entities to be parsed, this handler must be implemented.
390It is responsible for creating the sub-parser using
Fred Drake523ec572001-02-15 05:37:51 +0000391\code{ExternalEntityParserCreate(\var{context})}, initializing it with
392the appropriate callbacks, and parsing the entity. This handler
393should return an integer; if it returns \code{0}, the parser will
394throw an \constant{XML_ERROR_EXTERNAL_ENTITY_HANDLING} error,
395otherwise parsing will continue.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000396
397If this handler is not provided, external entities are reported by the
398\member{DefaultHandler} callback, if provided.
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000399\end{methoddesc}
400
401
Fred Drake1d8ad2b2001-02-14 18:54:32 +0000402\subsection{ExpatError Exceptions \label{expaterror-objects}}
403\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
404
405\exception{ExpatError} exceptions have a number of interesting
406attributes:
407
408\begin{memberdesc}[ExpatError]{code}
409 Expat's internal error number for the specific error. This will
410 match one of the constants defined in the \code{errors} object from
411 this module.
412 \versionadded{2.1}
413\end{memberdesc}
414
415\begin{memberdesc}[ExpatError]{lineno}
416 Line number on which the error was detected. The first line is
417 numbered \code{1}.
418 \versionadded{2.1}
419\end{memberdesc}
420
421\begin{memberdesc}[ExpatError]{offset}
422 Character offset into the line where the error occurred. The first
423 column is numbered \code{0}.
424 \versionadded{2.1}
425\end{memberdesc}
426
427
Fred Drake7fbc85c2000-09-23 04:47:56 +0000428\subsection{Example \label{expat-example}}
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000429
Fred Drakec05cbb02000-07-05 02:03:34 +0000430The following program defines three handlers that just print out their
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000431arguments.
432
433\begin{verbatim}
Fred Drake7fbc85c2000-09-23 04:47:56 +0000434import xml.parsers.expat
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000435
436# 3 handler functions
437def start_element(name, attrs):
438 print 'Start element:', name, attrs
439def end_element(name):
440 print 'End element:', name
441def char_data(data):
442 print 'Character data:', repr(data)
443
Fred Drake7fbc85c2000-09-23 04:47:56 +0000444p = xml.parsers.expat.ParserCreate()
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000445
446p.StartElementHandler = start_element
Fred Drake7fbc85c2000-09-23 04:47:56 +0000447p.EndElementHandler = end_element
448p.CharacterDataHandler = char_data
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000449
450p.Parse("""<?xml version="1.0"?>
451<parent id="top"><child1 name="paul">Text goes here</child1>
452<child2 name="fred">More text</child2>
453</parent>""")
454\end{verbatim}
455
456The output from this program is:
457
458\begin{verbatim}
459Start element: parent {'id': 'top'}
460Start element: child1 {'name': 'paul'}
461Character data: 'Text goes here'
462End element: child1
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +0000463Character data: '\n'
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000464Start element: child2 {'name': 'fred'}
465Character data: 'More text'
466End element: child2
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +0000467Character data: '\n'
Andrew M. Kuchling6b14eeb2000-06-11 02:42:07 +0000468End element: parent
469\end{verbatim}
Fred Drakec05cbb02000-07-05 02:03:34 +0000470
471
Fred Drake5ed1dac2001-02-08 15:40:33 +0000472\subsection{Content Model Descriptions \label{expat-content-models}}
473\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
474
475Content modules are described using nested tuples. Each tuple
476contains four values: the type, the quantifier, the name, and a tuple
477of children. Children are simply additional content module
478descriptions.
479
480The values of the first two fields are constants defined in the
481\code{model} object of the \module{xml.parsers.expat} module. These
482constants can be collected in two groups: the model type group and the
483quantifier group.
484
485The constants in the model type group are:
486
487\begin{datadescni}{XML_CTYPE_ANY}
488The element named by the model name was declared to have a content
489model of \code{ANY}.
490\end{datadescni}
491
492\begin{datadescni}{XML_CTYPE_CHOICE}
493The named element allows a choice from a number of options; this is
494used for content models such as \code{(A | B | C)}.
495\end{datadescni}
496
497\begin{datadescni}{XML_CTYPE_EMPTY}
498Elements which are declared to be \code{EMPTY} have this model type.
499\end{datadescni}
500
501\begin{datadescni}{XML_CTYPE_MIXED}
502\end{datadescni}
503
504\begin{datadescni}{XML_CTYPE_NAME}
505\end{datadescni}
506
507\begin{datadescni}{XML_CTYPE_SEQ}
508Models which represent a series of models which follow one after the
509other are indicated with this model type. This is used for models
510such as \code{(A, B, C)}.
511\end{datadescni}
512
513
514The constants in the quantifier group are:
515
516\begin{datadescni}{XML_CQUANT_NONE}
517\end{datadescni}
518
519\begin{datadescni}{XML_CQUANT_OPT}
520The model is option: it can appear once or not at all, as for
521\code{A?}.
522\end{datadescni}
523
524\begin{datadescni}{XML_CQUANT_PLUS}
525The model must occur one or more times (\code{A+}).
526\end{datadescni}
527
528\begin{datadescni}{XML_CQUANT_REP}
529The model must occur zero or more times, as for \code{A*}.
530\end{datadescni}
531
532
Fred Drake7fbc85c2000-09-23 04:47:56 +0000533\subsection{Expat error constants \label{expat-errors}}
Fred Drakec05cbb02000-07-05 02:03:34 +0000534\sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
535
Fred Drake1d8ad2b2001-02-14 18:54:32 +0000536The following constants are provided in the \code{errors} object of
537the \refmodule{xml.parsers.expat} module. These constants are useful
538in interpreting some of the attributes of the \exception{ExpatError}
539exception objects raised when an error has occurred.
Fred Drakec05cbb02000-07-05 02:03:34 +0000540
Fred Drake7fbc85c2000-09-23 04:47:56 +0000541The \code{errors} object has the following attributes:
Fred Drakec05cbb02000-07-05 02:03:34 +0000542
Fred Drake5ed1dac2001-02-08 15:40:33 +0000543\begin{datadescni}{XML_ERROR_ASYNC_ENTITY}
544\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000545
Fred Drake5ed1dac2001-02-08 15:40:33 +0000546\begin{datadescni}{XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF}
547An entity reference in an attribute value referred to an external
548entity instead of an internal entity.
549\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000550
Fred Drake5ed1dac2001-02-08 15:40:33 +0000551\begin{datadescni}{XML_ERROR_BAD_CHAR_REF}
552\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000553
Fred Drake5ed1dac2001-02-08 15:40:33 +0000554\begin{datadescni}{XML_ERROR_BINARY_ENTITY_REF}
555\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000556
Fred Drake5ed1dac2001-02-08 15:40:33 +0000557\begin{datadescni}{XML_ERROR_DUPLICATE_ATTRIBUTE}
Fred Drakeacab3d62000-07-11 16:30:30 +0000558An attribute was used more than once in a start tag.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000559\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000560
Fred Drake5ed1dac2001-02-08 15:40:33 +0000561\begin{datadescni}{XML_ERROR_INCORRECT_ENCODING}
562\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000563
Fred Drake5ed1dac2001-02-08 15:40:33 +0000564\begin{datadescni}{XML_ERROR_INVALID_TOKEN}
565\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000566
Fred Drake5ed1dac2001-02-08 15:40:33 +0000567\begin{datadescni}{XML_ERROR_JUNK_AFTER_DOC_ELEMENT}
Fred Drakeacab3d62000-07-11 16:30:30 +0000568Something other than whitespace occurred after the document element.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000569\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000570
Fred Drake5ed1dac2001-02-08 15:40:33 +0000571\begin{datadescni}{XML_ERROR_MISPLACED_XML_PI}
572\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000573
Fred Drake5ed1dac2001-02-08 15:40:33 +0000574\begin{datadescni}{XML_ERROR_NO_ELEMENTS}
575The document contains no elements.
576\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000577
Fred Drake5ed1dac2001-02-08 15:40:33 +0000578\begin{datadescni}{XML_ERROR_NO_MEMORY}
Fred Drakeacab3d62000-07-11 16:30:30 +0000579Expat was not able to allocate memory internally.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000580\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000581
Fred Drake5ed1dac2001-02-08 15:40:33 +0000582\begin{datadescni}{XML_ERROR_PARAM_ENTITY_REF}
583\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000584
Fred Drake5ed1dac2001-02-08 15:40:33 +0000585\begin{datadescni}{XML_ERROR_PARTIAL_CHAR}
586\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000587
Fred Drake5ed1dac2001-02-08 15:40:33 +0000588\begin{datadescni}{XML_ERROR_RECURSIVE_ENTITY_REF}
589\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000590
Fred Drake5ed1dac2001-02-08 15:40:33 +0000591\begin{datadescni}{XML_ERROR_SYNTAX}
Fred Drakeacab3d62000-07-11 16:30:30 +0000592Some unspecified syntax error was encountered.
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_TAG_MISMATCH}
Fred Drakeacab3d62000-07-11 16:30:30 +0000596An end tag did not match the innermost open start tag.
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_UNCLOSED_TOKEN}
600\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000601
Fred Drake5ed1dac2001-02-08 15:40:33 +0000602\begin{datadescni}{XML_ERROR_UNDEFINED_ENTITY}
Fred Drakeacab3d62000-07-11 16:30:30 +0000603A reference was made to a entity which was not defined.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000604\end{datadescni}
Fred Drakeacab3d62000-07-11 16:30:30 +0000605
Fred Drake5ed1dac2001-02-08 15:40:33 +0000606\begin{datadescni}{XML_ERROR_UNKNOWN_ENCODING}
Fred Drakeacab3d62000-07-11 16:30:30 +0000607The document encoding is not supported by Expat.
Fred Drake5ed1dac2001-02-08 15:40:33 +0000608\end{datadescni}