0

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 ?

Matteo
  • 37,680
  • 11
  • 100
  • 115
somaniA
  • 614
  • 2
  • 7
  • 30

1 Answers1

2

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221