Spring with Caster
Spring
with Castor Example
1. Spring and Castor Integration
2. Example of Spring and Castor
Integration
By the help of CastorMarshaller class,
we can marshal the java objects into xml and vice-versa using castor. It is the
implementation class for Marshaller and Unmarshaller interfaces. It doesn't
require any further configuration bydefault.
Example
of Spring and Castor Integration (Marshalling Java Object into XML)
You
need to create following files for marshalling java object into XML using
Spring with Castor:
1. Employee.java
2. applicationContext.xml
3. mapping.xml
4. Client.java
Required Jar files
To run this example, you need to
load:
Spring Core jar files
Spring Web jar files
castor-1.3.jar
castor-1.3-core.jar
Employee.java
If defines three properties id,
name and salary with setters and getters.
package com.smgc;
public class Employee {
private int id;
private String
name;
private float
salary;
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;
}
}
applicationContext.xml
It defines a bean
castorMarshallerBean where Employee class is bound with the OXM framework.
<?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="castorMarshallerBean"
class="org.springframework.oxm.castor.CastorMarshaller">
<property
name="targetClass"
value="com.smgc.Employee"></property>
<property
name="mappingLocation"
value="mapping.xml"></property>
</bean>
</beans>
mapping.xml
<?xml
version="1.0"?>
<!DOCTYPE mapping PUBLIC
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<class
name="com.smgc.Employee" auto-complete="true" >
<map-to xml="Employee"
ns-uri="http://www.smgc.com" ns-prefix="dp"/>
<field name="id"
type="integer">
<bind-xml name="id"
node="attribute"></bind-xml>
</field>
<field name="name">
<bind-xml
name="name"></bind-xml>
</field>
<field
name="salary">
<bind-xml
name="salary" type="float"></bind-xml>
</field>
</class>
</mapping>
Client.java
It gets the instance of Marshaller
from the applicationContext.xml file and calls the marshal method.
package com.smgc;
import java.io.FileWriter;
import java.io.IOException;
import
javax.xml.transform.stream.StreamResult;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
org.springframework.oxm.Marshaller;
public class Client{
public static void main(String[]
args)throws IOException{
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
Marshaller marshaller =
(Marshaller)context.getBean("castorMarshallerBean");
Employee employee=new Employee();
employee.setId(101);
employee.setName("Piyush
Patel");
employee.setSalary(100000);
marshaller.marshal(employee, new
StreamResult(new FileWriter("employee.xml")));
System.out.println("XML
Created Sucessfully");
}
}
Output
of the example
employee.xml
<?xml version="1.0"
encoding="UTF-8"?>
<dp:Employee
xmlns:dp="http://www.smgc.com" id="101">
<dp:name>Piyush
Patel</dp:name>
<dp:salary>100000.0</dp:salary>
</dp:Employee>
Comments
Post a Comment