Name

parse

Synopsis

parse(file,handler,error_handler=None)

file is a filename or a file-like object open for reading, containing an XML document. handler is generally an instance of your own subclass of class ContentHandler, covered later in this chapter. error_handler, if given, is generally an instance of your own subclass of class ErrorHandler. You don’t necessarily have to subclass ContentHandler and/or ErrorHandler: you just need to provide the same interfaces as the classes do. Subclassing is often a convenient means to this end.

Function parse is equivalent to the code:

                           p = make_parser( )
p.setContentHandler(handler)
if error_handler is not None: 
    p.setErrorHandler(error_handler)
p.parse(file)

This idiom is quite frequent in SAX parsing, so having it in a single function is convenient. When error_handler is None, the parser diagnoses errors by propagating an exception that is an instance of some subclass of SAXException.

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.