Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB and property ordering

I want the serialized XML output from my Java class to honor the ordering of the properties in the Java class.

It seems that JAXB orders alphabetically.

I can override this by using @XmlType with propOrder and specifying ALL of the properties, but I have a class with many properties and these are not yet finalized.

I read that specifying an empty propOrder would do it but it don't.

My example class:

package test;  import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType;  @XmlRootElement //@XmlType(propOrder={"company", "scheme", "agreementNumber"}) @XmlType(propOrder={}) // makes no difference - still alphabetical in XML  public class CustomerPlan2 {      private String company;     private String scheme;     private String agreementNumber;      @XmlElement(name="Company")     public String getCompany() {         return company;     }     public void setCompany(String company) {         this.company = company;     }      @XmlElement(name="Scheme")     public String getScheme() {         return scheme;     }     public void setScheme(String scheme) {         this.scheme = scheme;     }      @XmlElement(name="AgreementNumber")     public String getAgreementNumber() {         return agreementNumber;     }     public void setAgreementNumber(String agreementNumber) {         this.agreementNumber = agreementNumber;     } } 

My serialize code:

    CustomerPlan2 cp2 = new CustomerPlan2();      cp2.setCompany("company");     cp2.setScheme("scheme");     cp2.setAgreementNumber("agreementnumber");     JAXBContext context = JAXBContext.newInstance(CustomerPlan2.class);     Marshaller marshaller = context.createMarshaller();      marshaller.marshal(cp2, new FileWriter("C:\\temp\\out.xml")); 

Output:

    <customerPlan2>       <AgreementNumber>agreementnumber</AgreementNumber>        <Company>company</Company>        <Scheme>scheme</Scheme>      </customerPlan2> 

I want my output to be (as the property order of my class):

    <customerPlan2>       <Company>company</Company>       <Scheme>scheme</Scheme>        <AgreementNumber>agreementnumber</AgreementNumber>      </customerPlan2> 

Thanks for any help on this.

like image 723
andy hallam Avatar asked Mar 25 '11 16:03

andy hallam


People also ask

How do I define the XML element order in JAXB?

To define the element order we need to use the @XmlType annotation in our POJO. This annotation propOrder attribute is where we define what element should come first and which element should be place at the end.

What is required to be included in the propOrder element of the XmlType annotation?

The annotation element propOrder() in the @XmlType annotation allows you to specify the content order in the generated schema type. When you use the @XmlType. propOrder annotation on a class to specify content order, all public properties and public fields in the class must be specified in the parameter list.

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

What is JAXB marshalling?

JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.


Video Answer


2 Answers

It's possible using:

@XmlType (propOrder={"prop1","prop2",..."propN"}) 

Just uncomment the code like this:

//@XmlType(propOrder={"company", "scheme", "agreementNumber"}) 

This is the correct usage.

like image 105
VIVEK PANDIAN S Avatar answered Sep 18 '22 15:09

VIVEK PANDIAN S


Note: I lead EclipseLink JAXB (MOXy)

The order in which Java reflection returns the list of fields/properties is not guaranteed. This is why JAXB implementations do not use it to determine element order.

By default JAXB provides no guaranteed ordering. However most (if not all JAXB implementations) use alphabetical ordering since it is deterministic. To guarantee this ordering you must annotate your class as follows:

@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL) public class Foo {     ... } 
like image 24
bdoughan Avatar answered Sep 17 '22 15:09

bdoughan