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 !!!
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>