Monday, March 29, 2010

JAXB tutorial

XML is the lingua franca of the web services world. Unsurprisingly, the ability to process XML documents simply is an essential requirement. To my mind DOM and SAX parsers are the equivalent of assembler programming compared to the high-level programming provided by the Java Architecture for XML Binding, JAXB. It is an essential tool in every Java developer's portfolio. But many tutorials on the web hark back to JAXB1.0 so here is something to get you started.

The JAXB tutorial provided by Rose India is rather dated now. The IBM resource was a fairly good introduction to Java-XML mapping once upon a time but the Sun/Oracle JAXB tutorial is simply amazing.

Rarely a tutorial is written which is as comprehensive as the one provided here. All the aspects of mapping XML Schema Language types to Java, marshalling Java-based tree content to XML, unmarshalling from XML-to-Java, ObjectFactory class, XML annotations, the JAXB schema compiler (xjc) et al have been covered very lucidly.

If it still doesn't satisfy your curiosity then the book JAXB 2.0 is all you need for all the JAXB requirements but unfortunately it is in German.

Here is a sample code to marshal and unmarshal a simple letter written by me to Helen of Troy.

package raj;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Letter {

/**
* The sender of the letter.
*/
private String sender;
/**
* The receiver.
*/
private String receiver;
/**
* A unique number for the message number sent .
*/
private int messageNo;
/**
* The missive despatched.
*/
private String message;

public Letter() {
}

public String getSender() {
return sender;
}

public void setSender(String sender) {
this.sender = sender;
}

public String getReceiver() {
return receiver;
}

public void setReceiver(String receiver) {
this.receiver = receiver;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public int getMessageNo() {
return messageNo;
}

@XmlAttribute
public void setMessageNo(int messageNo) {
this.messageNo = messageNo;
}

public String toString() {
return String.format("Letter %s from %s to %s, saying '%s'", messageNo, sender, receiver, message);
}

public static void main(String[] arg) {
Letter text = new Letter();
text.setSender("Rajeev Trikha");
text.setMessageNo(2010);
text.setReceiver("Helen of Troy");
text.setMessage("Welcome to my world of JAXB in 2010!");
// Create a JAXB context and a marshaller
try {
// here just using a class as an argument but normally is a package
JAXBContext context = JAXBContext.newInstance(Letter.class);
Marshaller marshaller = context.createMarshaller();
// Set the output in formatted manner
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Make the actual serialization

marshaller.marshal(text, new File("raj.xml"));
Unmarshaller unmarshaller = context.createUnmarshaller();
// Get the object
Letter msg = (Letter) unmarshaller.unmarshal(new File("raj.xml"));
System.out.println(msg);
} catch (Exception e) {
System.out.println("exc");
}

}
}

The marshalled file raj.xml shows the created XML message which is well indented.

The unmarshalling prints out to the console:
Letter 2010 from Rajeev Trikha to Helen of Troy, saying 'Welcome to my world of JAXB in 2010!'

If you are using Netbeans then check out for understanding how xjc is tied up within the product.

No comments:

Post a Comment