Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB and property ordering
    primarykey
    data
    text
    <p>I want the serialized XML output from my Java class to honor the ordering of the properties in the Java class.</p> <p>It seems that JAXB orders alphabetically.</p> <p>I can override this by using <code>@XmlType</code> with <em>propOrder</em> and specifying ALL of the properties, but I have a class with many properties and these are not yet finalized.</p> <p>I read that specifying an empty <em>propOrder</em> would do it but it don't.</p> <p>My example class:</p> <pre><code>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; } } </code></pre> <p>My serialize code:</p> <pre><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")); </code></pre> <p>Output:</p> <pre><code> &lt;customerPlan2&gt; &lt;AgreementNumber&gt;agreementnumber&lt;/AgreementNumber&gt; &lt;Company&gt;company&lt;/Company&gt; &lt;Scheme&gt;scheme&lt;/Scheme&gt; &lt;/customerPlan2&gt; </code></pre> <p>I want my output to be (as the property order of my class):</p> <pre><code> &lt;customerPlan2&gt; &lt;Company&gt;company&lt;/Company&gt; &lt;Scheme&gt;scheme&lt;/Scheme&gt; &lt;AgreementNumber&gt;agreementnumber&lt;/AgreementNumber&gt; &lt;/customerPlan2&gt; </code></pre> <p>Thanks for any help on this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload