请注意您的输入 xml 无效,请修复根和基本标记以使其有效
#Solution1 由于您还没有准备好使用 Dataweave,下面是另一个解决方案,根据需要使用xslt转换器和 XPath:-:-
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<logger message="input xml:- #[payload]" level="INFO" doc:name="Logger"/>
<mulexml:xslt-transformer xsl-file="transform.xsl" maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT" mimeType="application/xml">
</mulexml:xslt-transformer>
</flow>
将 transform.xsl 放在src/main/resource
location 下:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:for-each select="root/base/status">
<status>
<type>
<xsl:value-of select="type" />
</type>
<set>
<xsl:value-of select="set" />
</set>
</status>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
#Solution2:使用XPath3和for-each
在流程中:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<expression-component doc:name="Expression">
<![CDATA[
StringBuffer sb = new StringBuffer();
flowVars.stBuffer=sb;
]]>
</expression-component>
<foreach collection="#[xpath3('//root/base/status', message.payload, 'NODESET')]" doc:name="For Each">
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<logger message="#[payload.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "")]" level="INFO" doc:name="Logger"/>
<set-payload value="#[payload.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "")]" doc:name="Set Payload"/>
<set-payload value="#[flowVars.stBuffer.append(message.payload).toString()]" doc:name="Set Payload"/>
</foreach>
<set-payload value="#[flowVars.stBuffer]" doc:name="Set Payload" mimeType="application/xml"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
</flow>
两种情况下,响应如下: