tags:

views:

160

answers:

2

We have this JAXB annotation:

 @XmlElement(name = "Strategy", required = true)
 protected List<Strategy> strategy;

If there are no Strategy elements present, no exception is thrown.. why is this? Shouldn't we get an exception?

+4  A: 

The JAXB reference implementation doesn't use this attribute for validation, it's purely there for documentation purposes.

If you need to validate the documents, you need to define an XML Schema, and inject it into the Marshaller or Unmarshaller, using SchemaFactory.

skaffman
A: 

As written above true validation is only done from a schema.

If you wish to first create your class and still enjoy schema validation without writing your schema to disk you can create it from the class at run-time and use it for validating when you unmarshal here is an example:

http://stackoverflow.com/questions/2603778/how-can-i-unmarshall-in-jaxb-and-enjoy-the-schema-validation-without-using-an-exp/2614278#2614278

ekeren