1
我有这样的方法:JAXB定制验证
private static <T> T readXML(final String file, final String schemaFile, final Class<T> clazz) {
try {
final JAXBContext context = JAXBContext.newInstance(clazz);
final Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setEventHandler(new EventHandler());
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final String directory = System.getProperty("schema.dir");
unmarshaller.setSchema(schemaFactory.newSchema(new File(directory, schemaFile)));
return clazz.cast(unmarshaller.unmarshal(new File(directory, file)));
} catch (final Exception ex) {
ex.printStackTrace();
}
System.exit(1);
return null;
}
,我使用它是这样的:
private static final Primitives primitives = readXML("primitives.xml", "primitives.xsd", Primitives.class);
我的问题是,是否有可能自定义验证添加到解组? 我的意思是让我说我有一个名为Foo的complexType。我想让它的一些验证,我不能在XSD文件描述,所以我想要做这样的事情:
unmarshaller.setCustomValidator(Foo.class, new MyCustomValidator());
所以MyCustomValidator将被称为每次JAXB读取富。 或者如果我无法在xsd文件中描述某些验证,那么比在JAXB读取完成后必须验证已解析的类?
哦,顺便说一句...我从xsd与xjc生成了JAXB类。
我说我想要一个cutom验证器。 schema.newValidator()根据xsd进行验证。我想做一些其他的验证,而不是在xsd中定义的。 – ffddani