Javatpoint Logo
Javatpoint Logo

Spring with Castor Example

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.


applicationContext.xml

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


mapping.xml

Client.java

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

Output of the example

employee.xml




Please Share

facebook twitter google plus pinterest tumbler

Learn Latest Tutorials