JAXB Schema validation when unmarshalling non-root element

≯℡__Kan透↙ 提交于 2020-01-03 07:32:43

问题


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);

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'

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.


回答1:


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



来源:https://stackoverflow.com/questions/45733478/jaxb-schema-validation-when-unmarshalling-non-root-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!