可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a Mule ESB project using HTTP Connector where I want to use XPath to split/route the XML.
The message payload after the HTTP Connector is a org.glassfish.grizzly.utils.BufferInputStream.
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?
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?
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"/> <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 to split
<?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 David
回答1:
Thanks to the answer for this stackoverflow question helped provide the solution to my question.
Answer:
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')]
I also had to add all the splitting components into a sub-flow, especially for my HTTP source (as I kept getting payloadAsString() errors.
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>
回答2:
Your flow looks good. I tried it on my machine and I see where you're coming from. Try changing the Mule expression from #[xpath3('//Order')] to #[xpath('//Order')]. Doing so caused Mule to successfully generate two Mule messages - one for each Order element.
HTH,
Justin