0

In my project, I would like to achieve specific order of properties in XML. In java object is represented as:

public class Plan {
    private List<Act> act;
    private List<Leg> leg;
    ...

An output object should look similar to this one:

   <plan>
        <act x="-25000" y="0"/>
        <leg mode="car">
              ...
        </leg>

        <act x="10000" y="0"/>
        <leg mode="car">
              ...
        </leg>
    </plan>

Is JAXB able to set up specific order for such case where I need to put items in order:

   act.get(0)
   leg.get(0)

   act.get(1)
   leg.get(1)
   ...
   ..
   .

I know JAXB is able to save specific order of parameters like firstly act, then all legs, using @XmlType (propOrder={"prop1","prop2",..."propN"}) but it is not the case of this project as the 3rd party application which reads this xml's read them in pairs and propOrder would print them one by another.

Alex R
  • 175
  • 1
  • 12

1 Answers1

0

Ok, I approach the problem from a different side and I solved it... Previously I thought it was a sorting problem - in fact, it is a problem with Java POJO class construction and JAXB annotations.

The solution for that is to use

    @XmlElements({
            @XmlElement(name="leg", type=Leg.class),
            @XmlElement(name="act", type=Act.class)
    })
    @XmlElementWrapper(name="plan")
    public List<Plan> getPlan() {
        return plan;
    }

and then items must be put one by another.

more details can be found in this answer: Marshalling a List of objects implementing a common interface, with JaxB

Alex R
  • 175
  • 1
  • 12