Splitting XML Messages
XPathMessageSplitter
支持具有 String
或 Document
有效负载的消息。分割器使用提供的 XPath 表达式将有效负载分割成多个节点。默认情况下,这样会导致每个 Node
实例成为一条新消息的有效负载。当每条消息都应该是一个 Document
时,可以设置 createDocuments
标志。当传入 String
有效负载时,有效负载将被转换,然后在转换回多个 String
消息之前进行分割。XPath 分割器实现 MessageHandler
,因此应该与适当的端点一起配置(参见以下示例之后的命名空间支持示例,了解更简单的配置替代方案)。以下示例配置了一个使用 XPathMessageSplitter
的 bean:
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 results in each Node
instance becoming the payload of a new message.
When each message should be a Document
, you can set the createDocuments
flag.
Where a String
payload is passed in, the payload is converted and then split before being converted back to a number of String
messages.
The XPath splitter implements MessageHandler
and should therefore be configured in conjunction with an appropriate endpoint (see the namespace support example after the following example for a simpler configuration alternative).
The following example configures a bean that uses an XPathMessageSplitter
:
<bean id="splittingEndpoint"
class="org.springframework.integration.endpoint.EventDrivenConsumer">
<constructor-arg ref="orderChannel" />
<constructor-arg>
<bean class="org.springframework.integration.xml.splitter.XPathMessageSplitter">
<constructor-arg value="/order/items" />
<property name="documentBuilder" ref="customisedDocumentBuilder" />
<property name="outputChannel" ref="orderItemsChannel" />
</bean>
</constructor-arg>
</bean>
XPath 分割器命名空间支持允许你使用输入通道和输出通道创建带有消息端点,如下图所示:
XPath splitter namespace support lets you create a message endpoint with an input channel and output channel, as the following example shows:
<!-- Split the order into items and create a new message for each item node -->
<int-xml:xpath-splitter id="orderItemSplitter"
input-channel="orderChannel"
output-channel="orderItemsChannel">
<int-xml:xpath-expression expression="/order/items"/>
</int-xml:xpath-splitter>
<!-- Split the order into items, create a new document for each item-->
<int-xml:xpath-splitter id="orderItemDocumentSplitter"
input-channel="orderChannel"
output-channel="orderItemsChannel"
create-documents="true">
<int-xml:xpath-expression expression="/order/items"/>
<int:poller fixed-rate="2000"/>
</int-xml:xpath-splitter>
从 4.2 版开始,当请求 payload
不是 org.w3c.dom.Node
类型时,XPathMessageSplitter
为 javax.xml.transform.Transformer
实例公开 outputProperties
(例如 OutputKeys.OMIT_XML_DECLARATION
)属性。以下示例定义了一个属性,并使用 output-properties
属性:
Starting with version 4.2, the XPathMessageSplitter
exposes the outputProperties
(such as OutputKeys.OMIT_XML_DECLARATION
) property for an javax.xml.transform.Transformer
instance when a request payload
is not of type org.w3c.dom.Node
.
The following example defines a property and uses it with the output-properties
property:
<util:properties id="outputProperties">
<beans:prop key="#{T (javax.xml.transform.OutputKeys).OMIT_XML_DECLARATION}">yes</beans:prop>
</util:properties>
<xpath-splitter input-channel="input"
output-properties="outputProperties">
<xpath-expression expression="/orders/order"/>
</xpath-splitter>
从 version 4.2
开始,XPathMessageSplitter
将 iterator
选项公开了作为一个 boolean
标志(默认为 true
)。这允许 “streaming” 在下游流中拆分节点。将 iterator
模式设置为 true
时,在迭代时每个节点都会被转换。当为 false
时,在将拆分节点开始发送到输出频道之前,首先转换所有条目。(您可以将差异视为 “transform, send, transform, send” 与 “transform, transform, send, send”)。有关详细信息,请参见 Splitter。
Starting with version 4.2
, the XPathMessageSplitter
exposes an iterator
option as a boolean
flag (defaults to true
).
This allows the “streaming” of split nodes in the downstream flow.
With the iterator
mode set to true
, each node is transformed while iterating.
When false
, all entries are first transformed, before the split nodes start being sent to the output channel.
(You can think of the difference as “transform, send, transform, send” versus “transform, transform, send, send”.)
See Splitter for more information.