We have a large number of classes using JAXB for serialization and have not employed any validations for them during marshaling or unmarshalling process. Been exploring on JAXB in built mechanism for validation using set schema method. Now since I have a large number of JAXB model classes, generating schema for bulk of classes would result in complex schema. Trying to figure out to see if there is an ideal option through which validation can be performed for a specific class only in the schema and ignore the rest ?
I have the following lines of code,
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema;
JAXBContext jc;
try {
jc = JAXBContext.newInstance(MyClass.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
schema = sf.newSchema(new File("c:\\schema1.xsd"));
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent event) {
if (event == null) {
return true;
}
String line = "unknown"; //$NON-NLS-1$
String col = "unknown"; //$NON-NLS-1$
ValidationEventLocator locator = event.getLocator();
if (locator != null) {
line = Integer.toString(locator.getLineNumber());
col = Integer.toString(locator.getColumnNumber());
}
return true;
}
});
unmarshaller.unmarshal(new File("C:\\Temp\\Test\\Test.xml"));
I have auto generated schema1.xsd for MyClass.java by following the link Is it possible to generate a XSD from a JAXB-annotated class? Now, in my case there are numerous other model JAXB classes involved apart from MyClass.java, so event gets raised for all those indicating an error which I do not want. How can i want to restrict the schema for a single JAXB class only ? Is that something possible ? Thanks in advance