Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring OXM Castor Example

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.66k
    Comment on it

    Spring OXM Castor Example

    Spring OXM using Castor : Spring has the feature of xml/object binding. To bind the Object/XML spring use different api. Castor XML mapping is a way to binding the Java object to XML document. It allows to transform the data in java object into/from an XML document. Castor allows the user to specify the some of it's configuration of marshalling/unmarshalling behaviors using a xml mapping file. This file tells the run time environment that how a java object and xml document will relate to each other.
    Spring Castor marshaller is used to convert the Java object to the XML document. And spring Castor unmarshaller is used to convert XML document back to the Java object. The information about the object mapping is written in mapping file separately by developer.


    Spring Castor Example : To run this example you need to download the spring and Castor jar dependencies. You can also define those dependencies in pom so you don't need to download jars separately.


    POM.XML

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <groupId>com.evon.castor.example</groupId>
            <artifactId>spring-castor-example</artifactId>
            <version>0.0.1-SNAPSHOT</version>
           <properties>
                    <spring.version>3.2.3.RELEASE</spring.version>
            </properties>
            <dependencies>
            <!-- Spring dependencies -->
                    <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-core</artifactId>
                            <version>${spring.version}</version>
                    </dependency>
                    <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-context</artifactId>
                            <version>${spring.version}</version>
                    </dependency>
            <!-- Spring OXM -->
                    <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-oxm</artifactId>
                            <version>${spring.version}</version>
                    </dependency>
            <!-- castor dependency -->
                    <dependency>
                            <groupId>org.codehaus.castor</groupId>
                            <artifactId>castor</artifactId>
                            <version>1.2</version>
                    </dependency>
                    <dependency>
                            <groupId>xerces</groupId>
                            <artifactId>xercesImpl</artifactId>
                            <version>2.8.1</version>
                    </dependency>
            <!-- end of castor dependency -->
            </dependencies>
    </project>
    

    StudentInfo.java

    package com.evon.castor.example; 
    public class StudentInfo { 
        private long id;     
        private String name; 
        public long getId() { 
            return id; 
        } 
        public void setId(long id) { 
            this.id = id; 
        } 
        public String getName() { 
            return name; 
        } 
        public void setName(String name) { 
            this.name = name; 
        } 
    
        @Override 
        public String toString() { 
            return "StudentInfo [id=" + id + ", name=" + name + "]"; 
        }    
    } 
    

    The above is the model object which we will bind to the XML document. Now we will write a Java file which will marshal and unmarsahel the Java object.


    XMLConverter.java

    package com.evon.castor.example; 
    
    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 XMLConverter { 
        private static final String FILE = "student.xml"; 
        private StudentInfo studentObj = new StudentInfo(); 
        private Marshaller marshaller; 
        private Unmarshaller unmarshaller; 
    
        public void setMarshaller(Marshaller marshaller) { 
            this.marshaller = marshaller; 
        } 
    
        public void setUnmarshaller(Unmarshaller unmarshaller) { 
            this.unmarshaller = unmarshaller; 
        } 
    
        public void saveStudentInfo() throws IOException { 
            FileOutputStream outputStream = null; 
            try { 
                System.out.println("--- Marshaller ---"); 
                outputStream = new FileOutputStream(FILE); 
                studentObj.setId(1); 
                studentObj.setName("Sumit"); 
                this.marshaller.marshal(studentObj, new StreamResult(outputStream)); 
                System.out.println("StudentInfo " + studentObj + " saved to student.xml file. "); 
            } finally { 
                if (outputStream != null) { 
                    outputStream.close(); 
                } 
            } 
        } 
    
        public void loadStudentInfo() throws IOException { 
            FileInputStream inputStream = null; 
            try { 
                System.out.println("--- Unmarshaller ---"); 
                inputStream = new FileInputStream(FILE); 
                this.studentObj = (StudentInfo) this.unmarshaller 
                        .unmarshal(new StreamSource(inputStream)); 
                System.out.println("Info loaded from xml : " + studentObj); 
            } finally { 
                if (inputStream != null) { 
                    inputStream.close(); 
                } 
            } 
        } 
    }
    

    In above class the function saveStudentInfo() will convert the Student class object to the xml document called student.xml, and loadStudentInfo() function will convert the student.xml document back to the java object.


    Now we will register the Castor object and the mapping file in spring configuration file and inject the marshaller and unmarshaller object to the XMLConverter class.

    ApplicationContext.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
            xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
            xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:task="http://www.springframework.org/schema/task"
            xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
    
            <bean id="converterBean" class="com.evon.castor.example.XMLConverter">
                    <property name="marshaller" ref="castorMarshaller" />
                    <property name="unmarshaller" ref="castorMarshaller" />
            </bean>
            <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" >
                     <property name="mappingLocation" value="classpath:mapping.xml" />
            </bean>
    </beans>
    


    Now we will define the mapping file which will show the relationship between java object and xml document. It will show how java object properties are bind to the xml document.

    Mapping.xml

    <mapping>
            <class name="com.evon.castor.example.StudentInfo">
                    <map-to xml="student" />
                    <field name="id" type="long">
                            <bind-xml name="id" node="attribute" /> 
                    </field>
                    <field name="name" type="string">
                            <bind-xml name="name" node="element" />
                    </field>
            </class>
    </mapping>
    


    MainApp.java

    package com.evon.castor.example; 
    import java.io.IOException; 
    import org.springframework.context.ConfigurableApplicationContext; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 
    
    public class MainApp { 
        public static void main(String[] args) {     
                ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
                XMLConverter converter = (XMLConverter) context.getBean("converterBean"); 
                try { 
                    converter.saveStudentInfo(); 
                    converter.loadStudentInfo(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                }            
                context.close(); 
        } 
    } 
    


    Output :

    <?xml version="1.0" encoding="UTF-8"?>
    <info id=1><name>Sumit</name></info>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: