XML Processing-JAXB example

In the previous chapter we discussed about XML processing with JAXB. There we discussed with  very simple example.Here , we are discussing XML Processing with JAXB with little more complex examples.This chapter discusses JAXB example for XML parsing as well as XML generation

Overview to JAXB

The term JAXB stands for Java API for XML Binding. It allows mapping between XML document and Java class. The marshal  and unmarshal techniques  of JAXB eases the effort of developer.JAXB is inbuilt with JDK from J2SE 1.6 onwards.If our JDK version is below 1.6 , then we need to download  JAXB API and we need to include the libraries in our class path.(In that case , we can download JAXB from here)

Parsing XML using JAXB

Consider an XML file school.xml.Which contains a list of students.The input XML is :




XA
1
Bijoy


XB
2
Karthik

Now we need to parse the school.xml using JAXB. Here the root element is school.It has one attribute ‘name’ . It has a list of students.So the JAXB class :

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement
public class School {

private String name;
private List students = new ArrayList();

public String getName() {
return name;
}
@XmlAttribute
public void setName(String name) {
this.name = name;
}

public List getStudents() {
return students;
}
@XmlElement(name = "student")
public void setStudents(List students) {
this.students = students;
}
public String toString(){
return "Name = "+getName() +", Students ="+getStudents();
}
}

Now see  the Student.java. The School object contains a list of Student objects.Both classes are using JAXB specific annotations.

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

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

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 getBatch() {
return batch;
}
@XmlElement
public void setBatch(String batch) {
this.batch = batch;
}
public String toString(){
return "Id = "+getId() +" Name = "+getName() +" Batch = "+getBatch();
}
}

Now lets see the main class.It does the unmarshalling .

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 parseXML() {

try {
File inputFile = new File("C:\\Users\\My PC\\Projects\\Sample\\files\\school.xml");
JAXBContext context = JAXBContext.newInstance(School.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
School school = (School) unmarshaller.unmarshal(inputFile);
System.out.println(school);
} catch (JAXBException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
JAXBParser parser = new JAXBParser();
parser.parseXML();
}
}

Now let us see the output.

Output

Name = HighSchool, Students =[Id = 1 Name = Bijoy Batch = XA, Id = 2 Name = Karthik Batch = XB]

Generating XML with JAXB

The marshalling technique of JAXB is using  for generating XML. The School.java and Student.java are using as JAXB classes here also. We are generating a school1.xml file which contains a school .The scool contains few students.The main class is shown below.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class JAXBGenerator {
public JAXBGenerator() {

}

public void generateXML() {
School school = new School();
school.setName("HighSchool");
List studentList = new ArrayList();
Student student1 = new Student();
student1.setId(1);
student1.setName("Bijoy");
student1.setBatch("XA");
studentList.add(student1);
Student student2 = new Student();
student2.setId(2);
student2.setName("Karthik");
student2.setBatch("XB");
studentList.add(student2);

//setting students to school
school.setStudents(studentList);
try {
JAXBContext context = JAXBContext.newInstance(School.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
File outputFile = new File("C:\\Users\\My PC\\Projects\\Sample\\files\\school1.xml");
marshaller.marshal(school, outputFile);
System.out.println("Done");
} catch (JAXBException e) {
e.printStackTrace();
}

}

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

Generated school1.xml  :




XA
1
Bijoy


XB
2
Karthik


See related topics:

Overview to XML parsing with Java

DOM interface

SAX interface

StAX interface

Overview to JAXB