Dealing with XML Payloads
Introduction
Spring Integration's XML support extends the Spring Integration Core with
implementations of splitter, transformer, selector and router designed
to make working with xml messages in Spring Integration simple. The provided messaging
components are designed to work with xml represented in a range of formats including
instances of
java.lang.String, org.w3c.dom.Document
and javax.xml.transform.Source. It should be noted however that
where a DOM representation is required, for example in order to evaluate an XPath expression,
the String payload will be converted into the required type and then
converted back again to String. Components that require an instance of
DocumentBuilder will create a namespace aware instance if one is
not provided. Where greater control of the document being created is required an appropriately
configured instance of DocumentBuilder should be provided.
Splitting xml messages
XPathMessageSplitter supports messages with either
String or Document payloads.
The splitter uses the provided XPath expression to split the payload into a number of
nodes. By default this will result in each Node instance
becoming the payload of a new message. Where it is preferred that each message be a Document
the createDocuments flag can be set. Where a String payload is passed
in the payload will be converted then split before being converted back to a number of String
messages. The XPath splitter implements MessageConsumer and should
therefore be configured in conjunction with an appropriate endpoint.
]]>
Routing xml messages using XPath
Two Router implementations based on XPath are provided XPathSingleChannelRouter and
XPathMultiChannelRouter. The implementations differ in respect to how many channels
any given message may be routed to, exactly one in the case of the single channel version
or zero or more in the case of the multichannel router. Both evaluate an XPath
expression against the xml payload of the message, supported payload types by default
are Node, Document and
String. For other payload types a custom implementation
of XmlPayloadConverter can be provided. The router
implementations use ChannelNameResolver to convert the
result(s) of the XPath expression to a channel name. By default a
BeanFactoryChannelName strategy will be used, this means that the string returned by the XPath
evaluation should correspond directly to the name of a channel. Where this is not the case
an alternative implementation of ChannelNameResolver can
be used. Where there is a simple mapping from Xpath result to channel name
the provided MapBasedChannelName can be used.
]]>
Selecting xml messages using XPath
Two MessageSelector implementations are provided,
BooleanTestXPathMessageSelector and StringValueTestXPathMessageSelector.
BooleanTestXPathMessageSelector requires an XPathExpression which evaluates to a boolean,
for example boolean(/one/two) which will only select messages which have an element named
two which is a child of a root element named one. StringValueTestXPathMessageSelector
evaluates any XPath expression as a String and compares the result with the provided value.
]]>
XPath components namespace support
All XPath based components have namespace support allowing them to be configured as instances of
MessageEndpoint with the exception of the XPath selectors which are not designed to act as
endpoints. Each component allows the XPath to either be referenced at the top level or configured via a nested
xpath-expression element. So the following configurations of an xpath-selector are all valid and represent the general
form of XPath namespace support. All forms of XPath expression result in the creation of an
XPathExpression using the Spring XPathExpressionFactory
]]>
XPath splitter namespace support allows the creation of a MessageEndpoint with an input channel and output channel.
]]>
XPath router namespace support allows for the creation of a MessageEndpoint with an input channel but no output channel
since the output channel is determined dynamically. The multi-channel attribute causes the creation of a multi channel router capable of
routing a single message to many channels when true and a single channel router when false.
]]>