Spring OXM - Overview



The Spring Framework provides Object/XML or O/X mapping using global marshaller/unmarshaller interfaces and allows to switch O/X mapping frameworks easily. This process of converting an object to XML is called XML Marshalling/Serialization and conversion from XML to object is called XML Demarshalling/Deserialization.

Spring framework provides a Marshaller and UnMarshaller interfaces where Marshaller interface is responsible to marshall an object to XML and UnMarshaller interface deserializes the xml to an object. Following are the key benefits of using Spring OXM framework.

  • Easy Configuration − Using spring bean context factory, creation of marshaller/unmarshaller is quite easy and is configurable without worrying about O/X libraries structures like JAXB Context, JiBX binding factories etc. A marsaller/unmarshaller can be configured as any other bean.

  • Consistent Interfacing − Marshaller and UnMarshaller are global interfaces. These interfaces provides an abstraction layer over other O/X mapping frameworks and allows to switch between them without changing the code or with little code change.

  • Consistent Exception Handling − All underlying exceptions are mapped to a XmlMappingException as root exception. Thus developers need not to worry about underlying O/X mapping tool own exception hiearchy.

Marshaller

Marshaller is an interface with single method marshal.

public interface Marshaller {
   /**
      * Marshals the object graph with the given root into the provided Result.
   */
   void marshal(Object graph, Result result)
      throws XmlMappingException, IOException;
}

Where graph is any object to be marshalled and result is a tagging interface to represent the XML output. Following are the available types −

  • javax.xml.transform.dom.DOMResult − Represents org.w3c.dom.Node.

  • javax.xml.transform.sax.SAXResult − Represents org.xml.sax.ContentHandler.

  • javax.xml.transform.stream.StreamResult − Represents java.io.File, java.io.OutputStream, or java.io.Writer.

UnMarshaller

UnMarshaller is an interface with single method unmarshal.

public interface UnMarshaller {
   /**
      * Unmarshals the given provided Source into an object graph.
   */
   Object unmarshal(Source source)
      throws XmlMappingException, IOException;
}

Where source is a tagging interface to represent the XML input. Following are the available types −

  • javax.xml.transform.dom.DOMSource − Represents org.w3c.dom.Node.

  • javax.xml.transform.sax.SAXSource − Represents org.xml.sax.InputSource, and org.xml.sax.XMLReader.

  • javax.xml.transform.stream.StreamSource − Represents java.io.File, java.io.InputStream, or java.io.Reader.

Advertisements