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 NestedObjest
s 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.