DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • What Is Ant, Really?
  • Extracting Data From Very Large XML Files With X-definition
  • Filtering Java Collections via Annotation-Driven Introspection
  • Composing Custom Annotations in Spring

Trending

  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems
  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Going All In On the JVM 25 Virtual Threads

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. JAXB's @XmlType and propOrder

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.

By 
Blaise Doughan
·
Feb. 14, 12 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
65.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Domain model XML Property (programming) Element Attribute (computing) Annotation POST (HTTP)

Opinions expressed by DZone contributors are their own.

Related

  • What Is Ant, Really?
  • Extracting Data From Very Large XML Files With X-definition
  • Filtering Java Collections via Annotation-Driven Introspection
  • Composing Custom Annotations in Spring

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook