2000-09-23 01:47:56 -03:00
|
|
|
\section{\module{xml.parsers.expat} ---
|
2000-10-29 01:10:30 -04:00
|
|
|
Fast XML parsing using Expat}
|
2000-06-10 23:42:07 -03:00
|
|
|
|
2000-09-23 01:47:56 -03:00
|
|
|
\declaremodule{standard}{xml.parsers.expat}
|
|
|
|
\modulesynopsis{An interface to the Expat non-validating XML parser.}
|
2000-06-10 23:42:07 -03:00
|
|
|
\moduleauthor{Paul Prescod}{paul@prescod.net}
|
|
|
|
\sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
|
|
|
|
|
2000-09-23 01:47:56 -03:00
|
|
|
\versionadded{2.0}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
The \module{xml.parsers.expat} module is a Python interface to the
|
|
|
|
Expat\index{Expat} non-validating XML parser.
|
2000-06-10 23:42:07 -03:00
|
|
|
The module provides a single extension type, \class{xmlparser}, that
|
|
|
|
represents the current state of an XML parser. After an
|
|
|
|
\class{xmlparser} object has been created, various attributes of the object
|
|
|
|
can be set to handler functions. When an XML document is then fed to
|
|
|
|
the parser, the handler functions are called for the character data
|
|
|
|
and markup in the XML document.
|
2000-09-23 01:47:56 -03:00
|
|
|
|
|
|
|
This module uses the \module{pyexpat}\refbimodindex{pyexpat} module to
|
|
|
|
provide access to the Expat parser. Direct use of the
|
|
|
|
\module{pyexpat} module is deprecated.
|
2000-10-29 01:10:30 -04:00
|
|
|
|
|
|
|
This module provides one exception and one type object:
|
|
|
|
|
|
|
|
\begin{excdesc}{error}
|
|
|
|
The exception raised when Expat reports an error.
|
|
|
|
\end{excdesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XMLParserType}
|
|
|
|
The type of the return values from the \function{ParserCreate()}
|
|
|
|
function.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
|
2000-09-23 01:47:56 -03:00
|
|
|
The \module{xml.parsers.expat} module contains two functions:
|
2000-06-10 23:42:07 -03:00
|
|
|
|
|
|
|
\begin{funcdesc}{ErrorString}{errno}
|
|
|
|
Returns an explanatory string for a given error number \var{errno}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{funcdesc}{ParserCreate}{\optional{encoding\optional{,
|
|
|
|
namespace_separator}}}
|
2000-06-10 23:42:07 -03:00
|
|
|
Creates and returns a new \class{xmlparser} object.
|
|
|
|
\var{encoding}, if specified, must be a string naming the encoding
|
|
|
|
used by the XML data. Expat doesn't support as many encodings as
|
|
|
|
Python does, and its repertoire of encodings can't be extended; it
|
|
|
|
supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII.
|
|
|
|
|
|
|
|
Expat can optionally do XML namespace processing for you, enabled by
|
2000-10-29 01:10:30 -04:00
|
|
|
providing a value for \var{namespace_separator}. The value must be a
|
|
|
|
one-character string; a \exception{ValueError} will be raised if the
|
|
|
|
string has an illegal length (\code{None} is considered the same as
|
|
|
|
omission). When namespace processing is enabled, element type names
|
|
|
|
and attribute names that belong to a namespace will be expanded. The
|
|
|
|
element name passed to the element handlers
|
2000-06-10 23:42:07 -03:00
|
|
|
\function{StartElementHandler()} and \function{EndElementHandler()}
|
|
|
|
will be the concatenation of the namespace URI, the namespace
|
|
|
|
separator character, and the local part of the name. If the namespace
|
2000-10-29 01:10:30 -04:00
|
|
|
separator is a zero byte (\code{chr(0)}) then the namespace URI and
|
|
|
|
the local part will be concatenated without any separator.
|
2000-06-10 23:42:07 -03:00
|
|
|
|
2000-11-28 02:38:22 -04:00
|
|
|
For example, if \var{namespace_separator} is set to a space character
|
|
|
|
(\character{ }) and the following document is parsed:
|
2000-06-10 23:42:07 -03:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
<?xml version="1.0"?>
|
|
|
|
<root xmlns = "http://default-namespace.org/"
|
|
|
|
xmlns:py = "http://www.python.org/ns/">
|
|
|
|
<py:elem1 />
|
|
|
|
<elem2 xmlns="" />
|
|
|
|
</root>
|
|
|
|
\end{verbatim}
|
|
|
|
|
2000-09-25 11:14:30 -03:00
|
|
|
\function{StartElementHandler()} will receive the following strings
|
|
|
|
for each element:
|
2000-06-10 23:42:07 -03:00
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
http://default-namespace.org/ root
|
|
|
|
http://www.python.org/ns/ elem1
|
|
|
|
elem2
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\class{xmlparser} objects have the following methods:
|
|
|
|
|
2000-11-28 02:38:22 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{Parse}{data\optional{, isfinal}}
|
2000-06-10 23:42:07 -03:00
|
|
|
Parses the contents of the string \var{data}, calling the appropriate
|
|
|
|
handler functions to process the parsed data. \var{isfinal} must be
|
2000-07-04 23:03:34 -03:00
|
|
|
true on the final call to this method. \var{data} can be the empty
|
|
|
|
string at any time.
|
2000-06-10 23:42:07 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{ParseFile}{file}
|
2000-06-10 23:42:07 -03:00
|
|
|
Parse XML data reading from the object \var{file}. \var{file} only
|
|
|
|
needs to provide the \method{read(\var{nbytes})} method, returning the
|
|
|
|
empty string when there's no more data.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{SetBase}{base}
|
2000-06-10 23:42:07 -03:00
|
|
|
Sets the base to be used for resolving relative URIs in system identifiers in
|
|
|
|
declarations. Resolving relative identifiers is left to the application:
|
|
|
|
this value will be passed through as the base argument to the
|
|
|
|
\function{ExternalEntityRefHandler}, \function{NotationDeclHandler},
|
|
|
|
and \function{UnparsedEntityDeclHandler} functions.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{GetBase}{}
|
2000-06-10 23:42:07 -03:00
|
|
|
Returns a string containing the base set by a previous call to
|
|
|
|
\method{SetBase()}, or \code{None} if
|
|
|
|
\method{SetBase()} hasn't been called.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
|
2000-09-25 11:14:30 -03:00
|
|
|
\class{xmlparser} objects have the following attributes:
|
2000-08-17 20:15:21 -03:00
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{memberdesc}[xmlparser]{returns_unicode}
|
2000-08-17 20:15:21 -03:00
|
|
|
If this attribute is set to 1, the handler functions will be passed
|
|
|
|
Unicode strings. If \member{returns_unicode} is 0, 8-bit strings
|
|
|
|
containing UTF-8 encoded data will be passed to the handlers.
|
2000-10-29 01:10:30 -04:00
|
|
|
\end{memberdesc}
|
2000-08-17 20:15:21 -03:00
|
|
|
|
|
|
|
The following attributes contain values relating to the most recent
|
|
|
|
error encountered by an \class{xmlparser} object, and will only have
|
|
|
|
correct values once a call to \method{Parse()} or \method{ParseFile()}
|
2000-09-23 01:47:56 -03:00
|
|
|
has raised a \exception{xml.parsers.expat.error} exception.
|
2000-06-10 23:42:07 -03:00
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{memberdesc}[xmlparser]{ErrorByteIndex}
|
2000-06-10 23:42:07 -03:00
|
|
|
Byte index at which an error occurred.
|
2000-10-29 01:10:30 -04:00
|
|
|
\end{memberdesc}
|
2000-06-10 23:42:07 -03:00
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{memberdesc}[xmlparser]{ErrorCode}
|
2000-06-10 23:42:07 -03:00
|
|
|
Numeric code specifying the problem. This value can be passed to the
|
|
|
|
\function{ErrorString()} function, or compared to one of the constants
|
2000-09-23 01:47:56 -03:00
|
|
|
defined in the \module{errors} object.
|
2000-10-29 01:10:30 -04:00
|
|
|
\end{memberdesc}
|
2000-06-10 23:42:07 -03:00
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{memberdesc}[xmlparser]{ErrorColumnNumber}
|
2000-06-10 23:42:07 -03:00
|
|
|
Column number at which an error occurred.
|
2000-10-29 01:10:30 -04:00
|
|
|
\end{memberdesc}
|
2000-06-10 23:42:07 -03:00
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{memberdesc}[xmlparser]{ErrorLineNumber}
|
2000-06-10 23:42:07 -03:00
|
|
|
Line number at which an error occurred.
|
2000-10-29 01:10:30 -04:00
|
|
|
\end{memberdesc}
|
2000-06-10 23:42:07 -03:00
|
|
|
|
|
|
|
Here is the list of handlers that can be set. To set a handler on an
|
2000-07-04 23:03:34 -03:00
|
|
|
\class{xmlparser} object \var{o}, use
|
|
|
|
\code{\var{o}.\var{handlername} = \var{func}}. \var{handlername} must
|
|
|
|
be taken from the following list, and \var{func} must be a callable
|
|
|
|
object accepting the correct number of arguments. The arguments are
|
|
|
|
all strings, unless otherwise stated.
|
2000-06-10 23:42:07 -03:00
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{StartElementHandler}{name, attributes}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for the start of every element. \var{name} is a string
|
|
|
|
containing the element name, and \var{attributes} is a dictionary
|
|
|
|
mapping attribute names to their values.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{EndElementHandler}{name}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for the end of every element.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{ProcessingInstructionHandler}{target, data}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for every processing instruction.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{CharacterDataHandler}{data}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for character data.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{UnparsedEntityDeclHandler}{entityName, base,
|
|
|
|
systemId, publicId,
|
|
|
|
notationName}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for unparsed (NDATA) entity declarations.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{NotationDeclHandler}{notationName, base,
|
|
|
|
systemId, publicId}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for notation declarations.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{StartNamespaceDeclHandler}{prefix, uri}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called when an element contains a namespace declaration.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{EndNamespaceDeclHandler}{prefix}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called when the closing tag is reached for an element
|
|
|
|
that contained a namespace declaration.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{CommentHandler}{data}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for comments.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{StartCdataSectionHandler}{}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called at the start of a CDATA section.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{EndCdataSectionHandler}{}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called at the end of a CDATA section.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{DefaultHandler}{data}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for any characters in the XML document for
|
|
|
|
which no applicable handler has been specified. This means
|
|
|
|
characters that are part of a construct which could be reported, but
|
|
|
|
for which no handler has been supplied.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{DefaultHandlerExpand}{data}
|
2000-06-10 23:42:07 -03:00
|
|
|
This is the same as the \function{DefaultHandler},
|
|
|
|
but doesn't inhibit expansion of internal entities.
|
|
|
|
The entity reference will not be passed to the default handler.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{NotStandaloneHandler}{}
|
2000-09-25 11:14:30 -03:00
|
|
|
Called if the XML document hasn't been declared as being a standalone
|
|
|
|
document.
|
2000-06-10 23:42:07 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-10-29 01:10:30 -04:00
|
|
|
\begin{methoddesc}[xmlparser]{ExternalEntityRefHandler}{context, base,
|
|
|
|
systemId, publicId}
|
2000-06-10 23:42:07 -03:00
|
|
|
Called for references to external entities.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
|
2000-09-23 01:47:56 -03:00
|
|
|
\subsection{Example \label{expat-example}}
|
2000-06-10 23:42:07 -03:00
|
|
|
|
2000-07-04 23:03:34 -03:00
|
|
|
The following program defines three handlers that just print out their
|
2000-06-10 23:42:07 -03:00
|
|
|
arguments.
|
|
|
|
|
|
|
|
\begin{verbatim}
|
2000-09-23 01:47:56 -03:00
|
|
|
import xml.parsers.expat
|
2000-06-10 23:42:07 -03:00
|
|
|
|
|
|
|
# 3 handler functions
|
|
|
|
def start_element(name, attrs):
|
|
|
|
print 'Start element:', name, attrs
|
|
|
|
def end_element(name):
|
|
|
|
print 'End element:', name
|
|
|
|
def char_data(data):
|
|
|
|
print 'Character data:', repr(data)
|
|
|
|
|
2000-09-23 01:47:56 -03:00
|
|
|
p = xml.parsers.expat.ParserCreate()
|
2000-06-10 23:42:07 -03:00
|
|
|
|
|
|
|
p.StartElementHandler = start_element
|
2000-09-23 01:47:56 -03:00
|
|
|
p.EndElementHandler = end_element
|
|
|
|
p.CharacterDataHandler = char_data
|
2000-06-10 23:42:07 -03:00
|
|
|
|
|
|
|
p.Parse("""<?xml version="1.0"?>
|
|
|
|
<parent id="top"><child1 name="paul">Text goes here</child1>
|
|
|
|
<child2 name="fred">More text</child2>
|
|
|
|
</parent>""")
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
The output from this program is:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
Start element: parent {'id': 'top'}
|
|
|
|
Start element: child1 {'name': 'paul'}
|
|
|
|
Character data: 'Text goes here'
|
|
|
|
End element: child1
|
|
|
|
Character data: '\012'
|
|
|
|
Start element: child2 {'name': 'fred'}
|
|
|
|
Character data: 'More text'
|
|
|
|
End element: child2
|
|
|
|
Character data: '\012'
|
|
|
|
End element: parent
|
|
|
|
\end{verbatim}
|
2000-07-04 23:03:34 -03:00
|
|
|
|
|
|
|
|
2000-09-23 01:47:56 -03:00
|
|
|
\subsection{Expat error constants \label{expat-errors}}
|
2000-07-04 23:03:34 -03:00
|
|
|
\sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
|
|
|
|
|
|
|
|
The following table lists the error constants in the
|
2000-09-23 01:47:56 -03:00
|
|
|
\code{errors} object of the \module{xml.parsers.expat} module. These
|
|
|
|
constants are useful in interpreting some of the attributes of the
|
|
|
|
parser object after an error has occurred.
|
2000-07-04 23:03:34 -03:00
|
|
|
|
2000-09-23 01:47:56 -03:00
|
|
|
The \code{errors} object has the following attributes:
|
2000-07-04 23:03:34 -03:00
|
|
|
|
2000-07-11 13:30:30 -03:00
|
|
|
\begin{datadesc}{XML_ERROR_ASYNC_ENTITY}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_BAD_CHAR_REF}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_BINARY_ENTITY_REF}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_DUPLICATE_ATTRIBUTE}
|
|
|
|
An attribute was used more than once in a start tag.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_INCORRECT_ENCODING}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_INVALID_TOKEN}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_JUNK_AFTER_DOC_ELEMENT}
|
|
|
|
Something other than whitespace occurred after the document element.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_MISPLACED_XML_PI}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_NO_ELEMENTS}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_NO_MEMORY}
|
|
|
|
Expat was not able to allocate memory internally.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_PARAM_ENTITY_REF}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_PARTIAL_CHAR}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_RECURSIVE_ENTITY_REF}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_SYNTAX}
|
|
|
|
Some unspecified syntax error was encountered.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_TAG_MISMATCH}
|
|
|
|
An end tag did not match the innermost open start tag.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_UNCLOSED_TOKEN}
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_UNDEFINED_ENTITY}
|
|
|
|
A reference was made to a entity which was not defined.
|
|
|
|
\end{datadesc}
|
|
|
|
|
|
|
|
\begin{datadesc}{XML_ERROR_UNKNOWN_ENCODING}
|
|
|
|
The document encoding is not supported by Expat.
|
|
|
|
\end{datadesc}
|