Posted By:
AlessandroA_Garbagnati
Posted On:
Thursday, May 10, 2001 09:46 AM
Hi,
The default error handler of jaxp it considers a validation error as a 'not-fatal' error, not throwing any Exception.
To make it work in the way you want (and the way it should be) you need to define your own error handler. The simplest way to do it is to create a class that implements ErrorHandler and, for example, consider each error as a fatal error:
import org.xml.sax.*;
public class XMLParserErrorHandler implements ErrorHandler {
// fatal error
public void fatalError(SAXParseException ex) throws SAXException {
throw new SAXException("Fatal error", ex);
}
// normal error
public void error(SAXParseException ex) throws SAXException {
throw new SAXException("Validation error", ex);
}
// warnings
public void warning(SAXParseException ex) throws SAXException {
throw new SAXException("Warning", ex);
}
}
Then in your code you just need to define this handler:
...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new XMLParserErrorHandler());
...