Wednesday, 19 September 2012

Java XML JAXB Example


What is JAXB ?

JAXB ( Java Architecture for XML Binding) is annotation based API for convert Java Object to xml and xml to Java Objects. it is easier to access XML documents from applications written in Java.
Below are the main process in this api
1. Marshalling – Convert a Java object into a XML file.
2. Unmarshalling – Convert XML content into a Java Object
JAXB classes
javax.xml.bind.JAXBContext ->The JAXBContext class provides the client's entry point to the JAXB API.
javax.xml.bind.JAXBException->This is the root exception class for all JAXB exceptions.
javax.xml.bind.Marshaller->The Marshallerclass is responsible for governing the process of serializing Java content trees back into XML data
javax.xml.bind.Unmarshaller->The Unmarshallerclass governs the process of deserializing XML data into Java Object.

Added annotations in POJO

package mycollectiontest;

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

public class Customer {
String name;
String designation;
String address;
String employer;
int id;

public String toString() {
return "Customer [name=" + name + ", designation=" + designation +
", address=" + address + ", employer=" + employer + ", id=" + id
+ "]";

public String getName() {
return name;

public void setName(String name) { = name;

public String getDesignation() {
return designation;

public void setDesignation(String designation) {
this.designation = designation;

public String getAddress() {
return address;

public void setAddress(String address) {
this.address = address;

public String getEmployer() {
return employer;

public void setEmployer(String employer) {
this.employer = employer;

public int getId() {
return id;

public void setId(int id) { = id;

Main class to test object to xml and xml to object
package mycollectiontest;


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

public class JAXBExample {

public static void main(String[] args) {

// Object to xml

Customer customer = new Customer();
customer.setEmployer("Self employment");
try {

File file = new File("customer.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
} catch (JAXBException e) {

try {
File file1 = new File("customer.xml");
JAXBContext jaxbContext1 = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext1.createUnmarshaller();
Customer pc = (Customer) jaxbUnmarshaller.unmarshal(file1);
} catch (JAXBException e) {
// XML to object


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="0">
<employer>Self employment</employer>
Customer [name=vinod, designation=Publisher, address=Bangalore,
employer=Self employment, id=0]