on 07-30-2020 2:09 PM
Hi All,
I was wondering on how to add a header to payload as an extra node using groovy.
Have tried append node. Since we can get headers in hashmap.(but it didnt work)
Want to know how to append a header or property as a part of main XML.
Please suggest
Hello Mounika,
Below should work.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil.*;
def Message processData(Message message) {
def body = message.getBody(java.lang.String);
def map = message.getHeaders();
def value = map.get("Threshold");
def Threshold = "<Threshold>"+value+"</Threshold>"
def response= new XmlSlurper().parseText(body)
def newNode = new XmlSlurper().parseText( Threshold )
response.MessageProcessingLog.appendNode( newNode );
def outxml = groovy.xml.XmlUtil.serialize( response )
message.setBody(outxml)
return message;
}
Regards,
Sriprasad Shivaram Bhat
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mounika,
You need to initialize hash map object and get the value . Please try with to initialize and then check
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Mounika,
Adding below to your question might help you to get some answers.
1) Input XML or Data.
2) What you have tried so far ( may be code snippet you have written or any method you have tried ).
3) Output Expected .
Regards,
Sriprasad Shivaram Bhat
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sriprasad,
Thank youThe Header is
Threshold =5
Groovy used:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import org.custommonkey.xmlunit.*;
import org.w3c.dom.NodeList;
import javax.xml.xpath.*;
import javax.xml.transform.TransformerFactory;
import org.w3c.dom.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.util.LinkedHashMap;
def Message processData(Message message) {
def body = message.getBody();
def response= new XmlSlurper().parseText(body)
x= response.MessageProcessingLogs
def map = message.getHeaders();
def value = map.get("Threshold");
def res = x.appendNode(value);
message.setBody(res)
return message;
}
Output Expected:
<MessageProcessingLogs> <MessageProcessingLog> <Status>FAILED</Status> <LogStart>2020-07-28T04:07:56.806</LogStart> <MessageGuid>XXXXXX</MessageGuid> <LogEnd>2020-07-28T04:07:56.850</LogEnd> <IntegrationFlowName>XXXXXXX</IntegrationFlowName> </MessageProcessingLog><Threshold>5</Threshold> </MessageProcessingLogs>
That was the requirement to add the header "Threshold" to the nodes of XML
User | Count |
---|---|
87 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.