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.
Join the DZone community and get the full member experience.
Join For FreeIn 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.
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.
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;
}
}
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>
When field (instance variable) access is used, the entries in the propOrder attribute correspond to the field names.
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;
}
}
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>
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
Opinions expressed by DZone contributors are their own.
Comments