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