cancel
Showing results for 
Search instead for 
Did you mean: 

Comparing XML Payloads in SAP CPI

0 Kudos

How to compare the two incoming xml payload's from databases and finding the difference between the payloads in sap cpi?

View Entire Topic
vbalko-claimate
Active Participant
0 Kudos

This is just for inspiration. I didnt tested this code, but it give you the idea how to do it.

import com.sap.gateway.ip.core.customdev.util.Message
import groovy.util.XmlSlurper
import groovy.util.slurpersupport.GPathResult

def Message processData(Message message) {
    // Retrieve XML strings from properties
    def xmlString1 = message.getProperty("property1")
    def xmlString2 = message.getProperty("property2")

    // Parse the XML strings
    def xml1 = new XmlSlurper().parseText(xmlString1)
    def xml2 = new XmlSlurper().parseText(xmlString2)

    // Compare the XMLs
    def differences = compareXml(xml1, xml2)

    // Add differences to the message or return them as needed
    message.setProperty("differences", differences)

    return message
}

def compareXml(GPathResult xml1, GPathResult xml2) {
    def differences = []

    // Compare children of the root element
    compareNodes(xml1, xml2, differences)

    return differences ?: "No differences found"
}

def compareNodes(GPathResult node1, GPathResult node2, List differences, String path = '') {
    // Check if nodes are equal
    if (node1.name() != node2.name() || node1.text() != node2.text()) {
        differences << "Difference found at path '${path}': '${node1.name()}' vs '${node2.name()}'"
    }

    // Compare all children
    def children1 = node1.children()
    def children2 = node2.children()

    int max = Math.max(children1.size(), children2.size())
    for (int i = 0; i < max; i++) {
        def child1 = children1[i]
        def child2 = children2[i]
        if (child1 && child2) {
            compareNodes(child1, child2, differences, path + '/' + child1.name())
        } else {
            differences << "Mismatch in children at path '${path}': Missing element"
        }
    }
}
0 Kudos

Thank you Balko