-\section{\module{pyexpat} ---
- Fast XML parsing using the Expat C library}
+\section{\module{xml.parsers.expat} ---
+ Fast XML parsing using the Expat library}
-\declaremodule{builtin}{pyexpat}
-\modulesynopsis{An interface to the Expat XML parser.}
+\declaremodule{standard}{xml.parsers.expat}
+\modulesynopsis{An interface to the Expat non-validating XML parser.}
\moduleauthor{Paul Prescod}{paul@prescod.net}
\sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
-The \module{pyexpat} module is a Python interface to the Expat
+\versionadded{2.0}
+
+The \module{xml.parsers.expat} module is a Python interface to the Expat
non-validating XML parser.
The module provides a single extension type, \class{xmlparser}, that
represents the current state of an XML parser. After an
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.
+
+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.
-The \module{pyexpat} module contains two functions:
+The \module{xml.parsers.expat} module contains two functions:
\begin{funcdesc}{ErrorString}{errno}
Returns an explanatory string for a given error number \var{errno}.
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()}
-has raised a \exception{pyexpat.error} exception.
+has raised a \exception{xml.parsers.expat.error} exception.
\begin{datadesc}{ErrorByteIndex}
Byte index at which an error occurred.
\begin{datadesc}{ErrorCode}
Numeric code specifying the problem. This value can be passed to the
\function{ErrorString()} function, or compared to one of the constants
-defined in the \module{pyexpat.errors} submodule.
+defined in the \module{errors} object.
\end{datadesc}
\begin{datadesc}{ErrorColumnNumber}
\end{methoddesc}
-\subsection{Example \label{pyexpat-example}}
+\subsection{Example \label{expat-example}}
The following program defines three handlers that just print out their
arguments.
\begin{verbatim}
-
-import pyexpat
+import xml.parsers.expat
# 3 handler functions
def start_element(name, attrs):
def char_data(data):
print 'Character data:', repr(data)
-p=pyexpat.ParserCreate()
+p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = start_element
-p.EndElementHandler = end_element
-p.CharacterDataHandler= char_data
+p.EndElementHandler = end_element
+p.CharacterDataHandler = char_data
p.Parse("""<?xml version="1.0"?>
<parent id="top"><child1 name="paul">Text goes here</child1>
\end{verbatim}
-\section{\module{pyexpat.errors} --- Error constants}
-
-\declaremodule{builtin}{pyexpat.errors}
-\modulesynopsis{Error constants defined for the Expat parser}
-\moduleauthor{Paul Prescod}{paul@prescod.net}
+\subsection{Expat error constants \label{expat-errors}}
\sectionauthor{A.M. Kuchling}{amk1@bigfoot.com}
The following table lists the error constants in the
-\module{pyexpat.errors} submodule, available once the
-\refmodule{pyexpat} module has been imported.
-
-Note that this module cannot be imported directly until
-\refmodule{pyexpat} has been imported.
+\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.
-The following constants are defined:
+The \code{errors} object has the following attributes:
\begin{datadesc}{XML_ERROR_ASYNC_ENTITY}
\end{datadesc}
\begin{datadesc}{XML_ERROR_UNKNOWN_ENCODING}
The document encoding is not supported by Expat.
\end{datadesc}
-
# XXX TypeErrors on calling handlers, or on bad return values from a
# handler, are obscure and unhelpful.
-import pyexpat
+from xml.parsers import expat
class Outputter:
def StartElementHandler(self, name, attrs):
print "Not OK."
out = Outputter()
-parser = pyexpat.ParserCreate(namespace_separator='!')
+parser = expat.ParserCreate(namespace_separator='!')
# Test getting/setting returns_unicode
parser.returns_unicode = 0; confirm(parser.returns_unicode == 0)
parser.returns_unicode = 0
try:
parser.Parse(data, 1)
-except pyexpat.error:
- print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
+except expat.error:
+ print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
print '** Line', parser.ErrorLineNumber
print '** Column', parser.ErrorColumnNumber
print '** Byte', parser.ErrorByteIndex
# Try the parse again, this time producing Unicode output
-parser = pyexpat.ParserCreate(namespace_separator='!')
+parser = expat.ParserCreate(namespace_separator='!')
parser.returns_unicode = 1
for name in HANDLER_NAMES:
setattr(parser, name, getattr(out, name))
try:
parser.Parse(data, 1)
-except pyexpat.error:
- print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
+except expat.error:
+ print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
print '** Line', parser.ErrorLineNumber
print '** Column', parser.ErrorColumnNumber
print '** Byte', parser.ErrorByteIndex
# Try parsing a file
-parser = pyexpat.ParserCreate(namespace_separator='!')
+parser = expat.ParserCreate(namespace_separator='!')
parser.returns_unicode = 1
for name in HANDLER_NAMES:
file = StringIO.StringIO(data)
try:
parser.ParseFile(file)
-except pyexpat.error:
- print '** Error', parser.ErrorCode, pyexpat.ErrorString(parser.ErrorCode)
+except expat.error:
+ print '** Error', parser.ErrorCode, expat.ErrorString(parser.ErrorCode)
print '** Line', parser.ErrorLineNumber
print '** Column', parser.ErrorColumnNumber
print '** Byte', parser.ErrorByteIndex