Assuming I have a schema that describes a root element class Root that contains a List<Entry> where the Entry class has a required field name.
Here is how it looks in code:
@XmlRootElement
class Root{
@XmlElement(name="entry")
public List<Entry> entries = Lists.newArrayList();
}
@XmlRootElement
class Entry{
@XmlElement(name="name",required=true)
public String name;
}
If I supply the following XML for unmarshalling:
<root>
<entry>
<name>ekeren</name>
</entry>
<entry>
</entry>
</root>
I have a problem because the second entry does not contain a name. So unmarshall produces null.
Is there a way to customize JAXB to unmarshall a Root object that will only contain the “good” entry?
You could add the magic afterUnmarshal method to take care of empty entries:
EDIT:
Not sure if this is better suited for you, but maybe it’s of help. You could also use a Pacher, e.g. if not all objects you need to fix/validate your result are available in afterUnmarshal(..)
Here’s an example:
I wouldn’t abuse it too much though, not only as it is Sun-only API.
But if you’re really looking into something configurable that isn’t part of the code of the marshalled objects itself. It might be best to look at something after unmarshalling. I wonder if Bean Validation (JSR 303) wouldn’t be a perfect fit for you, e.g. using Hibernate Validator (don’t get intimidated by the name, you don’t need Hibernate ORM to use it). I haven’t used it myself, but using a (new) standard for validation sounds reasonable, doesn’t it?