on ‎2023 Sep 18 5:40 PM
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
Request clarification before answering.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 8 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.