Mule ESB 3.6-将BufferedInputStream转换为XML的最佳方法 [英] Mule ESB 3.6 - Best way to convert BufferedInputStream to XML
问题描述
我有一个使用HTTP连接器的Mule ESB项目,我想在其中使用XPath来拆分/路由XML.
I have a Mule ESB project using HTTP Connector where I want to use XPath to split/route the XML.
HTTP连接器之后的消息有效负载是org.glassfish.grizzly.utils.BufferInputStream.
The message payload after the HTTP Connector is a org.glassfish.grizzly.utils.BufferInputStream.
将其转换为可以使用诸如"Splitter"或"Expression"(使用XPath)之类的组件来拆分/路由的类型的最佳方法是什么?
What is the best way to transform this to a type where I can use a component such as a 'Splitter' or 'Expression' (using XPath) to split/route it?
当有效负载为字符串(即在HTTP之后使用对象到字符串)时,对象到XML"似乎不起作用,拆分器也不起作用.如果有标准组件可以做到这一点,我宁愿不编写自定义Java转换器?
The 'Object to XML' doesn't seem to work and the splitter doesn't work when the payload is a String (i.e. using Object to String after HTTP). I would rather not write a custom Java transformer if there are standard components that could do this?
流量
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
<flow name="collectionsplitterFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/CollectionSplitter" doc:name="HTTP"/>
<splitter expression="#[xpath3('//Order')]" doc:name="Splitter"/>
<logger level="INFO" doc:name="Logger"/>
</flow>
要拆分的XML
<?xml version="1.0" encoding="UTF-8"?>
<msglist>
<msg>
<Order>
<Id>1</Id>
<Description>Order 1</Description>
</Order>
</msg>
<msg>
<Order>
<Id>2</Id>
<Description>Order 2</Description>
</Order>
</msg>
</msglist>
谢谢 大卫
推荐答案
Thanks to the answer for this stackoverflow question helped provide the solution to my question.
答案:
将NODESET选项添加到xpath3 MEL,然后将DOM转换为XML转换器,以将DOM对象转换回XML.
Add the NODESET option to the xpath3 MEL and then a DOM to XML transformer to convert the DOM object back to XML.
#[xpath3('//Order',payload,'NODESET')]
我还必须将所有拆分组件添加到一个子流中,尤其是对于我的HTTP源(因为我不断收到payloadAsString()错误.
I also had to add all the splitting components into a sub-flow, especially for my HTTP source (as I kept getting payloadAsString() errors.
M子流量
Mule Flow
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
<file:connector name="PickupFile2" autoDelete="true" streaming="false" validateConnections="true" doc:name="File"/>
<mulexml:object-to-xml-transformer returnClass="java.lang.String" encoding="UTF-8" name="Object_to_XML" doc:name="Object to XML"/>
<mulexml:dom-to-xml-transformer name="DOM_to_XML" doc:name="DOM to XML"/>
<flow name="collectionsplitterFlow2">
<http:listener config-ref="HTTP_Listener_Configuration" path="/CollectionSplitter" doc:name="HTTP"/>
<set-session-variable variableName="Filename" value="HTTP" doc:name="Session Variable"/>
<flow-ref name="collectionsplitterSub_Flow" doc:name="collectionsplitterSub_Flow"/>
<set-payload value="Response2" doc:name="Set Payload"/>
</flow>
<sub-flow name="collectionsplitterSub_Flow">
<splitter expression="#[xpath3('//Order',payload,'NODESET')]" doc:name="Splitter"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<file:outbound-endpoint path="C:\Mule\CollectionSplitter\Backup\01_SplitFile" outputPattern="#[sessionVars.Filename]-split2-#[function:datestamp].xml" connector-ref="File" responseTimeout="10000" transformer-refs="DOM_to_XML" doc:name="File"/>
</sub-flow>
<flow name="collectionsplitterFlow">
<file:inbound-endpoint path="C:\Mule\CollectionSplitter\In" moveToPattern="#[message.inboundProperties['originalFilename']]-backup-#[function:datestamp].xml" moveToDirectory="C:\Mule\CollectionSplitter\Backup" connector-ref="PickupFile2" responseTimeout="10000" doc:name="File">
<file:filename-regex-filter pattern="(.*).xml" caseSensitive="false"/>
</file:inbound-endpoint>
<set-session-variable variableName="Filename" value="#[message.inboundProperties['originalFilename']]" doc:name="Session Variable"/>
<flow-ref name="collectionsplitterSub_Flow" doc:name="collectionsplitterSub_Flow"/>
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
这篇关于Mule ESB 3.6-将BufferedInputStream转换为XML的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!