February 9, 2012

JAXB's @XmlType and propOrder

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);
 }

}

Further Reading

If you enjoyed this post then you may also be interested in:

3 comments: