Home  >  Spring Core

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.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
        mavenCentral()
}
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.
AppConfig.java
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;
@Configuration
public class AppConfig {
	@Bean
	public Converter getHandler(){
	  Converter handler= new Converter();
	  handler.setMarshaller(getCastorMarshaller());
	  handler.setUnmarshaller(getCastorMarshaller());
	  return handler;
	}
	@Bean
	public CastorMarshaller getCastorMarshaller() {
	  CastorMarshaller castorMarshaller = new CastorMarshaller();
	  Resource resource = new ClassPathResource("mapping.xml");
	  castorMarshaller.setMappingLocation(resource);
	  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.
Converter.java
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 {
        	fos.close();
        }
    }
    //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 {
        	fis.close();
        }
    }
} 

Create Bean for XML Mapping

We need to create a class which will be used to map object to XML and XML to Object.
Country.java
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.
castor.properties
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.
mapping.xml
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" 
     "http://castor.org/mapping.dtd">
<mapping>
    <description>
		Spring OXM and Castor
    </description>
    <class name="com.concretepage.bean.Country">
        <map-to xml="country-info" />
        <field name="id" type="int">
            <bind-xml name="id" node="attribute"/>
        </field>
        <field name="countryName" type="string">
            <bind-xml name="country-name" node="element"/>
        </field>
        <field name="pmName" type="string">
            <bind-xml name="pm-name" node="element"/>
        </field>
    </class>
 </mapping>    
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.
RunApplication.java
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();
	   ctx.register(AppConfig.class);
	   ctx.refresh();
	   Converter converter = ctx.getBean(Converter.class);
	   //Perform Marshaling
	   Country country = new Country();
	   country.setId(100);
	   country.setCountryName("India");
	   country.setPmName("ABC");
	   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.
country.xml
<?xml version="1.0" encoding="UTF-8"?>
<country-info id="100">
    <country-name>India</country-name>
    <pm-name>ABC</pm-name>
</country-info> 
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

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us