Java – 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;

//@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;

    public String getCompany() {
        return company;
    public void setCompany(String company) { = company;

    public String getScheme() {
        return scheme;
    public void setScheme(String scheme) {
        this.scheme = scheme;

    public String getAgreementNumber() {
        return agreementNumber;
    public void setAgreementNumber(String agreementNumber) {
        this.agreementNumber = agreementNumber;

My serialize code:

    CustomerPlan2 cp2 = new CustomerPlan2();

    JAXBContext context = JAXBContext.newInstance(CustomerPlan2.class);
    Marshaller marshaller = context.createMarshaller();

    marshaller.marshal(cp2, new FileWriter("C:\\temp\\out.xml"));



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


Thanks for any help on this.

Best Solution

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.