Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring OXM with XStream API

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.07k
    Comment on it

    Spring with XStream : XStream is a simple library to serialize the Java object to the XML document and vice versa.We can use Spring OXM with XStream library for XML binding. The benefit of the XStream library over Castor is that XStream does not use any mapping file like Castor does.

    Spring with Xstream example : First you need the XStream library to your project lib. So download the xstream-x.x.jar or put the following maven dependency in pom.xml.

    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.xstream.example</groupId>
            <artifactId>spring-xstream-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>
            <!-- XStream  dependency -->
            <dependency>
                <groupId>com.evon.xstream</groupId>
                <artifactId>xstream</artifactId>
            <version>1.3.1</version>
    </dependency>
            </dependencies>
    </project>
    

    Employee.java

    package com.evon.xstream.example; 
    public class Employee { 
    
        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 "Employee details [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 unmarshall the Java object.

    XMLConverter.java

    package com.evon.xstream.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 = "Employee.xml"; 
        private Employee employee = new Employee(); 
        private Marshaller marshaller; 
        private Unmarshaller unmarshaller; 
    
        public void setMarshaller(Marshaller marshaller) { 
            this.marshaller = marshaller; 
        } 
    
        public void setUnmarshaller(Unmarshaller unmarshaller) { 
            this.unmarshaller = unmarshaller; 
        } 
    
        public void objectToXML() throws IOException { 
            FileOutputStream outputStream = null; 
            try { 
                System.out.println("--- Marshaller ---"); 
                outputStream = new FileOutputStream(FILE); 
                employee.setId(123); 
                employee.setName("Rahul"); 
                this.marshaller.marshal(employee, new StreamResult(outputStream)); 
                System.out.println("Employee Info " + employee + " saved to empoyee.xml file. "); 
            } finally { 
                if (outputStream != null) { 
                    outputStream.close(); 
                } 
            } 
        } 
    
        public void xmlToObject() throws IOException { 
            FileInputStream inputStream = null; 
            try { 
                System.out.println("--- Unmarshaller ---"); 
                inputStream = new FileInputStream(FILE); 
                this. employee = (Employee) this.unmarshaller 
                        .unmarshal(new StreamSource(inputStream)); 
                System.out.println("Info loaded from xml : " + employee); 
            } finally { 
                if (inputStream != null) { 
                    inputStream.close(); 
                } 
            } 
        } 
    } 
    

    In above class the function objectToXML() will convert the Employee class object to the XML document called Employee.xml, and xmlToObject() function will convert the Employee.xml document back to the Java object.

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

    ApplicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>  
        <beans xmlns="http://www.springframework.org/schema/beans"  
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    
        <bean id="converterBean" class="com.evon.jaxb.example.XMLConverter">
             <property name="marshaller" ref="xstreamMarshaller" />
             <property name="unmarshaller" ref="xstreamMarshaller" />
          </bean>
        <bean id="xstreamMarshallerBean" class="org.springframework.oxm.xstream.XStreamMarshaller">  
            <property name="annotatedClasses" value="com.evon.xstream.Employee"></property>  
        </bean>  
      </beans>  
    

    MainApp.java

    package com.evon.xstream.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.objectToXML(); 
                    converter.xmlToObject(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                }            
                context.close(); 
        } 
    } 
    

    Output :

    <?xml version="1.0" encoding="UTF-8"?>
    <info><id>123</id><name>Rahul</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: