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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • Designing a Java Connector for Software Integrations
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Mastering Advanced Aggregations in Spark SQL
  • Infrastructure as Code (IaC) Beyond the Basics
  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 user avatar
Blaise Doughan
·
Feb. 14, 12 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
63.7K Views

Join the DZone community and get the full member experience.

Join For Free

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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!