Hi,
I tried to extract the repeated occurrence of an XML child node from the input, so I blogged and couldn't found the one which suits my requirement. Hence I am writing this blog to make the use/modify of the script based on the requirement of yours.
My requirement:
Below is the input structure, where I need to extract and concatenate the message node from each dynamic parent node.
Input XML structure
<fields>
<body.secondary_contact>
<message>secondary_contact is required</message>
<value>
</value>
</body.secondary_contact>
<body.secondary_contact_number>
<message>secondary_contact_number is required</message>
<value>
</value>
</body.secondary_contact_number>
<body.workstream_prefix>
<message>workstream_prefix must be a number string</message>
<value>
</value>
</body.workstream_prefix>
</fields>
Ouput - secondary_contact is required,secondary_contact_number is required,workstream_prefix must be a number string
Groovy script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.util.*;
def Message processData(Message message)
{
def body = message.getBody(java.lang.String) as String;
def root = new XmlParser().parseText(body);
def i=0;
def errors="";
def allmessage=[];
root.'**'.findAll { it.name() == 'message'}.each { a ->allmessage << a.text()};
int len = allmessage.size();
while(i<allmessage.size())
{
errors=errors+","+allmessage[i];
i++;
}
errors=errors.substring(1);
def messageLog = messageLogFactory.getMessageLog(message);
if(messageLog != null){
messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment")
messageLog.addAttachmentAsString("only_message:", len + "message - "+errors, "text/plain");
}
message.setBody(errors);
return message;
}
Using the above script, I fulfilled my requirements. Hope I will get some suggestions to achieve this in another way.
Appreciating the responses.
Thank you,
Naveen