What is JAXB ?

JAXB stands for Java Architecture for XML Binding and is used to convert a XML to JAVA objects and vice versa. So, there are two important operations you perform using JAXB –

Converting a Java object to XML – Marshalling.

Converting a XML file to Java object – UnMarshalling.

Its the Java’s standard way for Marshiling and UnMarshalling and is included as a part of Java 1.6 JDK and above. If you are using a version of JDK lesser than 1.6, download JAXB from here, and puts “jaxb-api.jar” and “jaxb-impl.jar” on your project classpath and proceed with using it.

JAXB Tutorial:

Using simple predefined Annotations and API’s, you can convert XML to/from Java objects in no time.

Create a student Class ( a Java object which will be used to convert from and to XML):


package com.gontuseries.jaxb;

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

@XmlRootElement  // annotation: @XmlRootElement tells
                 // that student will be the root element in the XML file to be produced
@XmlType(propOrder = { "name", "age", "rollNo" })//optional annotation: @XmlType You
                // can Specify the order in which XML elements output will be produced
public class Student {

String name;
int age;
int rollNo;

public String getName() {
return name;
}

@XmlElement // annotation: @XmlElement is used for producing this element (Name) under
// the root element specified above
public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

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

@XmlElement
public int getRollNo() {
return rollNo;
}

public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
}

Converting a Java object to XML file:


package com.gontuseries.jaxb;

import java.io.File;

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

public class JAXBJavaToXML {
public static void main(String[] args) {

// create student object
Student student = new Student();
student.setRollNo(1);
student.setName("gontuseries");
student.setAge(29);

try {

// specify the location and name of xml file to be produced
File XMLfile = new File("D:\XMLfile.xml");

// create JAXB context and initializing Marshaller
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

// set this property to format the generated output in a preety way
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

jaxbMarshaller.marshal(student, XMLfile); // this will create XML file in the specified location
jaxbMarshaller.marshal(student, System.out); // to print the XMLfile in the console

} catch (JAXBException e) {
// some exception occured
e.printStackTrace();
}

}
}

Output:

XML file generated from Java object student:

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?-->

 gontuseries
 29
 1


Converting a XML file to Java object:


package com.gontuseries.jaxb;

import java.io.File;

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

public class JAXBXMLToJava {
public static void main(String[] args) {

try {
// specify the location and name of xml file to be consumed
File XMLfile = new File("D:\XMLfile.xml");
// create JAXB context and initializing Marshaller
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

// this will create Java object - student from the XML file
Student student = (Student) jaxbUnmarshaller.unmarshal(XMLfile);

System.out.println(student);

} catch (JAXBException e) {
// some exception occured
e.printStackTrace();
}

}
}