Finding a parser, using the parser factory

You can find a SAX-compliant XML parser in one of three ways.

  1. Call a known parser directly, e.g.,

  2. Call the parser factory with the name of a known driver module:
    Note that there have been versions which require a somewhat different call:

  3. Call the parser factory without arguments, and let it find a parser for you:

The saxexts module defines a ParserFactory class. Upon import it makes an instance of it, called XMLParserFactory, which lists all known SAX-compliant XML parsers (actually it lists their driver modules). [It also makes instances with known validating XML parsers, HTML parsers and SGML parsers.]

A parser can be found by calling the module's make_parser method, which calls the XMLParserFactory's make_parser method. Then, for each of its listed parsers, the parser factory tries to load the driver module and tests whether it contains a working XML parser. If it finds one, the search is stopped; otherwise the next driver is tried.

If the make_parser method is called with a driver module as its argument, then only that module is tested.

Such a factory is very convenient for users, who need not be bothered with picking a specific parser, or installing yet another parser. This is only possible because all parsers present the same SAX interface to an application, and is therefore one of the achievements of the SAX specification.

Note that the make_parser method of the saxexts module delivers a well-formedness parser. If you want a validating parser, you must call specifically the make_parser method of the XML validating parser factory (currently it contains only one parser):