Steven
Steven

Reputation: 3894

java xml validation: getting a better validation error description

I am using javax.xml.validation.Validator from SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) to perform XSD validation.

I would like to be able to get a better error message back for validation errors. All I currently get is: org.xml.sax.SAXParseException: cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type 'TypeCode'.

Is it possible to have the error print out which element or xml fragment is failing validation?

*Edit: I am using the standard java6 xml parsers *Edit2: Sorry for not mentioning - I am using JAXB to read the XML with a contenthandler and a listener (due to the XML being > 1GB most of the time).

Upvotes: 3

Views: 1971

Answers (2)

bdoughan
bdoughan

Reputation: 149007

Have you tried registering an ErrorHandler with your Validator and accessing the details on the corresponding SAXParseException?

Upvotes: 0

StaxMan
StaxMan

Reputation: 116512

Probably not with the validator (maybe one Xerces uses?) you are using. There are other validators, Sun's Multi-Schema Validator for example, but I don't know if any have particularly good error messages for schema validation. For DTD validation Woodstox has better exception messages, but for XML Schema validation it relies on MSV.

However; even the default parser/validator should be able to give you actual error location (or something nearby). If not with error message, you should at least be able to keep track of preceding locations by registering SAX listener, since validator you are using is using SAX API.

Upvotes: 1

Related Questions