Antonio
Antonio

Reputation: 89

Jaxb Java: Ordering list of string

Is there a way to "force" the marshaller to respect the order of the elements inside the XmlElementsRefs annotation applied to a list of objects?

I have a Java Class which has an attribute that is a list of generic objects.

This is a snippet of the class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "rootClass")
public class RootClass implements Equals, HashCode, ToString {

  @XmlElementRefs({
     @XmlElementRef(name = "first-element", type = FirstElement.class, required = false),
        @XmlElementRef(name = "second-element", type = SecondElement.class, required = false),
        @XmlElementRef(name = "third-element", type = ThirdElement.class, required = false),
        [...]
     })
 protected List<Object> content;

// then getter, setter, equals, hashCode

The list content actually has 15 elements or more. I have put 3 @XmlElementRef just for simplicity.

Let's suppose that I add to the list content an instance of ThirdElement, then an instance of SecondElement and then an instance of FirstElement. I want the marshaller to create an XML that has first-element, then second-element and then third-element, no matter in which order I have added the elements to the list.

Just to give a bit of context, I am using the marshaller org.springframework.oxm.jaxb.Jaxb2Marshaller configured with the property jakarta.xml.bind.Marshaller.JAXB_FRAGMENT set to true.

Upvotes: 0

Views: 36

Answers (0)

Related Questions