Friday, April 6, 2012

Java : Simple JAXB marshalling and unmarshalling Hello World Example

JAXB is an acronym derived from Java Architecture for XML Binding. This articles shows how to convert Java object to XML using JAXB annotation and vice versa. If you would like to know more about JAXB, see below links.

Marshalling - Convesrting a Java object to XML is known as Marshalling.
Unmarshalling - Converting a XML to Java Object is known as Unmarshalling.

About the example :

We have a Employee class which contains Name class reference in it. i.e HAS-A relation. Along with emplId and dept. contains marshalIt() and unmarshalIt() method. marshalIt() as the name suggest, it accepts Employee object and return XML string out of it. And similarly unmarshalIt() method takes Class name and XML string and return Employee object out of it.

marshalIt(Object objectName) method uses JAXBContext to create Marshaller object

JAXBContext jaxbContext = JAXBContext.newInstance(objectName.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();

Similarly unmarshalIt(Class className,String xml) method use JAXBContext to create Unmarshaller object

JAXBContext jaxbContext = JAXBContext.newInstance(className);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

No need to include any JAR file if you are using Java 1.6 and above because JAXB is bundled in JDK 1.6. Directly you can run below example and see the output. However if you are using Java 1.5 or lower version you need to include JAR file (e.g JAXB2_20120218.jar ) from

Below are the self explanatory Java example. Directly you can run the example and see the output no need to have any IDE (e.g. Eclipse)

/* */

public class Name {

String firstName;
String lastName;

 public String getFirstName() {
  return firstName;

 public void setFirstName(String firstName) {
  this.firstName = firstName;

 public String getLastName() {
  return lastName;

 public void setLastName(String lastName) {
  this.lastName = lastName;

/* */

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class Employee {

 private String emplId;
 private Name name;
 private String dept;

 public String getEmplId() {
  return emplId;

 //This field is used as attribute
 public void setEmplId(String emplId) {
  this.emplId = emplId;

public Name getName() {
  return name;

public void setName(Name name) { = name;

 //This field is element with custom name
 @XmlElement (name="Department")
 public String getDept() {
  return dept;

public void setDept(String dept) {
  this.dept = dept;

/* */


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class TestMarshalling {

 public static void main(String[] args) throws JAXBException {

  //create employee object
  Employee emp = new Employee();

  //create name object
  Name name = new Name();

  //set name object to employee object


  //get XML out of Object by Marshalling
  String xml = marshalIt(emp);

  System.out.println("--- Generated XML ---");

  //get Object out of XML by Unmarshalling
  Employee newEmp = (Employee) unmarshalIt(Employee.class,xml);

  System.out.println("--- Generated Employee Object ---");

 public static String marshalIt(Object objectName) throws JAXBException {

  JAXBContext jaxbContext = JAXBContext.newInstance(objectName.getClass());
  Marshaller marshaller = jaxbContext.createMarshaller();

  //For Pretty printing output
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

  StringWriter writer = new StringWriter(); 
  marshaller.marshal(objectName, writer);

  return writer.toString();


 public static Object unmarshalIt(Class<?> className,String xml) throws JAXBException {

  JAXBContext jaxbContext = JAXBContext.newInstance(className);

  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

  StringReader reader = new StringReader(xml);

  return unmarshaller.unmarshal(reader);






$java TestMarshalling
--- Generated XML ---
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee emplId="001">
--- Generated Employee Object ---

No comments:

Post a Comment