简体   繁体   中英

Replace node text with Mule & groovy

I am trying to replace the text value of a node using a groovy scripting transformer in Mule.

I first use a XML to DOM transformer then I am using the following

<scripting:transformer name="replacePassword">
        <scripting:script engine="Groovy"><![CDATA[import org.dom4j.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory

Node passwordElement = message.payload.getRootElement().selectSingleNode("//*[local-name()=\'Password\']")
passwordElement.setTextContent("xxxxxxxxxxxxx")
return message.payload
]]></scripting:script>
    </scripting:transformer>

But this seems to be throwing the following exception

Root Exception stack trace: groovy.lang.MissingMethodException: No signature of method: org.dom4j.tree.DefaultElement.setTextContent() is applicable for argument types: (java.lan g.String) values: [xxxxxxxxxxxxx]

I am not too familiar with groovy is my script correct?

Thanks

just look at the documentation for org.dom4j.tree.DefaultElement

there is no setTextContent() method... but there is a setText() method

Went with this in the end, this will get a node and replace the text value

 <scripting:transformer name="replacePassword">
            <scripting:script engine="Groovy"><![CDATA[
            node = message.payload.getRootElement().selectSingleNode('//*[local-name()=\'Password\'][1]');
            node.text = 'xxxxxxx';
            return message.payload;]]></scripting:script>
        </scripting:transformer>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM