11
votes

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
could you provide some sample input, where this error occurs?Mr.Arjun

1 Answers

0
votes

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