如何在m子的xml负载中添加节点 [英] how to add a node in a xml payload in mule

查看:83
本文介绍了如何在m子的xml负载中添加节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在m子的xml有效负载中添加一个节点.有人可以告诉我怎么做. 输入xml-

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>

我想在

结果xml像这样-

<Location>  
<id> 1234 </id>     
<cde> Hello </cde> 
</Location>

我尝试了

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

什么都没用.请帮助!!!

nothing is working..Please help !!!

推荐答案

您可以使用 XSLT 通过以下方式添加节点或修改XML:-

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>  

您的 XSLT 如下,需要放在src/main/resource文件夹下:-

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>  

如您所见,您首先需要使用 XPATH3 从输入XML中提取值 cde ,并将其存储到变量中.您还可以将您的 id 值存储到变量..

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

最后,您可以使用给定的 XSLT 修改XML,并将所有变量值传递给XML,如上所示.

Finally you can modify the XML using the XSLT as given, and passing all the variables values into it as shown above.

更新后的答案

<enricher source="#[message.payload]" target="#[flowVars.test]">
    <processor-chain>
    <set-payload value="&lt;Location&gt;&lt;cde&gt; Hello &lt;/cde&gt;&lt;/Location&gt;" 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>

这篇关于如何在m子的xml负载中添加节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆