Spring with Castor Example
With the use of CastorMarshaller class, we can achieve marshal a java object into XML code and vice-versa of it with the help of using castor. The castor is the implemented class for Marshaller and Unmarshaller interfaces within it, thus it does not require other further configurations by its default. The most common example of Spring and Castor Integration is ‘Marshalling the Java Object into the XML.
Implementation: We need to create the following files for marshaling Java objects within the XML use of Spring with the Castor:
- Worker.java
- aplcontext.xml
- mapper.xml
- User.java
The requirement of Jar files is as follows:
Note: For Running this example, we need to load the following files:
- Spring Core jar files
- Spring Web jar files
- castor-1.3.jar
- castor-1.3-core.jar
A. File: Worker.java
This file defines the three properties that are id, name, and salary of worker with setters and getters.
Java
// Java Program to Illustrate Worker Class package com.geeksforgeeks; // Class public class Worker { // Class data members // Worker id private int id; // Worker name private String name; // Worker Salary private float sal; // Getter public int gettingId() { return id; } // Setter public void settingId( int id) { this .id = id; } // Getter public String gettingName() { return name; } // Setter public void settingName(String name) { this .name = name; } // Getter public float gettingSal() { return salary; } // Setter public void settingSal( float sal) { this .sal = sal; } } |
B. File: aplcontext.xml
this file is defining a bean castorMarshallerBean as ‘Worker class’ is bounded at with the OXM framework
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schemas/beans < bean id = "castorMarshallerBean" class = "org.springframework.oxm.castor.CastorMarshaller" > < property name = "targetClass" value = "com.geeksforgeeks.Worker" > </ property > < property name = "mappingLocation" value = "mapper.xml" > </ property > </ bean > </ beans > |
C. File: mapper.xml
XML
<? xml version = "1.0" ?> <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTED Version 1.0//EN" < mapping > < class name = "com.geeksforgeeks.Worker" auto-complete = "true" > < field name = "id" type = "integer" > < bind-xml name = "id" node = "attribute" > </ bind-xml > </ field > < field name = "name" > < bind-xml name = "name" > </ bind-xml > </ field > < field name = "sal" > < bind-xml name = "sal" type = "float" > </ bind-xml > </ field > </ class > </ mapping > |
D. File: User.java
This file is getting instance of the Marshaller from within the aplcontext.xml file and calls the marshal method through it
Java
// Java Program to Illustrate Application Class package com.geeksforgeeks; // Importing required classes import java.io.FileWriter; import java.io.IOException; import javax.xml.transform.stream.StreamResult; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.oxm.Marshaller; // Main Class public class User { // Main driver method public static void main(String[] args) throws IOException { // Creating object of ApplicationContext and // Marshaller class ApplicationContext context = new ClassPathXmlApplicationContext( "aplcontext.xml" ); Marshaller marshaller = (Marshaller)context.getBean( "castorMarshallerBean" ); // Creating object of Worker class Worker worker = new Worker(); worker.settingId( 123 ); worker.settingName( "Rushikesh Khurpade" ); worker.settingSal( 300000 ); marshaller.marshal(worker, new StreamResult( new FileWriter( "worker.xml" ))); // Display message for successful execution // of program System.out.println( " XML Created Successfully " ); } } |
Output:
XML Created Successfully
File: worker.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < dp:name >Rushikesh Khurpade</ dp:name > < dp:salary >300000.0</ dp:salary > </ dp:Employee > |
Please Login to comment...