Tuesday, April 15, 2014

Serialize and de-serialize objects with JAXB

Java Architecture for XML Binding (JAXB) is used to serialize java objects to XML (marshal) and to de-serialize XML back to java object (unmarshal). Here I am going to demonstrate how to marshal and unmarshal a simple java object using two methods.

First method is for the classes that some annotations can be added. Bellow is the class we are using to make objects which we are going to serialize.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "student")
class Student {
 private String name;
 private int age;
 
 public Student() {
 }

  public void setName(String name) {
  this.name = name;
 }

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

  public String getName() {
  return name;
 }

  public int getAge() {
  return age;
 }
}

As you can see, a annotation is added to tell that this is a XML root element, which is used in serializing the object.

From the bellow method, you can serialize a object created by the above class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public String marshal(Student student){
  StringWriter writer = new StringWriter();
  JAXBContext context;
  try {
   context = JAXBContext.newInstance(Student.class);
   Marshaller m = context.createMarshaller();
   m.marshal(student, writer);
   return writer.toString();
   
  } catch (JAXBException e) {
   e.printStackTrace();
   return null;
  }
}

This method will return a XML representation of a student object. Bellow method will return the Student object back from a serialized student object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public static Student unMarshal(String input){
  JAXBContext context;
  try {
   context = JAXBContext.newInstance(Student.class);
   Unmarshaller m = context.createUnmarshaller();
   return (Student)m.unmarshal(new StringReader(input));
  } catch (JAXBException e) {
   e.printStackTrace();
   return null;
  }
}

But if you want to serialize a object of which class signature cannot be changed and it does not have the annotations provided in the class above. Lets see how to use JAXB with the objects of a class like bellow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Student {
 private String name;
 private int age;
 
 public Student() {
 }

  public void setName(String name) {
  this.name = name;
 }

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

  public String getName() {
  return name;
 }

  public int getAge() {
  return age;
 }
}

Bellow code can be used to serialize a Student object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public String marshal(Student student){
  StringWriter writer = new StringWriter();
  JAXBContext context;
  try {
   context = JAXBContext.newInstance(Student.class);
   Marshaller m = context.createMarshaller();
   
         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
         JAXBElement<Student> rootElement = new JAXBElement<Student>(new QName("student"), Student.class, student);
   m.marshal(rootElement, writer);
   return writer.toString();
  } catch (JAXBException e) {
   e.printStackTrace();
   return null;
  }
}

It will return a XML string as in the previous case. And the bellow code can be used to de-serialize a student object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public Student unMarshal(String input){
  JAXBContext context;
  try {
   context = JAXBContext.newInstance(Student.class);
   Unmarshaller m = context.createUnmarshaller();
   StreamSource source = new StreamSource(new StringReader(input));
         return m.unmarshal(source, Student.class).getValue();
  } catch (JAXBException e) {
   e.printStackTrace();
   return null;
  }
}

In this method we have provided appropriate JAXB mapped class to hold node's XML data to the unmarshal method. As the unmarshal method which accepts a class parameter does not accept a just a StringReader, we have made a StreamSource using that StringReader. You can will see that there are lots of unmarshal methods overloaded and you will have to use a suitable one according to your requirements.

No comments:

Post a Comment