replace xml particular node element value with other value in mule

空扰寡人 提交于 2019-12-29 08:15:14

问题


<healthcare>
    <plans>
        <plan1>
            <planid>100</planid>
            <planname>medical</planname>
            <desc>medical</desc>
            <offerprice>500</offerprice>
            <area>texas</area>
        </plan1>
        <plan2>
            <planid>101</planid>
            <planname>dental</planname>
            <desc>dental</desc>
            <offerprice>1000</offerprice>
            <area>texas</area>
        </plan2>
    </plans>
</healthcare>



<splitter evaluator="xpath" expression="/healthcare/plans" doc:name="Splitter"/> 
<transformer ref="domToXml" doc:name="Transformer Reference"/> 
<logger level="INFO" doc:name="Logger" message=" plans   detils...#[message.payload]" />

i want replace offerprice value with other values during runtime.anyhelp appreciated.I tried various various ways . anyone shed light means it saves me lot


回答1:


You could use XSLT and use identity templates to replace the one element.Or if your really want to do it with MEL, convert to DOM and use Dom4j APIs to set the value and then convert back to XML if needed:

    <expression-component><![CDATA[
          node = message.payload.getRootElement().selectSingleNode('//plans/plan1/planid');
          node.text = 'newvalue';
        ]]></expression-component>

    <mulexml:dom-to-xml-transformer />

    <logger level="ERROR" message=" #[payload]" />

EDIT

Here is an example if you want to update multiple nodes. If the transformation gets any more complex, I would really suggest taking a look at XSLT.

<mulexml:xml-to-dom-transformer             returnClass="org.dom4j.Document" />

        <expression-component><![CDATA[
                plans =  message.payload.getRootElement().selectNodes('//plans/*');
                foreach (plan : plans){
                    plan.selectSingleNode('offerprice').text = '3000';
                }       ]]></expression-component>

        <mulexml:dom-to-xml-transformer />

        <logger level="ERROR" message=" #[payload]" />


来源:https://stackoverflow.com/questions/30071432/replace-xml-particular-node-element-value-with-other-value-in-mule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!