Skip to Content
0

Change data of a child node in message payload using a content modifier property using groovy script

May 16 at 01:07 PM

85

avatar image

Hi Experts,

Can you please help me out in these scenario of the subject mentioned as am new to HCI.

I have created like these below:

In the First Content Modifier i have used like these in the body:

<?xml version="1.0" encoding="UTF-8"?>
<queryUserResponse>
<User>
<id>USR-93</id>
<city>New Hyde Park</city>
<cellPhone/>
<businessPhone>(1) 215 555-0922</businessPhone>
<benchStrength/>
<addressLine1>11 Lightner Ave</addressLine1>
<Item>
raj@xyz.com
</Item>
</User>
</queryUserResponse>


And in the second Modifier

And in the Groovy Script i have written these code:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.util.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

def Message processData(Message message)
{
//Body
def body = message.getBody();
//Headers
def map = message.getHeaders();

//get the constant value associated with header 'id'
def value = map.get("id");

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(body);

Element compoundEmployeeList = doc.getRootElement();
List<Element> User = compoundEmployeeList.getChildren("User");

for(Element cEmployee : User)
{
// Extract the external id for the current employee

Element person = cEmployee.getChild("Item");
person.setChildText(value);

}

//Write your logic here
message.setProperty("payload", new XMLOutputter().outputString(doc));

return message;
}



Kindly help me to get through these scenario.

Thanks

az4xi.png (17.0 kB)
hbayx.png (14.8 kB)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Morten Wittrock
May 16 at 02:20 PM
0

Hi Raju

The following Groovy script does three things:

  1. It parses the input XML using the groovy.util.XmlSlurper class
  2. It sets the text of the queryUserResponse/User/Item element to the contents of the id property
  3. It serializes the updated document using groovy.xml.XmlUtil and stores it in the message body

Here's the script:

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.util.XmlSlurper
import groovy.xml.XmlUtil

def Message processData(Message message) {
    def itemNewVal = message.getProperty('id')
    def queryUserResponse = new XmlSlurper().parseText(message.getBody(java.lang.String))
    queryUserResponse.User.Item.replaceBody(itemNewVal)
    message.setBody(XmlUtil.serialize(queryUserResponse))
    return message
}

Regards,

Morten

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Dear Morten,

Thanks a lot , it is working now.

You are super...!!

Thanks,

Raju.

1

No problem, glad to be of help.

Regards,

Morten

0