用m子中的其他值替换xml特定节点元素的值 [英] replace xml particular node element value with other value in mule

查看:83
本文介绍了用m子中的其他值替换xml特定节点元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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]" />

我想在运行时将offerprice值替换为其他值.任何帮助表示赞赏.我尝试了各种方法.任何人都明白,这意味着它可以为我省很多钱

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

推荐答案

您可以使用XSLT并使用标识模板来替换一个元素.或者,如果您真的想使用MEL进行转换,请转换为DOM并使用Dom4j API来设置值,然后根据需要转换回XML:

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]" />

编辑

这里是一个示例,如果您想更新多个节点.如果转换变得更加复杂,我真的建议您看一下XSLT.

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]" />

这篇关于用m子中的其他值替换xml特定节点元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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