0% found this document useful (0 votes)
4 views

Script to Compare XML in SAP - Part 1

This blog discusses the use of Groovy Script to compare XML payloads in SAP CPI, focusing on detecting data changes between two employee datasets. It outlines the process of setting up content modifiers to store the payloads and provides a Groovy script that identifies and returns only the updated data segments. The solution aims to enhance data/message flow optimization by highlighting relevant changes efficiently.

Uploaded by

jaydata2018
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Script to Compare XML in SAP - Part 1

This blog discusses the use of Groovy Script to compare XML payloads in SAP CPI, focusing on detecting data changes between two employee datasets. It outlines the process of setting up content modifiers to store the payloads and provides a Groovy script that identifies and returns only the updated data segments. The solution aims to enhance data/message flow optimization by highlighting relevant changes efficiently.

Uploaded by

jaydata2018
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Groovy Script to Compare XML Payloads in SAP CPI - 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 :

In SAP CPI implementation scenarios, comparing input payloads is a crucial factor in


data/message flow optimisation. In this blog, we delve into the comparison of two XML
payloads. Whether the payloads possess different structures or share a similar
structure, the focus remains on detecting data changes between the old and updated
versions. Our goal is to highlight the updated data segments, ensuring a streamlined
output.

Solution :

Leveraging Groovy Script in the Flow for Data Comparison.

IFlow Design:

 Content Modifier : We will provide a dataset of employees in the content


modifier named as "Payload input1". Below is the sample data provided.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<EMPLOYEE>
<EMPLOYEE_ID>1001</EMPLOYEE_ID>
<FIRST_NAME>Buggs</FIRST_NAME>
<LAST_NAME>Bunny</LAST_NAME>
<EMAIL>[email protected]</EMAIL>
<PHONE_NUMBER>212-867-5309</PHONE_NUMBER>
<HIRE_DATE>12 DEC 1985</HIRE_DATE>
<JOB_ID>MD12741</JOB_ID>
<SALARY>4000.00</SALARY>
<COMMISSION_PCT>0.03</COMMISSION_PCT>
<DESIGNATION>Developer</DESIGNATION>
<DEPARTMENT_ID>5341</DEPARTMENT_ID>
</EMPLOYEE>
<EMPLOYEE>
<EMPLOYEE_ID>1002</EMPLOYEE_ID>
<FIRST_NAME>Robert</FIRST_NAME>
<LAST_NAME>Jay</LAST_NAME>
<EMAIL>[email protected]</EMAIL>
<PHONE_NUMBER>212-867-2345</PHONE_NUMBER>
<HIRE_DATE>02 JAN 1983</HIRE_DATE>
<JOB_ID>MD12742</JOB_ID>
<SALARY>7000.00</SALARY>
<COMMISSION_PCT>0.13</COMMISSION_PCT>
<DESIGNATION>Consultant</DESIGNATION>
<DEPARTMENT_ID>5342</DEPARTMENT_ID>
</EMPLOYEE>
<EMPLOYEE>
<EMPLOYEE_ID>1003</EMPLOYEE_ID>
<FIRST_NAME>Tom</FIRST_NAME>
<LAST_NAME>Frank</LAST_NAME>
<EMAIL>[email protected]</EMAIL>
<PHONE_NUMBER>212-867-2342</PHONE_NUMBER>
<HIRE_DATE>01 OCT 1980</HIRE_DATE>
<JOB_ID>MD12743</JOB_ID>
<SALARY>10000.00</SALARY>
<COMMISSION_PCT>0.23</COMMISSION_PCT>
<DESIGNATION>Manager</DESIGNATION>
<DEPARTMENT_ID>5343</DEPARTMENT_ID>
</EMPLOYEE>
</root>

 Content Modifier : We will declare a property named "input1" to store payload1.


 Content Modifier : Another dataset of employees will be provided in the content
modifier named "Payload input2". However, in this example, the XML data
structure is different, and the data has been updated. Below is the sample data
provided.

The salary data of the employee with Employee Id 1003 has been updated from
<SALARY>10000.00</SALARY> to <SALARY>14000.00</SALARY>.

<?xml version="1.0" encoding="UTF-8"?>


<root>
<EMPLOYEE>
<EMPLOYEE_ID>1002</EMPLOYEE_ID>
<FIRST_NAME>Robert</FIRST_NAME>
<LAST_NAME>Jay</LAST_NAME>
<HIRE_DATE>02 JAN 1983</HIRE_DATE>
<JOB_ID>MD12742</JOB_ID>
<COMMISSION_PCT>0.13</COMMISSION_PCT>
<DESIGNATION>Consultant</DESIGNATION>
<DEPARTMENT_ID>5342</DEPARTMENT_ID>
<EMAIL>[email protected]</EMAIL>
<PHONE_NUMBER>212-867-2345</PHONE_NUMBER>
<SALARY>7000.00</SALARY>
</EMPLOYEE>
<EMPLOYEE>
<EMPLOYEE_ID>1001</EMPLOYEE_ID>
<FIRST_NAME>Buggs</FIRST_NAME>
<LAST_NAME>Bunny</LAST_NAME>
<HIRE_DATE>12 DEC 1985</HIRE_DATE>
<JOB_ID>MD12741</JOB_ID>
<COMMISSION_PCT>0.03</COMMISSION_PCT>
<DESIGNATION>Developer</DESIGNATION>
<DEPARTMENT_ID>5341</DEPARTMENT_ID>
<EMAIL>[email protected]</EMAIL>
<PHONE_NUMBER>212-867-5309</PHONE_NUMBER>
<SALARY>4000.00</SALARY>
</EMPLOYEE>
<EMPLOYEE>
<EMPLOYEE_ID>1003</EMPLOYEE_ID>
<FIRST_NAME>Tom</FIRST_NAME>
<LAST_NAME>Frank</LAST_NAME>
<HIRE_DATE>01 OCT 1980</HIRE_DATE>
<JOB_ID>MD12743</JOB_ID>
<COMMISSION_PCT>0.23</COMMISSION_PCT>
<DESIGNATION>General Manager</DESIGNATION>
<DEPARTMENT_ID>5343</DEPARTMENT_ID>
<EMAIL>[email protected]</EMAIL>
<PHONE_NUMBER>212-867-2342</PHONE_NUMBER>
<SALARY>14000.00</SALARY>
</EMPLOYEE>
</root>

 Content Modifier : We will declare a property named "input2" to store payload2.

 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 input1 = message.getProperty("input1")


def input2 = message.getProperty("input2")

def xml1 = new XmlSlurper().parseText(input1)


def xml2 = new XmlSlurper().parseText(input2)

def changedData = []

xml1.EMPLOYEE.each { row1 ->


def matchingRow = xml2.EMPLOYEE.find { row2 ->
row1.EMPLOYEE_ID.text() == row2.EMPLOYEE_ID.text()
}

if (matchingRow) {
def diff = false

row1.children().each { element ->


def correspondingElement = matchingRow."$
{element.name()}"
if (element.text() != correspondingElement.text()) {
diff = true
}
}

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.

You might also like