Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Spring with Castor


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

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

download castor-1.3.jar

download castor-1.3-core.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 castorMarshallerBean 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=“castorMarshallerBean” class=“org.springframework.oxm.castor.CastorMarshaller”>
  7.         <property name=“targetClass” value=“com.Kreationnext.Employee”></property>
  8.         <property name=“mappingLocation” value=“mapping.xml”></property>
  9.     </bean>
  10. </beans>

mapping.xml

  1. <?xml version=“1.0”?>
  2. <!DOCTYPE mapping PUBLIC “-//EXOLAB/Castor Mapping DTD Version 1.0//EN”
  3.                          “http://castor.org/mapping.dtd”>
  4.  <mapping>
  5.     <class name=“com.Kreationnext.Employee” auto-complete=“true” >
  6.         <map-to xml=“Employee” ns-uri=“http://www.Kreationnext.com” ns-prefix=“dp”/>
  7.         <field name=“id” type=“integer”>
  8.             <bind-xml name=“id” node=“attribute”></bind-xml>
  9.         </field>
  10.         <field name=“name”>
  11.             <bind-xml name=“name”></bind-xml>
  12.         </field>
  13.         <field name=“salary”>
  14.             <bind-xml name=“salary” type=“float”></bind-xml>
  15.         </field>
  16.     </class>
  17.  </mapping>

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(“castorMarshallerBean”);
  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. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <dp:Employee xmlns:dp=“http://www.Kreationnext.com” id=“101”>
  3. <dp:name>Sonoo Jaiswal</dp:name>
  4. <dp:salary>100000.0</dp:salary>
  5. </dp:Employee>

Comments

comments