Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Spring with Xstream


Xstream is a library to serialize objects to xml and vice-versa without requirement of any mapping file. Notice that castor requires an mapping file.

XStreamMarshaller class provides facility to marshal objects into xml and vice-versa.


Example of Spring and Xstream Integration (Marshalling Java Object into XML)

You need to create following files for marshalling java object into XML using Spring with Xstream:

  1. Employee.java
  2. applicationContext.xml
  3. Client.java

Required Jar files

To run this example, you need to load:

  • Spring Core jar files
  • Spring Web jar files
  • xstream-1.3.jar

download all the jar files for spring including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm etc.

download xstream-1.3.jar


Employee.java

If defines three properties id, name and salary with setters and getters.

  1. package com.Kreationnext;
  2. public class Employee {
  3. private int id;
  4. private String name;
  5. private float salary;
  6. public int getId() {
  7.     return id;
  8. }
  9. public void setId(int id) {
  10.     this.id = id;
  11. }
  12. public String getName() {
  13.     return name;
  14. }
  15. public void setName(String name) {
  16.     this.name = name;
  17. }
  18. public float getSalary() {
  19.     return salary;
  20. }
  21. public void setSalary(float salary) {
  22.     this.salary = salary;
  23. }
  24. }

applicationContext.xml

It defines a bean xstreamMarshallerBean where Employee class is bound with the OXM framework.

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans 
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>
  6. <bean id=“xstreamMarshallerBean” class=“org.springframework.oxm.xstream.XStreamMarshaller”>
  7.     <property name=“annotatedClasses” value=“com.Kreationnext.Employee”></property>
  8. </bean>
  9. </beans>

Client.java

It gets the instance of Marshaller from the applicationContext.xml file and calls the marshal method.

  1. package com.Kreationnext;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import javax.xml.transform.stream.StreamResult;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. import org.springframework.oxm.Marshaller;
  8. public class Client{
  9.  public static void main(String[] args)throws IOException{
  10.   ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  11.   Marshaller marshaller = (Marshaller)context.getBean(“xstreamMarshallerBean”);
  12.   Employee employee=new Employee();
  13.   employee.setId(101);
  14.   employee.setName(“Sonoo Jaiswal”);
  15.   employee.setSalary(100000);
  16.   marshaller.marshal(employee, new StreamResult(new FileWriter(“employee.xml”)));
  17.   System.out.println(“XML Created Sucessfully”);
  18.  }
  19. }

Output of the example

employee.xml

  1. <com.Kreationnext.Employee>
  2. <id>101</id>
  3. <name>Sonoo Jaiswal</name>
  4. <salary>100000.0</salary>
  5. </com.Kreationnext.Employee>

Comments

comments