问题
I want to add a node in my xml payload in mule. Can someone show me how to do it. Input xml --
<Location>
<cde> Hello </cde>
</Location>
I want to append a node after
The result xml shud be like this —
<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>
also
<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>
nothing is working..Please help !!!
回答1:
You can use XSLT to add the node or modify your XML in following way :-
<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>
And your XSLT will be as follows and need to be put under src/main/resource
folder :-
<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>
As you can see you need to first extract the value of cde from your input XML using XPATH3 and storing it into a variable. You can also store your id value into a variable ..
Finally you can modify the XML using the XSLT as given, and passing all the variables values into it as shown above.
UPDATED ANSWER
<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>
回答2:
For your expression component solution try using a xml-to-dom-transformer
and after the component the opposite transaformer.
Another clean solution would be a identity transformation (adding id) using XSLT.
Finally if you are using Mule EE <3.7 this is a very easy task for DataMapper, or even better for DaveWeave if on 3.7.
回答3:
I Was able to do it by using 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>
来源:https://stackoverflow.com/questions/31460514/how-to-add-a-node-in-a-xml-payload-in-mule