cancel
Showing results for 
Search instead for 
Did you mean: 

Compare two xml files and return the difference tags

0 Kudos
1,057

Hi 7a519509aed84a2c9e6f627841825b5a , engswee.yeoh

I am trying to find out the difference between two xml files and return the difference tags.

To Achieve this I am storing the xml files in Datastore and to find out the difference I am using the below Groovy script.

import com.sap.gateway.ip.core.customdev.util.Message
//Imports for DataStoreService-class
import com.sap.it.api.asdk.datastore.*
import com.sap.it.api.asdk.runtime.*
/*import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;*/
import org.jdom2.*;
import org.xmlunit.diff.*;
Message processData(Message message)
{
def service = new Factory(DataStoreService.class).getService()

//Check if valid service instance was retrieved
if( service != null) {
//Read data store entry via id
def dsEntry = service.get("MetadataStorage","Metadata")
def result = new String(dsEntry.getDataAsArray())
//message.setBody(body)
def dsEntry1 = service.get("MetadataCurrent","CurrentData")
def result1 = new String(dsEntry1.getDataAsArray())
//message.setBody(body)

DifferenceEngine diff = new DOMDifferenceEngine();
diff.addDifferenceListener(new ComparisonListener() {
        public void comparisonPerformed(Comparison comparison, ComparisonResult outcome) {
            Assert.fail("found a difference: " + comparison);
        }
    });
diff.compare(result, result1);
}
   // return message
}

Below is the Error I am Getting. Kindly let me know if we are missing anything/ suggest me how to get the difference between two xml files.

Note: I have imported JDOM and XMLUNIT Jar files.Error Details
javax.script.ScriptException: java.lang.Exception: java.lang.NullPointerException: Cannot invoke method getDataAsArray() on null object@ line 22 in compareXML.groovy, cause: java.lang.NullPointerException: Cannot invoke method getDataAsArray() on null object 

Accepted Solutions (0)

Answers (0)