Another reason to love Spring... OXM...
Basically there are 5 common Object to XML mapping projects out there... Castor, JaxB, XStream ,JibX and xmlBeans. Since I am lazy an a fan of reuse I won't be going into JibX as this other blogger (Guido Schumtz) did it quite nicely:
JibX Maven Spring OXM
I won't describe xmlBeans either, 'cause as soon as I saw that the objects need to extend XmlObject I lost interest... I don't wanna have to change my class hierarchy just to do mappings, that is so very 90's.
I did however slap together a quick Castor, JaxB and XStream example:
You need a bean that you want to map in and out of xml in this case (ClassToXMLMap.java)
package javaitzen.spring.oxm; import javax.xml.bind.annotation.XmlRootElement; import com.thoughtworks.xstream.annotations.XStreamAlias; @XmlRootElement(name="ClassToXMLMap") @XStreamAlias( "ClassToXMLMap" ) public class ClassToXMLMap { private String data; private String history; public String getData() { return data; } public void setData(String data) { this.data = data; } public String getHistory() { return history; } public void setHistory(String history) { this.history = history; } }
Then a class to use the Spring marshallers:
package javaitzen.spring.oxm; 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 OXMarshall { private Marshaller marshaller; private Unmarshaller unmarshaller; public void setMarshaller(Marshaller marshaller) { this.marshaller = marshaller; } public void setUnmarshaller(Unmarshaller unmarshaller) { this.unmarshaller = unmarshaller; } public void saveXML(ClassToXMLMap message, String fileName) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(fileName); this.marshaller.marshal(message, new StreamResult(fos)); } finally { if (fos != null) { fos.close(); } } } public ClassToXMLMap loadXML(String fileName) throws IOException { FileInputStream fis = null; try { fis = new FileInputStream(fileName); return (ClassToXMLMap) this.unmarshaller.unmarshal(new StreamSource(fis)); } finally { if (fis != null) { fis.close(); } } } }
The JUnit Test:
package javaitzen.spring.oxm; import static org.junit.Assert.assertNotNull; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations={"/javaitzen/spring/oxm/applicationContext.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class OXMTest { @Autowired OXMarshall castor; @Autowired OXMarshall jax; @Autowired OXMarshall xtream; ClassToXMLMap message; @Before public void setup(){ message = new ClassToXMLMap(); message.setData("I am data"); message.setHistory("in the past"); } @Test public void testCastor() throws Exception{ String castorFile = "castor.xml"; castor.saveXML(message, castorFile); assertNotNull(castor.loadXML(castorFile)); } @Test public void testJaxB() throws Exception{ String jaxbFile = "jaxb.xml"; jax.saveXML(message,jaxbFile); assertNotNull(jax.loadXML(jaxbFile)); } @Test public void testXStream() throws Exception{ String xtreamFile = "xtream.xml"; xtream.saveXML(message,xtreamFile); assertNotNull(xtream.loadXML(xtreamFile)); } }
The mapping file for Castor:
The application context for the Test:
Hint: If you want to use xmlBeans or JibX there are tags defined for them by Spring, and you will be able to substitute them for the ones in my example.
Last but not least the Maven pom file: