XML Serialization with Java

You might have come across various technologies with XML Serialization, but XStream happens to be one of those which can give quickest implementation and Go-Live. XStream is a very simple library which helps one serialize Java Objects to XML and vice-versa.

The below example will help one get their hands on XStream in a few minutes and you would like to implement it in real-world scenarios as soon as you are done with this example – Yes it is that simple.


package com.dirtyyourhands;

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamOmitField;

public class XStreamImpl {
  public static void main(String[] args) {
    School school = new School();
    school.addStudent(new Student("firstName", "lastName", 1));
    school.addStudent(new Student("firstName", "lastName", 2));
    school.addStudent(new Student("firstName", "lastName", 3));
    school.addStudent(new Student("firstName", "lastName", 4));
    school.addStudent(new Student("firstName", "lastName", 5));
    school.addStudent(new Student("firstName", "lastName", 6));

    XStream xStream = new XStream();
    xStream.autodetectAnnotations(true);
    System.out.println(xStream.toXML(school));

  }
}

@XStreamAlias("School")
class School {
  @XStreamAlias("studentList")
  List<Student> students;

  School() {
    students = new ArrayList<Student>();
  }

  public void addStudent(Student stud) {
    students.add(stud);
  }
}

@XStreamAlias("Student")
class Student {
  @XStreamOmitField
  String firstName;

  @XStreamOmitField
  String lastName;

  String displayName;

  int rollNumber;

  /**
   * @param firstName
   * @param lastName
   */
  public Student(String firstName, String lastName, int rollNumber) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.rollNumber = rollNumber;

    setDisplayName();
  }

  /**
   * @return the firstName
   */
  public String getFirstName() {
    return firstName;
  }

  /**
   * @param firstName
   *            the firstName to set
   */
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  /**
   * @return the lastName
   */
  public String getLastName() {
    return lastName;
  }

  /**
   * @param lastName
   *            the lastName to set
   */
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  /**
   * @return the rollNumber
   */
  public int getRollNumber() {
    return rollNumber;
  }

  /**
   * @param rollNumber
   *            the rollNumber to set
   */
  public void setRollNumber(int rollNumber) {
    this.rollNumber = rollNumber;
  }

  /**
   * @return the displayName
   */
  public String getDisplayName() {
    return displayName;
  }

  /**
   * @param displayName
   *            the displayName to set
   */
  public void setDisplayName() {
    this.displayName = firstName + " " + lastName;
  }

}

The Output from the above example looks like



<School>
  <studentList>
    <Student>
      <displayName>firstName lastName</displayName>
      <rollNumber>1</rollNumber>
    </Student>
    <Student>
      <displayName>firstName lastName</displayName>
      <rollNumber>2</rollNumber>
    </Student>
    <Student>
      <displayName>firstName lastName</displayName>
      <rollNumber>3</rollNumber>
    </Student>
    <Student>
      <displayName>firstName lastName</displayName>
      <rollNumber>4</rollNumber>
    </Student>
    <Student>
      <displayName>firstName lastName</displayName>
      <rollNumber>5</rollNumber>
    </Student>
    <Student>
      <displayName>firstName lastName</displayName>
      <rollNumber>6</rollNumber>
    </Student>
  </studentList>
</School>

Key points from this example:

1. @XStreamAlias – it helps you defined the xml tag XStream should use while serializing the xml.
2. @XStreamOmitField – it helps you define the fields which you need to ignore from serializing.