我想在mule的xml有效负载中添加一个节点。有人能告诉我怎么做吗?输入xml——
<Location>
<cde> Hello </cde>
</Location>
我想在
后面附加一个节点结果xml应该是这样的-
<Location>
<id> 1234 </id>
<cde> Hello </cde>
</Location>
I tried
<expression-component><![CDATA[
myNode = message.payload.rootElement.addElement(’ID’);
myNode.text = '1234';
message.payload.rootElement.elements().add(1, myNode.detach());
]]></expression-component>
也<enricher source="#[sessionVars.providerid]" doc:name="Message Enricher"
target="#[xpath3(’/Locations’,payload,’NODE’).appendChild(payload.importNode($.getFirstChild(),true) )]">
<http:request config-ref="HTTP_Request_Configuration" path="/system" method="POST" doc:name="HTTP"/>
</enricher>
什么都不工作,请帮忙!!
您可以使用XSLT以下列方式添加节点或修改XML:-
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8082" basePath="rc" doc:name="HTTP Listener Configuration"/>
<flow name="test">
<http:listener config-ref="HTTP_Listener_Configuration" path="/myservice" doc:name="HTTP"/>
<set-variable variableName="Cde" value="#[xpath3('//Location/cde')]" doc:name="Variable"/>
<logger level="INFO" message="Cde :-#[flowVars.Cde]" doc:name="Logger"/>
<set-variable variableName="Id" value="1324" doc:name="Variable"/>
<mulexml:xslt-transformer name="PrepareSOAPRequest" xsl-file="Transform.xsl" outputEncoding="UTF-8" encoding="UTF-8" maxIdleTransformers="2" maxActiveTransformers="5" returnClass="java.lang.String" doc:name="XSLT">
<mulexml:context-property key="Cde" value="#[flowVars.Cde]"/> <!-- Passing the variables in XSLT to produce the XML dynamically -->
<mulexml:context-property key="Id" value="#[flowVars.Id]"/> <!-- Passing the variables in XSLT to produce the XML dynamically -->
</mulexml:xslt-transformer>
<logger level="INFO" message="Final XML :-#[message.payload]" doc:name="Logger"/>
</flow>
您的XSLT将如下所示,需要放在src/main/resource
文件夹下:-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<!-- Getting values from Mule variables -->
<xsl:param name="Cde"/>
<xsl:param name="Id"/>
<xsl:template match="/">
<Location>
<id><xsl:value-of select="$Id"/></id>
<cde><xsl:value-of select="$Cde"/></cde>
</Location>
</xsl:template>
</xsl:stylesheet>
如您所见,您需要首先使用XPATH3从输入XML中提取cde的值并将其存储到一个变量中。您还可以将id值存储到一个变量中。
最后,您可以使用XSLT修改XML,并像上面所示的那样将所有变量的值传递给它。
更新后的答案
<enricher source="#[message.payload]" target="#[flowVars.test]">
<processor-chain>
<set-payload value="<Location><cde> Hello </cde></Location>" doc:name="Set Payload" />
<set-variable variableName="Cde" value="#[xpath3('//Location/cde')]" doc:name="Variable" />
<logger level="INFO" message="Cde :-#[flowVars.Cde]" doc:name="Logger" />
<set-variable variableName="Id" value="1324" doc:name="Variable" />
<mulexml:xslt-transformer name="PrepareSOAPRequest" xsl-file="Transform.xsl" outputEncoding="UTF-8" encoding="UTF-8" maxIdleTransformers="2" maxActiveTransformers="5" returnClass="java.lang.String"
doc:name="XSLT">
<mulexml:context-property key="Cde" value="#[flowVars.Cde]" /> <!-- Passing the variables in XSLT to produce the XML dynamically -->
<mulexml:context-property key="Id" value="#[flowVars.Id]" /> <!-- Passing the variables in XSLT to produce the XML dynamically -->
</mulexml:xslt-transformer>
<logger level="INFO" message="Final XML :-#[message.payload]" doc:name="Logger" />
<http:request config-ref="HTTP_Request_Configuration" path="/system" method="POST" doc:name="HTTP"/>
</processor-chain>
</enricher>
对于您的表达式组件解决方案,请尝试使用xml-to-dom-transformer
,并在组件后使用相反的变压器。
另一个干净的解决方案是使用XSLT进行恒等转换(添加id)。
最后,如果你使用的是Mule EE <3.7,这对于DataMapper来说是一个非常简单的任务,如果在3.7上,对于DaveWeave来说甚至更好。
我可以使用groovy
<enricher target="#[flowVars['compliant']]" doc:name="Message Enricher">
<flow-ref name="lookupComplaintData" doc:name="lookupComplaintData"/>
</enricher>
<set-payload value="#[flowVars.compliant]" doc:name="Set Payload"/>
<sub-flow doc:description="add Compliant information "
name="lookupComplaintData">
<scripting:transformer doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[import org.dom4j.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
Element compliantElement = payload.getRootElement().addElement("compliant")
compliantElement.addElement("compliantType").addText("Delivery")
compliantElement.addElement("compliantReference").addText("ABC123ABC")
compliantElement.addElement("complaintText").addText("Order was delivered on time")
return payload
]]></scripting:script>
</scripting:transformer>