PyXML features several parsers, and multiple ways to
instantiate them, depending on whether you’re using SAX, trying to
create a DOM tree, or doing something completely different. Designed for
portable code, a ParserFactory
class
is provided that supplies a SAX-ready parser guaranteed available in
your runtime environment. Additionally, you can explicitly create a
parser (or SAX driver) by dipping into any specific package, such as
PyExpat. We illustrate an example of both, but normally you should rely
on the parser factory to instantiate a parser.
The make_parser
function
(imported from xml.sax
) returns a SAX
driver for the first available parser in the list that you supply, or
returns an available parser if no list is specified or if the list
contains parsers that are not found or cannot be loaded. The make_parser
function has its roots as part of
the xml.sax.saxexts.ParserFactory
class, but it is better to import the method from xml.sax
(more on this in a bit). For
example:
from xml.sax import make_parser parser = make_parser( )
At the time of this writing, if you have PyXML installed, a call
to make_parser
without an argument is
sure to return either a PyExpat or xmlproc
driver. If you dig into the source of
the xml.sax
module, you will see this
list supplied to the ParserFactory
class. If you instantiate a parser factory directly out of xml.sax.saxexts
, you need to be sure to supply
a list containing the name of at least one valid parser, or it won’t be
able to create a parser:
>>> from xml.sax.saxexts import ParserFactory >>> p = ParserFactory( ) >>> parser = p.make_parser( ) Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/local/lib/python2.0/site-packages/_xmlplus/sax/saxexts.py", line 77, in make_parser raise SAXReaderNotAvailable("No parsers found", None) xml.sax._exceptions.SAXReaderNotAvailable: No parsers found
If you supply a list of parsers or drivers, you get what you’re after:
>>> from xml.sax.saxexts import ParserFactory >>> p = ParserFactory(["xml.sax.drivers.drv_pyexpat"]) >>> parser = p.make_parser( )
In most cases, it’s a good idea to use the make_parser
function from xml.sax
, but it’s also valuable to know what
is going on under the hood. Several factory classes are available, with
variations for HTML, SGML, non-validating XML, and validating XML
parsers.
3.141.192.120