Java Reference
In-Depth Information
@Override
public void warning(SAXParseException spe) {
System.out.println("Warning at line " + spe.getLineNumber());
System.out.println(spe.getMessage());
}
Directory "TrySAXHandler 3 with error reporting"
This outputs a message indicating the document line number where the error occurred. It also outputs
the string returned by the
getMessage()
method inherited from the base class,
SAXException
. This usually
indicates the nature of the error that was detected.
You could implement the
error()
callback method similarly, but you might want to implement
fatalError()
so that it throws an exception. For example:
@Override
public void fatalError(SAXParseException spe) throws SAXException {
System.out.println("Fatal error at line " + spe.getLineNumber());
System.out.println(spe.getMessage());
throw spe;
}
Directory "TrySAXHandler 3 with error reporting"
Here you just rethrow the
SAXParseException
after outputting an error message indicating the line num-
ber that caused the error. The
SAXParseException
class is a subclass of
SAXException,
so you can rethrow
spe
as the superclass type. Don't forget the
import
statements in the
MySAXHandler
source file for the
SAXException
and
SAXParseException
class names from the
org.xml.sax
package.
You could try these out with the previous example by adding them to the
MySAXHandler
class. You could
introduce a few errors into the XML file to get these methods called. Try deleting the
DOCTYPE
declaration
or deleting the forward slash on an element end tag, or even just deleting one character in an element name.
Parsing a Schema Instance Document
You can create a simple instance of a document that uses the
Sketcher.xsd
schema you developed earlier.
Here's the definition of the XML document contents:
<?xml version="1.0" encoding="UTF-8"?>
<sketch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:/D:/Beg%20Java%20Stuff/Sketcher.xsd">
<circle diameter="40" angle="0.0">
<color R="255" G="0" B="0"/>