Home  >  Spring

Spring OXM: Marshaller and Unmarshaller Annotation Example with Castor

By Arvind Rai, March 27, 2015
In this page we will learn marshaller and unmarshaller annotation example with Castor in spring OXM. Castor is an open source XML binding framework. It allows to convert XML to/from java object. We configure CastorMarshaller as bean in our annotation based configuration file. If we are using any mapping XML file, we can refer it by using CastorMarshaller.setMappingLocation method. Castor provides castor.properties file to override its configurations using which we can achieve pretty printing of XML. We will provide here complete example and will start with the meaning of marshaller and unmarshaller.
Marshaller : We convert java object into XML.
Unmarshaller : We convert XML into java object.

Software Used

Find the software and tool which we are using in our development.
1. Java 7
2. Eclipse
3. Gradle
4. Spring-Oxm:4.1.5.RELEASE

Project Structure in Eclipse

Find our demo project structure in eclipse.
Spring OXM:  Marshaller and Unmarshaller Annotation Example  with Castor

Gradle File

Find the Gradle file to resolve JAR dependencies.
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
dependencies {
	compile 'org.springframework.boot:spring-boot-starter:1.2.2.RELEASE'
	compile 'org.springframework:spring-oxm:4.1.5.RELEASE'
	compile 'org.codehaus.castor:castor-xml:1.3.3'

Spring Annotation Configuration for CastorMarshaller

In configuration file, we need to create bean for CastorMarshaller. CastorMarshaller.setMappingLocation() is used to refer xml mapping file. Using CastorMarshaller we create the instance for Marshaller and Unmarshaller.
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.castor.CastorMarshaller;
public class AppConfig {
	public Converter getHandler(){
	  Converter handler= new Converter();
	  return handler;
	public CastorMarshaller getCastorMarshaller() {
	  CastorMarshaller castorMarshaller = new CastorMarshaller();
	  Resource resource = new ClassPathResource("mapping.xml");
	  return castorMarshaller;

Create Class for Marshaller and Unmarshaller

In this class we create methods for marshaling and unmarshaling . Spring provides two interfaces org.springframework.oxm.Marshaller and org.springframework.oxm.Unmarshaller. Marshaller interface defines the contract for Object XML mapping Marshaller and Unmarshaller interface defines the object XML mapping Unmarshaller. While writing to XML, we use StreamResult for file and while converting XML to java object we use StreamSource.
package com.concretepage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
public class Converter {
    private Marshaller marshaller;
    private Unmarshaller unmarshaller;
    public void setMarshaller(Marshaller marshaller) {
        this.marshaller = marshaller;
    public void setUnmarshaller(Unmarshaller unmarshaller) {
        this.unmarshaller = unmarshaller;
    //Converts Object to XML file
    public void doMarshaling(String fileName, Object graph) throws IOException {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fileName);
            marshaller.marshal(graph, new StreamResult(fos));
        } finally {
    //Converts XML to Java Object
    public Object doUnMarshaling(String fileName) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fileName);
            return unmarshaller.unmarshal(new StreamSource(fis));
        } finally {

Create Bean for XML Mapping

We need to create a class which will be used to map object to XML and XML to Object.
package com.concretepage.bean;
public class Country {
	private Integer id;
	private String countryName;
	private String pmName;
	public Integer getId() {
		return id;
	public void setId(Integer id) {
		this.id = id;
	public String getCountryName() {
		return countryName;
	public void setCountryName(String countryName) {
		this.countryName = countryName;
	public String getPmName() {
		return pmName;
	public void setPmName(String pmName) {
		this.pmName = pmName;

XML Pretty Printing

We can perform pretty printing while marshaling. If we need proper indentation of XML tags, we have to override castor.properties file keeping in classpath. To achieve pretty printing, configure org.exolab.castor.indent as true.
org.exolab.castor.indent = true

Map Object and XML

To get the control of creating XML and its node nomenclature and mapping java file properties to a XML attribute or element, we have to create a mapping file which will be configured with CastorMarshaller in configuration file.
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" 
		Spring OXM and Castor
    <class name="com.concretepage.bean.Country">
        <map-to xml="country-info" />
        <field name="id" type="int">
            <bind-xml name="id" node="attribute"/>
        <field name="countryName" type="string">
            <bind-xml name="country-name" node="element"/>
        <field name="pmName" type="string">
            <bind-xml name="pm-name" node="element"/>
If we use node as attribute, then that property will be considered as attribute while mapping from java object to XML and if node is element, then that java property will be mapped as element in XML.

Run Application

Finally we will test our application. We will create an object of Country class and call marshaling method. That will generate an XML file as country.xml. After this, we will call method for unmarshaling in which we will pass our XML file name that will be converted into java object.
package com.concretepage;
import java.io.IOException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.concretepage.bean.Country;
public class RunApplication {
	public static void main(String[] args) throws IOException {
	   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	   Converter converter = ctx.getBean(Converter.class);
	   //Perform Marshaling
	   Country country = new Country();
	   converter.doMarshaling("country.xml", country);
	   System.out.println("Marshaling performed");
	   //Perform UnMarshaling
	   country = (Country)converter.doUnMarshaling("country.xml");
	   System.out.println("After UnMarshaling Data is: id:"+ country.getId()+", CountryName:"+country.getCountryName());
Find the output for marshaling as an XML.
<?xml version="1.0" encoding="UTF-8"?>
<country-info id="100">
Find the output for Unmarshaling in which we get a java object.
Marshaling performed
11:05:03.124 [main] DEBUG org.exolab.castor.xml.XMLContext - Creating new Unmarshaller instance.
After UnMarshaling Data is: id:100, CountryName:India 

Download Complete Source Code


©2022 concretepage.com | Privacy Policy | Contact Us