document in not validated
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jeetendra_kumar
Posted On:   Thursday, May 10, 2001 04:10 AM

i am using jaxp1.0.1's DOM parser and trying to validate a document against a schema. i have set the namespaceaware and validating to true. To test whether it is really working i deliberately made an error into the document. The application doesn't throw any exception.It remains silent and works as a nonvalidating parser. Can you tell me why this is happening

Re: document in not validated

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());
...
About | Sitemap | Contact