Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

replace xml particular node element value with other value in mule

<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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...