XStream — Java Object to XML and XML to Java Object | Code Factory
2 min readApr 15, 2020
Reference Link : Link
Donate : Link
XStreamTranslator.java
package com.codeFactory;import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;import javax.xml.parsers.DocumentBuilderFactory;import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.hibernate.converter.HibernatePersistentCollectionConverter;
import com.thoughtworks.xstream.hibernate.converter.HibernatePersistentMapConverter;
import com.thoughtworks.xstream.hibernate.converter.HibernatePersistentSortedMapConverter;
import com.thoughtworks.xstream.hibernate.converter.HibernatePersistentSortedSetConverter;
import com.thoughtworks.xstream.hibernate.converter.HibernateProxyConverter;
import com.thoughtworks.xstream.hibernate.mapper.HibernateMapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;/**
* @author code.factory
*
*/
public final class XStreamTranslator {
private XStream xstream = null;private XStreamTranslator() {
xstream = new XStream();
xstream.ignoreUnknownElements();
}/**
* Convert Object to a XML String
*
* @param object
* @return String
*/
public String toXMLString(Object object) {
return xstream.toXML(object);
}/**
* Convert xml String to an Object
*
* @param xml
* @return Object
*/
public Object toObject(String xml) {
return (Object) xstream.fromXML(xml);
}/**
* Returns an Object from File
*
* @param xmlFile
* @return Object
* @throws IOException
*/
public Object toObject(File xmlFile) throws IOException {
return xstream.fromXML(new FileReader(xmlFile));
}/**
* Create File from the Object
*
* @param objTobeXMLTranslated
* @param fileName
* @throws IOException
*/
public void toXMLFile(Object objTobeXMLTranslated, String fileName)
throws IOException {
FileWriter writer = new FileWriter(fileName);
xstream.toXML(objTobeXMLTranslated, writer);
writer.close();
}/**
* Create File from the Object, File name is generated automatically
*
* @param objTobeXMLTranslated
* @throws IOException
*/
public void toXMLFile(Object objTobeXMLTranslated) throws IOException {
toXMLFile(objTobeXMLTranslated, objTobeXMLTranslated.getClass().getName() + ".xml");
}/**
* Create Document from String
*
* @param input
* @return Document
* @throws Exception
*/
public Document readFromXml(String input) throws Exception {
try {
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(IOUtils.toInputStream(input));
} catch (Exception e) {
throw new Exception("Exception occured while converting input String into document", e);
}
}/**
* Create String from Object, Initialize Java Object and create XML
*
* @param objectToWrite
* @return
*/
public String initializeAndWriteToXml(Object objectToWrite) {
final XStream xstream = new XStream() {
@Override
protected MapperWrapper wrapMapper(final MapperWrapper next) {
return new HibernateMapper(next);
}
};
xstream.registerConverter(new HibernateProxyConverter());
xstream.registerConverter(new HibernatePersistentCollectionConverter(xstream.getMapper()));
xstream.registerConverter(new HibernatePersistentMapConverter(xstream.getMapper()));
xstream.registerConverter(new HibernatePersistentSortedMapConverter(xstream.getMapper()));
xstream.registerConverter(new HibernatePersistentSortedSetConverter(xstream.getMapper()));return xstream.toXML(objectToWrite);
}/**
* Create Object from String and targeted Object
*
* @param xmlContent
* @param targetObjectClass
* @return T
*/
public <T> T readFromXml(String xmlContent, Class<T> targetObjectClass) {
return targetObjectClass.cast(xstream.fromXML(xmlContent));
}/**
* Java main method
*
* @param args
* @throws Exception
*/
public static void main(String... args) throws Exception {
String xmlString;
Employee xmlEmployee;
XStreamTranslator xStreamTranslator = new XStreamTranslator();
xmlString = xStreamTranslator.toXMLString(new Employee());
System.out.println(xmlString);
xmlEmployee = (Employee) xStreamTranslator.toObject(xmlString);
System.out.println(xmlEmployee);
System.out.println(xStreamTranslator.toObject(new File("XmlFile.xml")));
xStreamTranslator.toXMLFile(xmlEmployee, "XmlFile1.xml");
xStreamTranslator.toXMLFile(xmlEmployee);
System.out.println(xStreamTranslator.readFromXml(xmlString).getFirstChild().getTextContent());
System.out.println(xStreamTranslator.initializeAndWriteToXml(xmlEmployee)); // new Employee()
System.out.println(xStreamTranslator.readFromXml(xmlString, Employee.class));
}
}
Employee.java
package com.codeFactory;/**
* @author code.factory
*
*/
public class Employee {
String name;public Employee() {
name = "Code Factory";
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}@Override
public String toString() {
return "Employee [name : " + name + "]";
}
}
XmlFile.xml
<?xml version="1.0" encoding="UTF-8"?>
<com.codeFactory.Employee>
<name>Hello Code Factory</name>
</com.codeFactory.Employee>