Marshalling a List of objects implementing a common interface, with JaxB

For this scenario I would recommend the use of @XmlElements. @XmlElements is used to represent the XML schema concept of choice:

  • http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html

Here is how it would look for your example:

@XmlElements({ 
    @XmlElement(name="girl", type=Girl.class),
    @XmlElement(name="boy", type=Boy.class)
})
@XmlElementWrapper
public List<Person> getPeople() {
    return people;
}

@XmlElementRef corresponds to the concept of substitution groups in XML schema. This is why the previous answer requires Person to be changed from an interface to a class.

  • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)