cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Issue in Groovy Script for Replace statement

meerhyderali
Explorer
1,180

Hi All,

I tried to find the below syntax for ReplaceAll in Groovy Script, but couldn't find the proper one to resolve my issue. Hence posted in SDN for support.

I am trying to replace the MT_FILE_DATA with other statement (MT_FILE_DATA xmlns:urn = "urn:xyz.com:SAP_ECC:CrossApplication:1.0"> <xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/), but getting the error near ReplaceAll statement. Can you all please help in this step.

Please find the below Script which I have written.

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import groovy.xml.*;

import groovy.util.XmlSlurper;

def Message processData(Message message) {

def body = message.getBody(java.lang.String);

def messageLog = messageLogFactory.getMessageLog(message);

if (messageLog != null) {

messageLog.addAttachmentAsString("03-INPUT-text", body, "text/plain");

}

def map = message.getHeaders();

def filename = map.get("Filename");

def writer = new StringWriter();

def builder = new MarkupBuilder(writer);

builder."urn:MT_FILE_DATA" {

"FILE_TYPE"('TEST_PAYLOAD')

"FILE_NAME"(filename)

"FILE_CONTENT"(body)

}

def replaceFrom = '<urn:MT_FILE_DATA>';

def replaceTo = ' <urn:MT_FILE_DATA xmlns:urn = "urn:xyz.com:SAP_ECC:CrossApplication:1.0"> <xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"> '

def xml1 = writer.builder(replaceFrom, replaceTo);

return xml1;

}

Regards,Ali

Accepted Solutions (0)

Answers (1)

Answers (1)

Bhargavakrishna
Active Contributor
0 Likes

Hi Ali,

The below code should work.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
    // Get the message body as a string
    def body = message.getBody(String) as String;
    
    // Replace the second pattern
    body = body.replaceAll("<urn:MT_FILE_DATA>", "http://schemas.xmlsoap.org/soap/envelope/");

    // Set the modified body back into the message
    message.setBody(body);
    
    return message;
}

Regards

Bhargava Krishna Talasila