Flat Preloader Icon

Spring with Castor

Castor is a Java data binding framework that allows you to map XML data to Java objects and vice versa. In this example, we will create a Spring application that uses Castor for XML data binding.

Here are the steps to create a Spring application with Castor:

Step 1: Set up your project Create a new Java project and add the necessary dependencies for Spring and Castor to your project’s build file (e.g., Maven or Gradle). You can include the following dependencies in your pom.xml for Maven:

    <!-- Spring dependencies -->

    <!-- Castor dependencies -->


Step 2: Create a Java class for your data object Let’s assume you have an XML document with the following structure:

    <name>John Doe</name>


Create a Java class that represents this XML structure:

					public class Person {
    private String name;
    private int age;

    // getters and setters


Step 3: Create a Castor mapping file Create a Castor mapping file that defines how Castor should map XML elements to Java objects. Save this XML file as person-mapping.xml:

					<class name="Person">
    <map-to xml="person"/>
    <field name="name" type="string">
        <bind-xml name="name"/>
    <field name="age" type="int">
        <bind-xml name="age"/>


Step 4: Configure Spring to use Castor Create a Spring configuration file (e.g., spring-config.xml) and configure Spring to use Castor for XML data binding. Include the Castor mapping file in your configuration:

					<beans xmlns="http://www.springframework

 <!-- Configure Castor as the data 
 binding framework -->
 <bean id="castorMarshaller" class="org
 <property name="mappingLocation" 

    <!-- Create a bean that uses Castor
    for XML data binding -->
    <bean id="personService" 
        <property name="marshaller" 


Step 5: Create a service class Create a service class (PersonService) that uses Castor for XML data binding:

					import org.springframework.oxm.Marshaller;

public class PersonService {
    private Marshaller marshaller;

    public void setMarshaller(Marshaller
    marshaller) {
        this.marshaller = marshaller;

    public String marshalPerson(Person person) 
    throws Exception {
        StringWriter writer 
        = new StringWriter();
        new StreamResult(writer));
        return writer.toString();

    public Person unmarshalPerson(String xml)
    throws Exception {
        return (Person) marshaller.unmarshal
        (new StreamSource(new StringReader(xml)));


Step 6: Use the Spring application Finally, you can use the Spring application to marshal and unmarshal XML data using Castor. Here’s a simple example:

					public class MainApp {
    public static void main(String[] args)
    throws Exception {
        ApplicationContext context
        = new ClassPathXmlApplicationContext

        PersonService personService =
        (PersonService) context.getBean

        // Create a Person object
        Person person = new Person();
        person.setName("John Doe");

        // Marshal the Person object to XML
        String xml = personService
        representation:\n" + xml);

        // Unmarshal XML to a Person object
        Person unmarshalledPerson = 
        System.out.println("Unmarshalled Person
        :\n" + unmarshalledPerson.getName() 
        + ", " + 


This example demonstrates how to configure a Spring application to use Castor for XML data binding. You can now customize it further for your specific use case and XML structures.

Share on: