Spring Castor
Hello Friends hope you are fine .there are two ways using which we can perform Marshalling of java objects into xml and unmarshalling of xml element into tree of java object using spring framework.
there are two ways of unmarshalling/marshalling using spring:
(1)Spring Xstream
(2) Spring Castor
i am going to demonstrate Spring Castor.The Key differance between spring castor and oxm is that in cse of Spring castor one extra configuration file is required inorder to map java object with xml.
You need to add castor-1.3 jar and other core spring jars required to run spring base application:
Project Structure :
sample code is given below :
Pojo Class
package org.castor.pojo;
//object that we want to marshal
public class Student
{
private int id;
private String name;
private String college;
public Student() { }
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 String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
}
applicationcontext.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="CastorMarshelingBean" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="targetClass" value="org.castor.pojo.Student"></property>
<property name="mappingLocation" value="mapping.xml"></property>
</bean>
</beans>
mapping.xml : to map java objets with corresponding xml element
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<class name="org.castor.pojo.Student">
<map-to xml="Student"/>
<field name="id" type="integer">
<bind-xml name="id" node="element"/>
</field>
<field name="name" type="string">
<bind-xml name="name" node="element"/>
</field>
<field name="college" type="string">
<bind-xml name="college" node="element"/>
</field>
</class>
</mapping>
Client Class
package org.castor.client;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import org.castor.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
public class Client
{
public static void main(String args[]) throws XmlMappingException, IOException
{
ApplicationContext context= new ClassPathXmlApplicationContext("applicationcontext.xml");
Marshaller m=(Marshaller) context.getBean("CastorMarshelingBean");
Student s=new Student();
s.setId(1);
s.setName("nirali");
s.setCollege("Semcom");
m.marshal(s,new StreamResult(new FileWriter("D:\\Student.xml")));
System.out.println("Task Completed!!!");
}
}
the console will look like given below:
Hello Friends hope you are fine .there are two ways using which we can perform Marshalling of java objects into xml and unmarshalling of xml element into tree of java object using spring framework.
there are two ways of unmarshalling/marshalling using spring:
(1)Spring Xstream
(2) Spring Castor
i am going to demonstrate Spring Castor.The Key differance between spring castor and oxm is that in cse of Spring castor one extra configuration file is required inorder to map java object with xml.
You need to add castor-1.3 jar and other core spring jars required to run spring base application:
Project Structure :
sample code is given below :
Pojo Class
package org.castor.pojo;
//object that we want to marshal
public class Student
{
private int id;
private String name;
private String college;
public Student() { }
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 String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
}
applicationcontext.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="CastorMarshelingBean" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="targetClass" value="org.castor.pojo.Student"></property>
<property name="mappingLocation" value="mapping.xml"></property>
</bean>
</beans>
mapping.xml : to map java objets with corresponding xml element
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<class name="org.castor.pojo.Student">
<map-to xml="Student"/>
<field name="id" type="integer">
<bind-xml name="id" node="element"/>
</field>
<field name="name" type="string">
<bind-xml name="name" node="element"/>
</field>
<field name="college" type="string">
<bind-xml name="college" node="element"/>
</field>
</class>
</mapping>
Client Class
package org.castor.client;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import org.castor.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
public class Client
{
public static void main(String args[]) throws XmlMappingException, IOException
{
ApplicationContext context= new ClassPathXmlApplicationContext("applicationcontext.xml");
Marshaller m=(Marshaller) context.getBean("CastorMarshelingBean");
Student s=new Student();
s.setId(1);
s.setName("nirali");
s.setCollege("Semcom");
m.marshal(s,new StreamResult(new FileWriter("D:\\Student.xml")));
System.out.println("Task Completed!!!");
}
}
the console will look like given below:
Thanks for reading this artilcle for any query ping me related to this post ping me on pathak.nisarg@yahoo.com
Regards