Search code examples
javajaxbxml-binding

JAXB : Order of elements in multiple lists


I have two lists in my root element object. List<Person> persons and List<Address> addresses . When i marshall this , it prints first all the person and then all the addresses . I want to print it one by one . Person then address , person and address and so on . How can i do that in JAXB ?


Solution

  • You can use @XmlElements or @XmlElementRefs.

    Assuming neither Person extends Address nor vice versa, the code will be something like:

    @XmlElements {
        @XmlElement(name="Person", type=Person.class),
        @XmlElement(name="Address", type=Address.class)
    }
    private List<Object> personOrAddress;
    

    However consider remodelling it as a special type like PointOfContact so that you don't have a heterogeneous property.