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



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

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

public int getId() {
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getDivision() {
return division;
}
@XmlElement
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);
System.out.println(student);
} catch (JAXBException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JAXBParser parser = new JAXBParser();
parser.parserDocument();
}
}

Now let us see the output.

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();
student.setId(1);
student.setName("Bijoy");
student.setDivision("A");
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) {
e1.printStackTrace();
} catch (JAXBException e1) {
e1.printStackTrace();
}
}

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

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


A 1
Bijoy

 

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 *