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:

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.

Leave a Comment