Saturday 29 August 2020

Groovy script to find the xml field and store in property

 Groovy script to find the xml field from the input payload and display the value in payload logging title in the monitoring.

Eg: 

<root>

<id>112</id>

<request_id>234</request_id>

<task_id>456</task_id>

</root>


Script:

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

import java.util.HashMap;

def Message processData(Message message) {

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


def data = new XmlSlurper().parseText(body);


def requestid = data.'**'.findAll { node -> node.name() == 'request_id' }*.text();

def taskid = data.'**'.findAll { node -> node.name() == 'task_id' }*.text();


requestid= requestid.toString().replaceAll("\\[", "");

requestid= requestid.toString().replaceAll('\\]', "");


message.setProperty("requestid",requestid);


taskid= taskid.toString().replaceAll("\\[", "");

taskid= taskid.toString().replaceAll('\\]', "");


message.setProperty("taskid",taskid);

return message;

}

Output

generally it displays as requestid as [234] and taskid as [456]

but need to remove [ ] for our requirement, so handled that aswell in script.



1 comment:

  1. How to get and store value ofrow 2 as property?

    .
    ..
    ...value
    ..
    ..
    ...value
    ..
    .

    ReplyDelete