XML Processing in Java-JAXB Tutorial

So far we have discussed the various interfaces in JAXP.In all those 3 interfaces , the developer need to write the logic for parsing as well as generating XML documents.JAXB is  a new technique in XML processing.This chapter gives a simple JAXB tutorial with simple examples

 JAXB Tutorial – Overview

JAXB is comparatively new  technique. JAXB is the abbreviation of Java API for XML Binding.It allows mapping between XML and Java class.The marshal and unmarshal features of  JAXB eases the development effort . It is possible to marshal java object to XML as well as unmarshal XML to java object . JAXB is inbuilt in Java with J2SE  1.6 onwards.If JDK version is less than 1.6 , then we should download JAXB and  we should put the libraries  in class path.(JAXB can be downloaded from here. )JAXB uses  concept of annotation.

Parsing XML using JAXB

The unmarshal technique of JAXB is using while parsing .

Now lets see our input student.xml

Bijoy A

Now let us see out Student.java which is used to map with the student.xml

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

public class Student {
private int id;
private String name;
private String division;

public int getId() {
return id;
public void setId(int id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public String getDivision() {
return division;
public void setDivision(String division) {
this.division = division;
public String toString(){
return "Id = "+getId() +" Name = "+getName() +" Division = "+getDivision();

Please see the annotations in the above class .(@XmlRootElement  and @XmlElement ).JAXB is using these annotations while parsing the document.

Now let us see the main class to parse the input file.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;

public class JAXBParser {
public JAXBParser() {

public void parserDocument() {

try {
File inputFile = new File("C:\\Users\\My PC\\Projects\\Sample\\files\\student.xml");
JAXBContext context = JAXBContext.newInstance(Student.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Student student = (Student)unmarshaller.unmarshal(inputFile);
} catch (JAXBException e) {
public static void main(String[] args) {
JAXBParser parser = new JAXBParser();

Now let us see the output.


Id = 1 Name = Bijoy Division = A

Generating XML using JAXB

As we discussed earlier the marshal technique of JAXB is using to generate XML file from Java objects.The following example creates a student1.xml file from a Student object.(Use the same  Student.java we discussed earlier in this chapter).Let us see the main class.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import java.io.File;

public class JAXBGenerator {
public JAXBGenerator() {

public void generateXML() {
try {
Student student = new Student();
File inputFile = new File("C:\\Users\\My PC\\Projects\\Sample\\files\\student1.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(student, inputFile);
} catch (PropertyException e1) {
} catch (JAXBException e1) {

public static void main(String[] args) {
JAXBGenerator generator = new JAXBGenerator();

Now run the code and check the student1.xml generated in the mentioned location.The student1.xml is shown below

A 1


See Also:

Overview to XML Processing

SAX interface

DOM interface

StAX interface

Leave a Reply

Your email address will not be published. Required fields are marked *