Team lead for the TopLink/EclipseLink JAXB & SDO implementations, and the Oracle representative on those specifications. Blaise is a DZone MVB and is not an employee of DZone and has posted 37 posts at DZone. You can read more from them at their website. View Full User Profile

JAXB's @XmlType and propOrder

02.14.2012
| 5498 views |
  • submit to reddit

In this post I will demonstrate how to use the propOrder property on the @XmlType annotation to control the ordering of XML elements.  I will also discuss the impact of @XmlAccessorType on how propOrder is configured.

Property (Public) Access
When property (method) access is used, the entries in the propOrder attribute correspond to the property names.  Note that in this example the property names are different from the underlying field names.
Domain Model
package blog.xmltype.proporder;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder={"ID", "firstName", "lastName"})
public class Customer {

 private String firstName;
 private String last_name;
 private int id;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return last_name;
 }

 public void setLastName(String last_name) {
  this.last_name = last_name;
 }

 public int getID() {
  return id;
 }

 public void setID(int id) {
  this.id = id;
 }

}

XML Output
Below is the XML output based on the above model.

<?xml version="1.0" encoding="UTF-8"?>
<customer>
    <ID>123</ID>
    <firstName>Jane</firstName>
    <lastName>Doe</lastName>
</customer>

Field Access
When field (instance variable) access is used, the entries in the propOrder attribute correspond to the field names.

Domain Model
package blog.xmltype.proporder;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"id", "firstName", "last_name"})
public class Customer {

 private String firstName;
 private String last_name;
 private int id;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return last_name;
 }

 public void setLastName(String last_name) {
  this.last_name = last_name;
 }

 public int getID() {
  return id;
 }

 public void setID(int id) {
  this.id = id;
 }

}

XML Output
Since we are using field access, the element names are now based on the field names.  The @XmlElement annotation could be used to override the default element names.

<?xml version="1.0" encoding="UTF-8"?>
<customer>
    <id>123</id>
    <firstName>Jane</firstName>
    <last_name>Doe</last_name>
</customer>

Demo Code
The following demo code can be used to produce the XML output shown above.

package blog.xmltype.proporder;

import javax.xml.bind.*;

public class Demo {

 public static void main(String[] args) throws Exception {
  JAXBContext jc = JAXBContext.newInstance(Customer.class);

  Customer customer = new Customer();
  customer.setFirstName("Jane");
  customer.setLastName("Doe");
  customer.setID(123);

  Marshaller marshaller = jc.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  marshaller.marshal(customer, System.out);
 }

}

 

From http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html

Published at DZone with permission of Blaise Doughan, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: