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 in case of
an error, you don't need to change the error handler. Otherwise, you
should provide your own version of the ErrorHandler interface,
and at minimum override 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.
So, to re-implement a variant of ErrorRaiser, simply define one of the three methods to print the exception they're 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.