JAXB Validator does not detect syntax errors?



I want to validate an xml file with its xsd before unmarshalling it.


The code is as follows :



SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(xsdFilePath);
Validator validator = schema.newValidator();
validator.setErrorHandler(new MyValidationErrorHandler());
validator.validate(new StreamSource(xmlFilePath));


I found that when an xml element is not closed, Validator failed to record it as an error, But the UnMarshaller recognizes this and throws an "Invalid content was found starting with element.." Error.


I want the Validation and the Unmarshalling/Marshalling to be different operations.


Are there ways to have the Validator detect such syntax errors in the xml file?


No comments:

Post a Comment