Script to Compare XML in SAP - Part 1
Script to Compare XML in SAP - Part 1
Introduction:
This blog is dedicated to exploring the intricacies of data comparison in SAP CPI
implementation scenarios. One of the key challenges in data/message flow optimisation
is effectively comparing input payloads. In this blog series, we will focus on comparing
two XML payloads, regardless of their structural similarities or differences, with a keen
eye on detecting and highlighting data changes.
Scenario :
Solution :
IFlow Design:
The salary data of the employee with Employee Id 1003 has been updated from
<SALARY>10000.00</SALARY> to <SALARY>14000.00</SALARY>.
Groovy Script : The Groovy script below compares the data of both payload1
and payload2 based on the Employee Id, which is the unique data identifier. It
then returns only the dataset that has been updated or modified compared to the
previous data.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.util.XmlSlurper;
import groovy.xml.MarkupBuilder;
def Message processData(Message message) {
def changedData = []
if (matchingRow) {
def diff = false
if (diff) {
changedData << matchingRow
}
}
}
def resultXml = new StringWriter()
def xmlBuilder = new MarkupBuilder(resultXml)
xmlBuilder.root {
changedData.each { changedRow ->
EMPLOYEE {
changedRow.children().each { element ->
"${element.name()}"(changedRow."$
{element.name()}".text())
}
}
}
}
message.setBody(resultXml.toString())
return message;
}
Result :
In conclusion, the output of using Groovy script in the flow for comparing data contains
the data of the employee whose dataset has been updated. This approach not only
streamlines data comparison processes but also ensures that only relevant and updated
information is highlighted, contributing to efficient data/message flow management.