What is JAXB?

It has become common for enterprise applications to exchange, represent any data using XML. It is possible to represent binary and character based data in XML files.

Read here Advantages of storing data in XML format.

And it would be often required to convert an object to an XML and vice versa.

Process of converting an object to XML is called Marshalling. And the process of converting XML to an object is called Un Marshalling.

J2EE has provided JAXB for the same. JAXB stands for Java Architecture for XML Binding.

JAXB is provided in package javax.xml

Below is an example for Marshalling, i..e converting an object to its XML representation.
This example has POJO classes Address8 and Employee8.
Employee8 and Address8 has composition relationship.
Annotations are used in Employee8 and Address8 classes, to specify XML Root Element, XML Elements and XML attributes.
MainAppl8 class creates marshaller, and marshalls Employee8 object to my_emp.xml XML file.


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

@XmlRootElement
     public class Address8 {
    
    private int id;
    private String city;
    private int postCode;
 
    public Address8(){}


    @XmlElement(name="aId")  
    public int getId() {
        return id;
    }

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

    @XmlElement(name="aCity")  
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @XmlElement(name="aPostC")  
    public int getPostCode() {
        return postCode;
    }

    public void setPostCode(int postCode) {
        this.postCode = postCode;
    }
}
_________________________________________________________________________________________________________________
import javax.xml.bind.annotation.XmlAttribute;  
import javax.xml.bind.annotation.XmlElement;  
import javax.xml.bind.annotation.XmlRootElement;  
import javax.xml.bind.annotation.XmlAccessorType;
  
@XmlRootElement
public class Employee8 {  
private int id;  
private String name;  
private float salary;  
private int age;
private Address8 address;

public Employee8(){}

@XmlAttribute(name="id")  
public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
@XmlElement(name="name")  
public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
@XmlElement(name="salary")  
public float getSalary() {  
    return salary;  
}  
public void setSalary(float salary) {  
    this.salary = salary;  
}  

@XmlElement(name="age")
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @XmlElement(name="address")  
    public Address8 getAddress() {
        return address;
    }

    public void setAddress(Address8 address) {
        this.address = address;
    }
}  
___________________________________________________________________________________________________________________
import java.io.FileOutputStream;  
import javax.xml.bind.JAXBContext;  
import javax.xml.bind.Marshaller;  
  
public class MainAppl8 {  
public static void main(String[] args) throws Exception{  
    JAXBContext contextObj = JAXBContext.newInstance(Employee8.class);  
  
    Marshaller marshallerObj = contextObj.createMarshaller();  
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  
    Address8 adr=new Address8();
      
    adr.setId(1);
    adr.setCity("Bangalore");
    adr.setPostCode(123456);
    
    Employee8 emp=new Employee8();
    emp.setId(2);
    emp.setAge(45);
    emp.setAddress(adr);
    emp.setName("Kiran Kumar");
    emp.setSalary(46000);
    
    marshallerObj.marshal(emp, new FileOutputStream("my_emp.xml"));  
       
}  
}  

JAXB marshall example(Netbeans)

Below is an example for UnMarshalling, i..e converting XML to an object. In this example XML String is converted into Employee object, using JAXB.

public class Employee {
 private String firstName;
 private String lastName;
 private String fatherName;

 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;
 }

  public String getFatherName() {
  return fatherName;
 }

 public void setFatherName(String fatherName) {
  this.fatherName = fatherName;
 }
}
______________________________________________________________________________________________________
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class CreateEmployeeFromXml {
 public static void main(String[] args) {
  try {
   String xml = "<Employee><firstName>Kiran</firstName><lastName>Kumar</lastName><fatherName>Kishore</fatherName></Employee>";
   JAXBContext jaxbc = JAXBContext.newInstance(Employee.class);
   Unmarshaller unmarshaller = jaxbc.createUnmarshaller();
   StreamSource ss = new StreamSource(new StringReader(xml));
   JAXBElement jbe = unmarshaller.unmarshal(ss,
     Employee.class);
   
   Employee employee = (Employee)jbe.getValue();
   System.out.println("Employee's First Name is" +  employee.getFirstName());
   System.out.println("Employee's Last Name is" +  employee.getLastName());
   System.out.println("Employee's father Name is" +  employee.getFatherName());
   
  } catch (JAXBException e) {
      System.out.println(e.getMessage());
   e.printStackTrace();
  }
 }
}

JAXB unmarshall example(Netbeans)

You may also like to read:
What is DTD in XML?
How many Servlet filters can be used?