Spring Xstream
Hello Friends i am going to demonstrate Spring Xstream,Xstream is a library to marshal objects to xml and vice-versa without requirement of any mapping file. Notice that castor requires an mapping file.
Here is sample code:
//object that i am serializing using Xstream
package org.demo;
public class Employee {
private int id;
private String name;
private float salary;
public Employee() { }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
//code of conversion from java object into xml form
Client.java
package org.demo;
import javax.xml.transform.stream.StreamResult;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
import java.io.FileWriter;
import java.io.IOException;
public class Client
{
public static void main(String[] args) throws XmlMappingException, IOException, BeansException
{
ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");
Marshaller marshaller=(Marshaller)context.getBean("xstreamMarshellerBean");
Employee e=new Employee();
e.setId(1);
e.setName("Dhruti");
e.setSalary(20000);
marshaller.marshal(e,new StreamResult(new FileWriter("employee.xml")));
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="xstreamMarshallerBean" class="org.springframework.oxm.xstream.XStreamMarshaller" scope="singelton">
<property name="annotatedClasses" value="org.demo.Employee"/>
</bean>
</beans>
hope you will get my point.in next post i will demonstrate you how to work with spring caster .
thanks
for any query ping me on pathak.nisarg@yahoo.com
Hello Friends i am going to demonstrate Spring Xstream,Xstream is a library to marshal objects to xml and vice-versa without requirement of any mapping file. Notice that castor requires an mapping file.
Here is sample code:
//object that i am serializing using Xstream
package org.demo;
public class Employee {
private int id;
private String name;
private float salary;
public Employee() { }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
//code of conversion from java object into xml form
Client.java
package org.demo;
import javax.xml.transform.stream.StreamResult;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
import java.io.FileWriter;
import java.io.IOException;
public class Client
{
public static void main(String[] args) throws XmlMappingException, IOException, BeansException
{
ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");
Marshaller marshaller=(Marshaller)context.getBean("xstreamMarshellerBean");
Employee e=new Employee();
e.setId(1);
e.setName("Dhruti");
e.setSalary(20000);
marshaller.marshal(e,new StreamResult(new FileWriter("employee.xml")));
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="xstreamMarshallerBean" class="org.springframework.oxm.xstream.XStreamMarshaller" scope="singelton">
<property name="annotatedClasses" value="org.demo.Employee"/>
</bean>
</beans>
hope you will get my point.in next post i will demonstrate you how to work with spring caster .
thanks
for any query ping me on pathak.nisarg@yahoo.com
No comments:
Post a Comment