Introduction to JAXB 2.0

Table of contents
Reading Time: 2 minutes

JAXB stands for Java Architecture for XML Binding. It provides mechanism to write java objects into XML and read XML into object. Simply, you can say it is used to convert java object into XML and vice-versa.

JAXB provides a fast and convenient way to bind XML schemas and Java representations, making it easy for Java developers to incorporate XML data and processing functions in Java applications. As part of this process, JAXB provides methods for unmarshalling (reading) XML instance documents into Java content, and then marshalling (writing) Java content back into XML instance documents. JAXB also provides a way to generate XML schema from Java objects.


Feature of JAXB 2.0 :-
1)  Support for all W3C XML Schema features.
2)  Annotation support with the addition of the javax.xml.bind.annotation package to   control this binding.
3)  Additional validation capabilities through the JAXP 1.3 validation APIs.
4)  Smaller runtime libraries.

JAXB Marshalling example : Converting Objects to XML

In this example we need to convert java object to XML document.
First we need to create a POJO class.

import javax.xml.bind.annotation.*;

public class Student {

private String name;
private int id;
private String subject;


Student(String name,int id,String subject){;;

public String getName() {
return name;
public void setName(String name) { = name;

public int getId() {
return id;
public void setId(int id) { = id;

public String getSubject() {
return subject;
public void setSubject(String subject) {
this.subject = subject;

In the above class

@XmlRootElement specifies the root element for the XML document.
@XmlAttribute specifies the attribute for the root element.
@XmlElement specifies the sub element for the root element.

Now, We will  call marshaller method

//creating the JAXB context
JAXBContext jContext = JAXBContext.newInstance(Student.class);
//creating the marshaller object
Marshaller marshallObj = jContext.createMarshaller();
//setting the property to show xml format output
marshallObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//setting the values in POJO class
Student student = new Student(“abhishek”, 1163, “hadoop”);
//calling the marshall method
marshallObj.marshal(student, new FileOutputStream(“/home/knoldus/Desktop/student.xml”));

} catch(Exception e) {

JAXB Unmarshalling example : Converting XML to Objects

//getting the xml file to read
File file = new File(“/home/knoldus/Desktop/student.xml”);
//creating the JAXB context
JAXBContext jContext = JAXBContext.newInstance(Student.class);
//creating the unmarshall object
Unmarshaller unmarshallerObj = jContext.createUnmarshaller();
//calling the unmarshall method
Student student=(Student) unmarshallerObj.unmarshal(file);

System.out.println(student.getName()+” “+student.getId()+” “+student.getSubject());
}catch(Exception e){

Refrences :-,


Written by 

Joseph Ross is a Principal Consultant at Knoldus Inc. having more than 10 years of experience. Joseph has a passion for identifying challenges and give impactful solutions to the clients. He is a football fan and loves to watch TV series. Joseph has a cross-functional business operations and technology consulting experience. Joseph is familiar with programming languages such as Scala, C++, Java, CSS and HTML.

1 thought on “Introduction to JAXB 2.02 min read

Comments are closed.