26.2 Transforming xml payloads

This section will explain the workings of XmlPayloadUnmarshallingTransformer, XmlPayloadMarshallingTransformer, XsltPayloadTransformer and how to configure them as beans. All of the provided xml transformers extend AbstractPayloadTransformer and therefore implement Transformer. When configuring xml transformers as beans in Spring Integration you would normally configure the transformer in conjunction with either a MessageTransformingChannelInterceptor or a MessageTransformingHandler. This allows the transformer to be used as either an interceptor, which transforms the message as it is sent or received to the channel, or as an endpoint. Finally the namespace support will be discussed which allows for the simple configuration of the transformers as elements in XML.

XmlPayloadUnmarshallingTransformer allows an xml Source to be unmarshalled using implementations of Spring OXM Unmarshaller. Spring OXM provides several implementations supporting marshalling and unmarshalling using JAXB, Castor and JiBX amongst others. Since the unmarshaller requires an instance of Source where the message payload is not currently an instance of Source, conversion will be attempted. Currently String and org.w3c.dom.Document payloads are supported. Custom conversion to a Source is also supported by injecting an implementation of SourceFactory.

<bean id="unmarshallingTransformer"
      class="org.springframework.integration.xml.transformer.XmlPayloadUnmarshallingTransformer">
    <constructor-arg>
        <bean class="org.springframework.oxm.jaxb.Jaxb1Marshaller">
		    <property name="contextPath" value="org.example" />
        </bean>
    </constructor-arg>
</bean>

The XmlPayloadMarshallingTransformer allows an object graph to be converted into xml using a Spring OXM Marshaller. By default the XmlPayloadMarshallingTransformer will return a DomResult. However the type of result can be controlled by configuring an alternative ResultFactory such as StringResultFactory. In many cases it will be more convenient to transform the payload into an alternative xml format. To achieve this configure a ResultTransformer. Two implementations are provided, one which converts to String and another which converts to Document.

<bean id="marshallingTransformer"
      class="org.springframework.integration.xml.transformer.XmlPayloadMarshallingTransformer">
    <constructor-arg>
        <bean class="org.springframework.oxm.jaxb.Jaxb1Marshaller">
            <property name="contextPath" value="org.example" />
        </bean>
    </constructor-arg>
    <constructor-arg>
        <bean class="org.springframework.integration.xml.transformer.ResultToDocumentTransformer" />
    </constructor-arg>
</bean>

XsltPayloadTransformer transforms xml payloads using xsl. The transformer requires an instance of either Resource or Templates. Passing in a Templates instance allows for greater configuration of the TransformerFactory used to create the template instance. As in the case of XmlPayloadMarshallingTransformer by default XsltPayloadTransformer will create a message with a Result payload. This can be customised by providing a ResultFactory and/or a ResultTransformer.

<bean id="xsltPayloadTransformer"
      class="org.springframework.integration.xml.transformer.XsltPayloadTransformer">
    <constructor-arg value="classpath:org/example/xsl/transform.xsl" />
    <constructor-arg>
        <bean class="org.springframework.integration.xml.transformer.ResultToDocumentTransformer" />
    </constructor-arg>
</bean>