Posted: February 7, 2012 in Random Posts

Nikos' Java blog

JAXB is a handy technology that easily transforms Java objects to XML documents and vice versa.

Define the object

There is a business need to define and use messages. For every message we need:

  • The sender
  • The receiver
  • The actual message
  • A unique identification
@XmlRootElement
public class Message {
   private String from;
   private String to;
   private String text;
   private int id;
}

API: XmlRootElement

We would like the id to be an attribute of the root element.

@XmlRootElement
public class Message {
   private String from;
   private String to;
   private String text;
   private int id;

   @XmlAttribute
   public void setId(int id) {
      this.id = id;
   }
}

API: XmlAttribute

Here’s the complete class.

/**
 * A message.
 */@XmlRootElement
public class Message {
   /**
    * The sender.
    */
   private String from;
   /**
    * The receiver.
    */
   private String to;
   /**
    * A unique identification.
    */
   private int id;
   /**     * The actual…

View original post 260 more words

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s