Java Reference
In-Depth Information
You place the statement here rather than after the call to the setValidating() method because the
setFeature() method can throw an exception of type ParserConfigurationException and it needs to
be in a try block. Now the parser calls the startPrefixMapping() method at the beginning of each
namespace scope, and the endPrefixMapping() method at the end. If you parse this document, you see that
each of the qname values is the local name qualified by the namespace prefix. You should also see that the
start and end of the namespace scope are recorded in the output.
Handling Other Parsing Events
I have considered only events arising from the recognition of document content, those declared in the Con-
tentHandler interface. In fact, the DefaultHandler class defines do-nothing methods declared in the other
three interfaces that you saw earlier. For example, when a parsing error occurs, the parser calls a method
to report the error. Three methods for error reporting are declared in the ErrorHandler interface and are
implemented by the DefaultHandler class:
void warning(SAXParseException spe) : Called to notify conditions that are not errors or fatal
errors. The exception object, spe , contains information to enable you to locate the error in the ori-
ginal document.
void error(SAXParseException spe) : Called when an error has been identified. An error is
anything in a document that violates the XML specification but is recoverable and allows the pars-
er to continue processing the document normally.
void fatalError(SAXParseException spe) : Called when a fatal error has been identified. A
fatal error is a non-recoverable error. After a fatal error the parser does not continue normal pro-
cessing of the document.
Each of these methods is declared to throw an exception of type SAXException , but you don't have to
implement them so that they do this. With the warning() and error() methods you probably want to out-
put an error message and return to the parser so it can continue processing. Of course, if your fatalError()
method is called, processing of the document does not continue anyway, so it would be appropriate to throw
an exception in this case.
Obviously your implementation of any of these methods wants to make use of the information from the
SAXParseException object that is passed to the method. This object has four methods that you can call to
obtain additional information that help you locate the error:
int getLineNumber() : Returns the line number of the end of the document text where the error
occurred. If this is not available, -1 is returned.
int getColumnNumber() : Returns the column number within the document that contains the end
of the text where the error occurred. If this is not available, -1 is returned. The first column in a
line is column 1.
String getPublicID() : Returns the public identifier of the entity where the error occurred, or
null if no public identifier is available.
String getSystemID() : Returns the system identifier of the entity where the error occurred, or
null if no system identifier is available.
A simple implementation of the warning() method could be like this:
Search WWH ::




Custom Search