cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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>
<MailId>raj@xyz.com</MailId>
</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

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor

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

Former Member

Dear Morten,

Thanks a lot , it is working now.

You are super...!!

Thanks,

Raju.

MortenWittrock
Active Contributor
0 Kudos

No problem, glad to be of help.

Regards,

Morten

NidhiY
Explorer
0 Kudos

HI @Morten Wittrock

I want to perform some XML parsing using JDOM lib by calling a below classes, so I used to import as stated below in my groovy script:

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;

but while deploying getting below error:

Message processing failed.

Processing Time: 8 sec 584 ms

Error Details javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: EffectiveData__Script.gsh: 11: unable to resolve class org.jaxen.XPath @ line 11, column 1. import org.jaxen.XPath; ^ EffectiveData__Script.gsh: 9: unable to resolve class org.jdom.output.XMLOutputter @ line 9, column 1. import org.jdom.output.XMLOutputter ^ EffectiveData__Script.gsh: 8: unable to resolve class org.jdom.input.SAXBuilder @ line 8, column 1. import org.jdom.input.SAXBuilder; ^ EffectiveData__Script.gsh: 12: unable to resolve class org.jaxen.jdom.JDOMXPath @ line 12, column 1. import org.jaxen.jdom.JDOMXPath; ^ EffectiveData__Script.gsh: 6: unable to resolve class org.jdom.Element @ line 6, column 1. import org.jdom.Element ^ EffectiveData__Script.gsh: 5: unable to resolve class org.jdom.Document @ line 5, column 1. import org.jdom.Document; ^ EffectiveData__Script.gsh: 7: unable to resolve class org.jdom.JDOMExcepti.......I can download the lib from the internet but now how to import/deploy the same to HCI or CPI tenant? Please help.
MortenWittrock
Active Contributor
0 Kudos

Hi ynidhu

Please post this as a new question.

Regards,

Morten

former_member638251
Discoverer

Thanks for such very simple code ! Helps here.

Answers (1)

Answers (1)

NidhiY
Explorer
0 Kudos