How to get more specific errors when validating XML on XSD using java.xml.validator

After finding the best approach to validating my XML in XSD, I came across java.xml.validator.

I started by using the sample code from the API and adding my own ErrorHandler

// parse an XML document into a DOM tree DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = parser.parse(new File("instance.xml")); // create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // load a WXS schema, represented by a Schema instance Source schemaFile = new StreamSource(new File("mySchema.xsd")); Schema schema = factory.newSchema(schemaFile); // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); // Add a custom ErrorHandler validator.setErrorHandler(new XsdValidationErrorHandler()); // validate the DOM tree try { validator.validate(new DOMSource(document)); } catch (SAXException e) { // instance document is invalid! } ... private class XsdValidationErrorHandler implements ErrorHandler { @Override public void warning(SAXParseException exception) throws SAXException { throw new SAXException(exception.getMessage()); } @Override public void error(SAXParseException exception) throws SAXException { throw new SAXException(exception.getMessage()); } @Override public void fatalError(SAXParseException exception) throws SAXException { throw new SAXException(exception.getMessage()); } } 

This works fine, however, the message passed to my XsdValidationErrorHandler does not give me any indication of exactly where the XML-violating document is located:

 "org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'X'. One of '{Y}' is expected." 

Is there a way to override or mount another Validator section so that I can define my own error messages sent to ErrorHandler without having to rewrite all the code?

Should I use a different library?

+8
java xml validation xsd
source share
5 answers

Try to catch a SaxParseException, this is a descendant of a SaxException. If you get one of them, it has methods getLineNumber (), getColumnNumber (), etc.

+9
source share

Check at the time of parsing. This will make location information available, and ErrorHandler will report it.

Just create a Schema before creating the DocumentBuilderFactory and apply it as follows:

 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); dbf.setSchema(schema); 

Note. The setValidating() method tells DBF whether to use DTD validation. Schema setup tells him to use schema validation.

+3
source share

You can make exception.getLineNumber() and exception.getColumnNumber() to get the coordinates of the error. This question is similar.

+3
source share

Have you seen a SAXParseException ? I assume you are looking for information such as line number ? As @parcifel mentioned, you should check when you parse (more efficient and better error information). You will do this with a schema reference to a DocumentBuilderFactory .

+1
source share
 public class KitXsdErrorHandler implements ErrorHandler { @Override public void warning(SAXParseException exception) throws SAXException { handleMessage("Warning", exception); } @Override public void error(SAXParseException exception) throws SAXException { handleMessage("Error", exception); } @Override public void fatalError(SAXParseException exception) throws SAXException { handleMessage("Fatal", exception); } private String handleMessage(String level, SAXParseException exception) throws SAXException { int lineNumber = exception.getLineNumber(); int columnNumber = exception.getColumnNumber(); String message = exception.getMessage(); throw new SAXException("[" + level + "] Sətr nömrə: " + lineNumber + " Sütun nömrə: " + columnNumber + " message: " + message); } } 

This information is also helpful.

How to get internalization using SAX XML Schema Validator validation messages?

http://vijiprakash.blogspot.com/2008/05/custom-error-message-in-xml-xsd.html

0
source share

All Articles