Spring OXM with XStream example

Here is a simple example to marshalling/unmarshalling xml content to/from java object using Spring XOM.

Here I am generating xml content from java object, writing the content to file system, reading that back to java object.

Spring configuration – applicationContext.xml

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

<bean id=”xstreamExample”>

<property name=”marshaller” ref=”xstreamMarshaller” />

</bean>

<bean id=”xstreamMarshaller”>

<property name=”aliases”>

<props>

<prop key=”bean”>com.example.ConfigurationBean</prop>

</props>

</property>

</bean>

</beans>

Simple java bean – ConfigurationBean.java

package com.example;

 

public class ConfigurationBean {

 

private String name;
private Integer age;
private String jobDescription;
private boolean executive;

 

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getJobDescription() {
return jobDescription;
}
public void setJobDescription(String jobDescription) {
this.jobDescription = jobDescription;
}
public boolean isExecutive() {
return executive;
}
public void setExecutive(boolean executive) {
this.executive = executive;
}
}

Spring bean with main main method (for testing): XStreamExample.java

package com.example;

 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.xstream.XStreamMarshaller;

 

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

 

/**
* Example to marshal/unmarshal xml to/from java object using Spring 3.0 OXM feature.
*
* @author Anil Sadineni
*/
public class XStreamExample {
private static final String F_NAME = "example.xml";
private List configurationBeans;

 

private XStreamMarshaller marshaller;

 

public void setMarshaller(XStreamMarshaller marshaller) {
this.marshaller = marshaller;
Map alias = new HashMap();
alias.put("configurations", List.class);
alias.put("configuration", ConfigurationBean.class);
try {
this.marshaller.setAliases(alias);
} catch (Exception e) {
e.printStackTrace();
}
}

 

public void loadConfigurationBeans() throws IOException {
FileInputStream is = null;
try {
is = new FileInputStream(F_NAME);
this.configurationBeans = (List) this.marshaller.unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}

 

public void saveConfigurationBeans() throws IOException {
FileOutputStream os = null;
try {
os = new FileOutputStream(F_NAME);
this.marshaller.marshal(configurationBeans, new StreamResult(os));
} finally {
if (os != null) {
os.close();
}
}
}

 

public static void main(String[] args) throws IOException {
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
XStreamExample ex = (XStreamExample) appContext.getBean("xstreamExample");
ex.execute();
}

private void execute() throws IOException {
configurationBeans = getConfigurationBeans();

saveConfigurationBeans();
loadConfigurationBeans();

for (ConfigurationBean configurationBean : configurationBeans) {
System.out.println(“name: ” + configurationBean.getName());
System.out.println(“desc : ” + configurationBean.getJobDescription());
System.out.println(“age: ” + configurationBean.getAge());
System.out.println(“Exec : ” + configurationBean.isExecutive());
}
}

private List getConfigurationBeans() {
List beans = new ArrayList();

for (int i = 1; i <= 5; i++) {
ConfigurationBean configurationBean = new ConfigurationBean();
configurationBean.setAge(26);
configurationBean.setExecutive(false);
configurationBean.setJobDescription(“Jab Desc – ” + i);
configurationBean.setName(“Anil S – ” + i);
beans.add(configurationBean);
}

return beans;
}
}

 

Output on console:
name: Anil S – 1
desc : Jab Desc – 1
age: 26
Exec : false
name: Anil S – 2
desc : Jab Desc – 2
age: 26
Exec : false
name: Anil S – 3
desc : Jab Desc – 3
age: 26
Exec : false
name: Anil S – 4
desc : Jab Desc – 4
age: 26
Exec : false
name: Anil S – 5
desc : Jab Desc – 5
age: 26
Exec : false

Generated XML content (example.xml) –

<configurations>

<configuration>

<name>Anil S – 1</name>

<age>26</age>

<jobDescription>Jab Desc – 1</jobDescription>

<executive>false</executive>

</configuration>

<configuration>

<name>Anil S – 2</name>

<age>26</age>

<jobDescription>Jab Desc – 2</jobDescription>

<executive>false</executive>

</configuration>

<configuration>

<name>Anil S – 3</name>

<age>26</age>

<jobDescription>Jab Desc – 3</jobDescription>

<executive>false</executive>

</configuration>

<configuration>

<name>Anil S – 4</name>

<age>26</age>

<jobDescription>Jab Desc – 4</jobDescription>

<executive>false</executive>

</configuration>

<configuration>

<name>Anil S – 5</name>

<age>26</age>

<jobDescription>Jab Desc – 5</jobDescription>

<executive>false</executive>

</configuration>