🚀 Boost Your Spring & Java Skills with Ram N Java!
Subscribe today for practical coding guides, framework integrations, and clean Java tutorials.
🔔 Subscribe to YouTube ChannelWhat is Spring OXM and XStream?
Spring OXM (Object/XML Mapping) provides an abstraction layer that simplifies converting Java objects to XML and vice versa. XStream is a lightweight, easy-to-use library that enables XML serialization and deserialization without requiring any complex mapping files or schema generation.
Key Concepts: Marshalling & Unmarshalling
- Marshalling: Serializing a Java object (e.g., a
Companyobject) into an XML format. - Unmarshalling: Deserializing XML data back into a structured Java object.
Maven Dependencies
Add the necessary Spring OXM and XStream dependencies to your pom.xml file:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>5.3.x</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.x</version>
</dependency>
Step-by-Step Implementation
1. Define the Java Model (Company Class)
public class Company {
private int id;
private String companyName;
private String ceoName;
private int numberOfEmployees;
// Getters and Setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getCompanyName() { return companyName; }
public void setCompanyName(String companyName) { this.companyName = companyName; }
public String getCeoName() { return ceoName; }
public void setCeoName(String ceoName) { this.ceoName = ceoName; }
public int getNumberOfEmployees() { return numberOfEmployees; }
public void setNumberOfEmployees(int numberOfEmployees) { this.numberOfEmployees = numberOfEmployees; }
}
2. Configure Spring XML Configuration
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<map>
<entry key="company" value="com.ram.model.Company"/>
</map>
</property>
</bean>
3. Marshalling & Unmarshalling Execution
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Marshaller marshaller = (Marshaller) context.getBean("xstreamMarshaller");
Unmarshaller unmarshaller = (Unmarshaller) context.getBean("xstreamMarshaller");
// Convert Object to XML
Company company = new Company();
company.setId(101);
company.setCompanyName("Tech Corp");
company.setCeoName("Alice");
company.setNumberOfEmployees(500);
marshaller.marshal(company, new StreamResult(new FileOutputStream("company.xml")));
// Convert XML to Object
Company resultCompany = (Company) unmarshaller.unmarshal(new StreamSource(new FileInputStream("company.xml")));
System.out.println("Company: " + resultCompany.getCompanyName());
Related Tutorials from Ram N Java
Click here to watch on Youtube:
https://www.youtube.com/watch?v=MuqMAnjHAxM&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
| Spring and Xstream Integration | Spring Object/XML Mapping | jaxb marshalling and unmarshalling |
| Spring and Xstream Integration | Spring Object/XML Mapping | jaxb marshalling and unmarshalling |
| Spring and Xstream Integration | Spring Object/XML Mapping | jaxb marshalling and unmarshalling |
| Spring and Xstream Integration | Spring Object/XML Mapping | jaxb marshalling and unmarshalling |
| Spring and Xstream Integration | Spring Object/XML Mapping | jaxb marshalling and unmarshalling |
| Spring and Xstream Integration | Spring Object/XML Mapping | jaxb marshalling and unmarshalling |
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ram.core</groupId>
<artifactId>SpringDemo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringDemo</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>5.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
</dependencies>
</project>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ram.core</groupId>
<artifactId>SpringDemo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringDemo</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>5.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
</dependencies>
</project>
Company.java
package com.ram.core.model;
public class Company
{
private Integer id;
private String companyName;
private String ceoName;
private Integer numberOfEmployees;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getCompanyName()
{
return companyName;
}
public void setCompanyName(String companyName)
{
this.companyName = companyName;
}
public String getCeoName()
{
return ceoName;
}
public void setCeoName(String ceoName)
{
this.ceoName = ceoName;
}
public Integer getNumberOfEmployees()
{
return numberOfEmployees;
}
public void setNumberOfEmployees(Integer numberOfEmployees)
{
this.numberOfEmployees = numberOfEmployees;
}
@Override
public String toString()
{
return "Company [id=" + id + ", companyName=" + companyName
+ ", ceoName=" + ceoName + ", numberOfEmployees="
+ numberOfEmployees + "]";
}
}
public class Company
{
private Integer id;
private String companyName;
private String ceoName;
private Integer numberOfEmployees;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getCompanyName()
{
return companyName;
}
public void setCompanyName(String companyName)
{
this.companyName = companyName;
}
public String getCeoName()
{
return ceoName;
}
public void setCeoName(String ceoName)
{
this.ceoName = ceoName;
}
public Integer getNumberOfEmployees()
{
return numberOfEmployees;
}
public void setNumberOfEmployees(Integer numberOfEmployees)
{
this.numberOfEmployees = numberOfEmployees;
}
@Override
public String toString()
{
return "Company [id=" + id + ", companyName=" + companyName
+ ", ceoName=" + ceoName + ", numberOfEmployees="
+ numberOfEmployees + "]";
}
}
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-4.3.xsd">
<bean id="xstreamMarshallerBean"
class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="annotatedClasses"
value="com.ram.core.model.Company"></property>
</bean>
</beans>
<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-4.3.xsd">
<bean id="xstreamMarshallerBean"
class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="annotatedClasses"
value="com.ram.core.model.Company"></property>
</bean>
</beans>
App.java
package com.ram.core;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import com.ram.core.model.Company;
public class App
{
public static void main(String[] args) throws IOException
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
convertObjectToXML(context);
convertXMLToObject(context);
}
private static void convertObjectToXML(ApplicationContext context)
throws IOException
{
Marshaller marshaller = (Marshaller) context
.getBean("xstreamMarshallerBean");
// Perform Marshaling
Company company = new Company();
company.setId(201);
company.setCompanyName("Google");
company.setCeoName("Peter");
company.setNumberOfEmployees(50000);
marshaller.marshal(company,
new StreamResult(new FileWriter("company.xml")));
System.out.println("XML Created Sucessfully");
}
private static void convertXMLToObject(ApplicationContext context)
throws FileNotFoundException, IOException
{
Unmarshaller unmarshaller = (Unmarshaller) context
.getBean("xstreamMarshallerBean");
FileInputStream is = null;
try
{
is = new FileInputStream("company.xml");
Object object = unmarshaller.unmarshal(new StreamSource(is));
System.out.println(object);
System.out.println("Converted XML to Object!");
}
finally
{
if (is != null)
{
is.close();
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import com.ram.core.model.Company;
public class App
{
public static void main(String[] args) throws IOException
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
convertObjectToXML(context);
convertXMLToObject(context);
}
private static void convertObjectToXML(ApplicationContext context)
throws IOException
{
Marshaller marshaller = (Marshaller) context
.getBean("xstreamMarshallerBean");
// Perform Marshaling
Company company = new Company();
company.setId(201);
company.setCompanyName("Google");
company.setCeoName("Peter");
company.setNumberOfEmployees(50000);
marshaller.marshal(company,
new StreamResult(new FileWriter("company.xml")));
System.out.println("XML Created Sucessfully");
}
private static void convertXMLToObject(ApplicationContext context)
throws FileNotFoundException, IOException
{
Unmarshaller unmarshaller = (Unmarshaller) context
.getBean("xstreamMarshallerBean");
FileInputStream is = null;
try
{
is = new FileInputStream("company.xml");
Object object = unmarshaller.unmarshal(new StreamSource(is));
System.out.println(object);
System.out.println("Converted XML to Object!");
}
finally
{
if (is != null)
{
is.close();
}
}
}
}
Output:
XML Created Sucessfully
Security framework of XStream not initialized, XStream is probably vulnerable.
Company [id=201, companyName=Google, ceoName=Peter, numberOfEmployees=50000]
Converted XML to Object!
Security framework of XStream not initialized, XStream is probably vulnerable.
Company [id=201, companyName=Google, ceoName=Peter, numberOfEmployees=50000]
Converted XML to Object!
company.xml
<com.ram.core.model.Company><id>201</id><companyName>Google</companyName><ceoName>Peter</ceoName><numberOfEmployees>50000</numberOfEmployees></com.ram.core.model.Company>
Click the below link to download the code:
https://sites.google.com/site/javaspringram2019/java_spring_2019/SpringDemo_Xtream_Integration.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java_Spring_2019/tree/master/Spring_2019/SpringDemo_Xtream_Integration
Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/2db5cd1f4e7d5ab55ad7af886d6164700143c614/Spring_2019/SpringDemo_Xtream_Integration/?at=master
See also:
No comments:
Post a Comment