Spring Oxm 简明教程

Spring OXM - Overview

Spring 框架使用全局 marshall/unmarshall 接口提供对象/XML 或 O/X 映射,并允许轻松切换 O/X 映射框架。将对象转换为 XML 的此过程称为 XML 编组/序列化,而将 XML 转换为对象的过程称为 XML 解编组/反序列化。

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 框架提供了一个 Marshall 和 UnMarshaller 接口,其中 Marshall 接口负责将对象编组到 XML,而 UnMarshaller 接口将 xml 反序列化到一个对象。以下是使用 Spring OXM 框架的主要优点。

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.

  1. 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.

  2. 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.

  3. 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 是一个具有单个方法 marshal 的接口。

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;
}

其中 graph 是要编组的任意对象,而 result 是表示 XML 输出的标记接口。以下是可用类型−

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

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

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

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

UnMarshaller

UnMarshaller 是一个具有单个方法 unmarshal 的接口。

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;
}

其中 source 是用于表示 XML 输入的标记化界面。以下是可用的类型:

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

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

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

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