+ Start a Discussion
SalesRedSalesRed 

How To Replace A Text In a DOM.XMLNode

Hi,

 

I have a requirement to check if a XML paramter Node contians certain text and if so to replace it.  

 

With the following code for example.  I would need to replace the content I get via 

arrayOfActionTypeNode.getText()

 

for(String parameterName : this.paramterMap.keySet()) {
       arrayOfFieldTypeNode = this.paramterMap.get(parameterName).toXML(arrayOfFieldTypeNode);
        if (parameterName.contains('AccountID')) {
            System.debug('AccountIDVal=' + this.paramterMap.get(parameterName));
            System.debug('arrayOfFieldTypeNode=' + arrayOfFieldTypeNode);
            System.debug('nameVal=' + arrayOfFieldTypeNode.getName());
            System.debug('textVal=' + arrayOfFieldTypeNode.getText());
     }
}

 

 

 

I tried it with 

 

arrayOfActionTypeNode.getText().put('test') but a similar function is not available. 

 

If I want to update the text in the parameter do I need to delete the attribute or child first via removeAttribute/removeChild and create a new one, or is there a simpler way of doing this?

 

Thanks in advance for your help.

rk_1978rk_1978

^bump^

 

Any help on this?

Murali RaghavanMurali Raghavan
You can loop through all the child nodes including the text node via node.getChildren(). Check the type of the child if its a TEXT node to remove and add the text node back.
if(node.getNodeType()+''=='TEXT' ){
   dom.XmlNode par = node.getParent();
    par.removeChild(node);
    par.addTextNode('Your Replacement text');
}