Archive

Archive for the ‘XStream’ Category

Serializing and Deserializing Object using XStream

December 21, 2010 Leave a comment

XStream artifact is used to convert java objects into xml and back again.

Library used:
xstream-1.3.jar

1. Employee.java

package com.room.xstream.demo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Employee implements Serializable{

	private static final long serialVersionUID = 1L;
	private Name name;
	private Address address;
	private List<Phone> phoneNos=new ArrayList<Phone>();
	
	public Name getName() {
		return name;
	}
	public void setName(Name name) {
		this.name = name;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public List<Phone> getPhoneNos() {
		return phoneNos;
	}
	public void setPhoneNos(List<Phone> phoneNos) {
		this.phoneNos = phoneNos;
	}
	
	public String toString(){
		return this.name+"\n"+this.address+"\n"+this.phoneNos;
	}
}

2. Name.java

package com.room.xstream.demo;

import java.io.Serializable;

public class Name implements Serializable{

	private static final long serialVersionUID = 1L;
	private String firstName;
	private String middleName;
	private String lastName;
	
	public Name(String first,String middle,String last){
		this.firstName=first;
		this.middleName=middle;
		this.lastName=last;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getMiddleName() {
		return middleName;
	}
	public String getLastName() {
		return lastName;
	}
	public String toString(){
		return this.firstName+ " "+this.middleName+" "+this.lastName;
	}
}

3. Address.java

package com.room.xstream.demo;

import java.io.Serializable;

public class Address implements Serializable{

	private static final long serialVersionUID = 1L;
	private String street;
	private String city;
	private String state;
	private Integer zipCode;
	
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public Integer getZipCode() {
		return zipCode;
	}
	public void setZipCode(Integer zipCode) {
		this.zipCode = zipCode;
	}
	public String toString(){
		return this.street+" "+this.city+" "+this.state+" "+this.zipCode;
	}
}

4. Phone.java

package com.room.xstream.demo;

import java.io.Serializable;

public class Phone implements Serializable{

	private static final long serialVersionUID = 1L;
	private String code;
	private Integer phone;
	public Phone(String code,Integer phone){
		this.code=code;
		this.phone=phone;
	}
	public String getCode() {
		return code;
	}
	public Integer getPhone() {
		return phone;
	}
	
	public String toString(){
		return this.code+" "+this.phone;
	}
}

5. Create a XStream Tester class
Here XStreamTester class is created to test the conversion of objects into XML and XML into equivalent objects.

package com.room.xstream.demo.test;

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

import com.room.xstream.demo.Address;
import com.room.xstream.demo.Employee;
import com.room.xstream.demo.Name;
import com.room.xstream.demo.Phone;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XStreamTester {
	
	/**
	 * Main method to test the conversion of XML into Java Objects
	 * and Java Objects into XML
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(toXML(buildEmployee()));
		System.out.println("------------------------------------------------");
		System.out.println(toObject(buildXMLAsString()));
	}
	
	/**
	 * This method is to convert Java Objects into XML
	 * @param employee
	 * @return XML as String
	 */
	public static String toXML(Employee employee){
		XStream xstream=new XStream(new DomDriver());
		xstream.alias("employee", Employee.class);
		xstream.alias("address", Address.class);
		xstream.alias("phone", Phone.class);
		return xstream.toXML(employee);
	}
	/**
	 * This method is to convert XML into Employee Object
	 * @param inputXML as String
	 * @return Employee
	 */
	public static Employee toObject(String inputXML){
		XStream xstream=new XStream(new DomDriver());
		xstream.alias("employee", Employee.class);
		xstream.alias("address", Address.class);
		xstream.alias("phone", Phone.class);
		return (!isEmpty(inputXML))?(Employee)xstream.fromXML(inputXML):null;
	}
	/**
	 * This method is to check object passed is empty or not
	 * @param input
	 * @return boolean [true or false]
	 */
	private static boolean isEmpty(Object input){
		return input==null || "".equals(input.toString().trim());
	}
	
	
	private static String buildXMLAsString(){
		final String xmlAsStr=
			    "<employee>" +
				"  <name>" +
				"    <firstName>Rathina</firstName>" +
				"    <middleName>Sabapathy</middleName>" +
				"    <lastName>Sudhakar</lastName>" +
				"  </name>" +
				"  <address>" +
				"    <street>North Street</street>" +
				"    <city>Madurai</city>" +
				"    <state>TamilNadu</state>" +
				"    <zipCode>7664646</zipCode>" +
				"  </address>" +
				"  <phoneNos>" +
				"    <phone>" +
				"      <code>011</code>" +
				"      <phone>786576464</phone>" +
				"    </phone>" +
				"    <phone>" +
				"      <code>022</code>" +
				"      <phone>646464644</phone>" +
				"    </phone>" +
				"    <phone>" +
				"      <code>033</code>" +
				"      <phone>986986576</phone>" +
				"    </phone>" +
				"  </phoneNos>" +
				"</employee>";
		return xmlAsStr;
	}
	
	private static Employee buildEmployee(){
		Employee emp=new Employee();
		emp.setName(new Name("Rathina", "Sabapathy", "Sudhakar"));
		Address address=new Address();
		address.setStreet("North Street");
		address.setCity("Madurai");
		address.setState("TamilNadu");
		address.setZipCode(Integer.valueOf("7664646"));
		emp.setAddress(address);
		List<Phone> phoneNos=new ArrayList<Phone>();
		phoneNos.add(new Phone("011", 786576464));
		phoneNos.add(new Phone("022", 646464644));
		phoneNos.add(new Phone("033", 986986576));
		emp.setPhoneNos(phoneNos);
		return emp;
	}
}
Advertisements
Categories: Java, XStream Tags: