解组非根元素时的JAXB模式验证 [英] JAXB Schema validation when unmarshalling non-root element

查看:68
本文介绍了解组非根元素时的JAXB模式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用JAXB解组整个XML时,可以设置XML模式以在解析期间启用验证:

When you unmarshal whole XML with JAXB, you can set XML schema to enable validation during parsing:

//javax.xml.validation.Schema schema = ...
jaxbUnmarshaller = JAXBContext.newInstance(SomeRootType.class).createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);

另一方面,当您解组 NestedObjest ,逐个(例如,减少内存使用)此方法失败(因为Schema只接受root),但有异常:

On the other hand, when you unmarshal list of NestedObjests from XML, one by one (eg. to decrease memory usage) this method fails (as Schema only accepts root) with an exception:

org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 22; cvc-elt.1: Cannot find the declaration of element 'NestedObject'

即使 NestedObjectType 在XSD中定义良好。是否有任何选项可以启用嵌套对象验证?请注意,定义新模式是一个悲惨的选择,因为XSD是我的应用程序外部维护的其他人。

It fails even if NestedObjectType is well-defined in the XSD. Is there any option to enable nested-object validation? Please note that defining new schema is a miserable option as the XSD is external for my application maintained by someone else.

推荐答案

你有没有试图将绑定bean的类传递给 unmarshal 方法?如在示例65中所述。解组为已知类型

Have you tried to pass to unmarshal method the class of the bound bean ? Like explained in Example 65. Unmarshalling into a known type

这篇关于解组非根元素时的JAXB模式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆