The xml.parsers.expat module is a Python interface to the Expat
non-validating XML parser. The module provides a single extension type,
xmlparser, that represents the current state of an XML parser. After
an 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.
This module uses the pyexpat module to provide access to the Expat
parser. Direct use of the pyexpat module is deprecated.
This module provides one exception and one type object:
-
exception
xml.parsers.expat.ExpatError¶ The exception raised when Expat reports an error. See section ExpatError Exceptions for more information on interpreting Expat errors.
-
exception
xml.parsers.expat.error¶ Alias for
ExpatError.
-
xml.parsers.expat.XMLParserType¶ The type of the return values from the
ParserCreate()function.
The xml.parsers.expat module contains two functions:
-
xml.parsers.expat.ErrorString(errno)¶ Returns an explanatory string for a given error number errno.
-
xml.parsers.expat.ParserCreate([encoding[, namespace_separator]])¶ Creates and returns a new
xmlparserobject. 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. If encoding [1] is given it will override the implicit or explicit encoding of the document.Expat can optionally do XML namespace processing for you, enabled by providing a value for namespace_separator. The value must be a one-character string; a
ValueErrorwill be raised if the string has an illegal length (Noneis 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 handlersStartElementHandlerandEndElementHandlerwill be the concatenation of the namespace URI, the namespace separator character, and the local part of the name. If the namespace separator is a zero byte (chr(0)) then the namespace URI and the local part will be concatenated without any separator.For example, if namespace_separator is set to a space character (
' ') and the following document is parsed:<?xml version="1.0"?> <root xmlns = "http://default-namespace.org/" xmlns:py = "http://www.python.org/ns/"> <py:elem1 /> <elem2 xmlns="" /> </root>StartElementHandlerwill receive the following strings for each element:http://default-namespace.org/ root http://www.python.org/ns/ elem1 elem2
See also
- The Expat XML Parser
- Home page of the Expat project.
19.5.1. XMLParser Objects¶
xmlparser objects have the following methods:
-
xmlparser.Parse(data[, isfinal])¶ Parses the contents of the string data, calling the appropriate handler functions to process the parsed data. isfinal must be true on the final call to this method. data can be the empty string at any time.
-
xmlparser.ParseFile(file)¶ Parse XML data reading from the object file. file only needs to provide the
read(nbytes)method, returning the empty string when there’s no more data.
-
xmlparser.SetBase(base)¶ 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
ExternalEntityRefHandler(),NotationDeclHandler(), andUnparsedEntityDeclHandler()functions.
-
xmlparser.GetBase()¶ Returns a string containing the base set by a previous call to
SetBase(), orNoneifSetBase()hasn’t been called.
-
xmlparser.GetInputContext()¶ Returns the input data that generated the current event as a string. The data is in the encoding of the entity which contains the text. When called while an event handler is not active, the return value is
None.New in version 2.1:
New in version 2.1.
-
xmlparser.ExternalEntityParserCreate(context[, encoding])¶ Create a “child” parser which can be used to parse an external parsed entity referred to by content parsed by the parent parser. The context parameter should be the string passed to the
ExternalEntityRefHandler()handler function, described below. The child parser is created with theordered_attributes,returns_unicodeandspecified_attributesset to the values of this parser.
-
xmlparser.UseForeignDTD([flag])¶ Calling this with a true value for flag (the default) will cause Expat to call the
ExternalEntityRefHandlerwithNonefor all arguments to allow an alternate DTD to be loaded. If the document does not contain a document type declaration, theExternalEntityRefHandlerwill still be called, but theStartDoctypeDeclHandlerandEndDoctypeDeclHandlerwill not be called.Passing a false value for flag will cancel a previous call that passed a true value, but otherwise has no effect.
This method can only be called before the
Parse()orParseFile()methods are called; calling it after either of those have been called causesExpatErrorto be raised with thecodeattribute set toerrors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING.New in version 2.3:
New in version 2.3.