0

I would like to ask on how to marshal an object with property order from a nested object.

@XmlRootElement(name = "sample")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"title", "code"})
public class SampleObject {

    @XmlAttribute(name = "title")
    private String title;
    

    @XmlAttribute(name = "code")
    private String code;
    
    

}

I have a wrapperList to set that object:

@XmlRootElement(name = "listWrapper")
@XmlAccessorType(XmlAccessType.FIELD)
public class WrapperObject {

    @XmlAnyElement(lax=true)
    private List<SampleObject> objectList;
    

}

And i want to set the list on this object. This object is the one who is being marshalled.

@XmlRootElement(name = "marshaller")
@XmlAccessorType(XmlAccessType.FIELD)
public class MarshallerObject {

    @XmlElement(name = "wrapperList")
    private WrapperObject objectList;
    

}

This is the output that i'm aiming for:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
            <marshaller>
               <wrapperList>
                  <sample title="sampleTitle code"001"/>
    
               </wrapperList>
            </marshaller>
</soap:Envelope>

Thanks in advance!

default001
  • 11
  • 2
  • 1
    Can you clarify what the specific problem is? You already have `@XmlType(propOrder = {"title", "code"})`. That should work fine for you (it works for me) for controlling the property order. – andrewJames May 22 '21 at 21:23
  • But first, you do need to remove the `@XmlAttribute` annotation from `private WrapperObject objectList;`. And you do need to fix the typo in ``. And just to note - there should be no logic which relies on the order of attributes in an element - it should not matter what the ordering is. – andrewJames May 22 '21 at 21:23
  • updated the code. propOrder is not working if my marshalled class is the marshallObject. It just comes back to alphabetical which is "code" "title". – default001 May 22 '21 at 22:16
  • Can you provide a [mre]? I cannot recreate the problem you are having. – andrewJames May 22 '21 at 22:23
  • Does this answer your question? [JAXB and property ordering](https://stackoverflow.com/questions/5435138/jaxb-and-property-ordering) – Lakshan May 27 '21 at 10:31

0 Answers0