Reputation: 5299
I want to validate xml documents with JaXB.
Code :
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(new File("D:/liferay-develop/workspace/cat_test/v2/STD_MP.xsd")));
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);
Now i want to get error if document not valide to schema. In examples is see that guys create EventHandler classes for this ( like this http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html). But maybe exist more simplest way to get validation error in conlose and in variable?
Upvotes: 3
Views: 521
Reputation: 149057
The ValidationEventHandler
approach is how you get a handle on the validation events. You could do something like the following if you are looking to minimize the amount of code you need to write:
jaxbUnmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent event) {
System.out.println(event.getMessage());
return true;
}
});
Upvotes: 4