Now, try running the above code with this file as input:
<collection> &foo; <comic title="Sandman" number='62'> </collection>
The &foo;
entity is unknown, and the comic
element
isn't closed (if it was empty, there would be a "/" before the
closing ">". As a result, you get a
SAXParseException, e.g.
xml.sax._exceptions<.SAXParseException: undefined entity at None:2:2
The default code for the ErrorHandler interface automatically
raises an exception for any error; if that is what you want, you don't
need to implement an error handler class at all. Otherwise, you can
provide your own version of the ErrorHandler interface, at
minimum overriding the error() and fatalError()
methods. The minimal implementation for each method can be a single
line. The methods in the ErrorHandler
interface--warning(), error(), and
fatalError()--are all passed a single argument, an exception
instance. The exception will always be a subclass of
SAXException, and calling str()
on it will produce
a readable error message explaining the problem.
For example, if you just want to continue running if a recoverable error occurs, simply define the error() method to print the exception it's passed:
def error(self, exception): import sys sys.stderr.write("\%s\n" \% exception)
With this definition, non-fatal errors will result in an error message, whereas fatal errors will continue to produce a traceback.