Forum Discussion

mchelikani's avatar
mchelikani
Contributor
11 years ago

GroovyScript - Compare Two XML's Ignoring TextAndAttribute

I am facing an issue comparing two xml's ignoring text and attribute values. (My requirement is just to compare the Skeleton)

Here is the Code I have. Please Help.


Code:

import groovy.io.FileType
import org.custommonkey.xmlunit.*

def diff = new Diff("<location><street-address>22 any street</street-address><postcode>XY00 99Z</postcode></location>",
"<location><street-address>20 east cheap</street-address><postcode>EC3M 1EB</postcode><country></country></location>")

DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener()
diff.overrideDifferenceListener(myDifferenceListener)

if(!diff.identical()) {
log.info'Files are Different'

def detailedDef = new DetailedDiff(diff)
def defferences = detailedDef.getAllDifferences()
//Build Text Report
buildDiffs(defferences)
} else {
log.info 'Files are Identical'
}


def buildDiffs( differences){
differences.each { nodeDiff ->
log.info nodeDiff.toString() + '<br>'
}
}


Actual Output:



Mon Nov 11 18:43:30 EST 2013:INFO:Expected number of child nodes '2' but was '3' - comparing <location...> at /location[1] to <location...> at /location[1]<br>
Mon Nov 11 18:43:30 EST 2013:INFO:Expected text value '22 any street' but was '20 east cheap' - comparing <street-address ...>22 any street</street-address> at /location[1]/street-address[1]/text()[1] to <street-address ...>20 east cheap</street-address> at /location[1]/street-address[1]/text()[1]<br>
Mon Nov 11 18:43:30 EST 2013:INFO:Expected text value 'XY00 99Z' but was 'EC3M 1EB' - comparing <postcode ...>XY00 99Z</postcode> at /location[1]/postcode[1]/text()[1] to <postcode ...>EC3M 1EB</postcode> at /location[1]/postcode[1]/text()[1]<br>
Mon Nov 11 18:43:30 EST 2013:INFO:Expected presence of child node 'null' but was 'country' - comparing at null to <country...> at /location[1]/country[1]<br>


Expected Output:


Mon Nov 11 18:43:30 EST 2013:INFO:Expected number of child nodes '2' but was '3' - comparing <location...> at /location[1] to <location...> at /location[1]<br>
Mon Nov 11 18:43:30 EST 2013:INFO:Expected presence of child node 'null' but was 'country' - comparing at null to <country...> at /location[1]/country[1]<br>

3 Replies

  • gauravkhurana's avatar
    gauravkhurana
    Occasional Contributor

    Thanks a ton..

     

    Thats the best way available on internet to compare 2 XMLs.

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      Hi all,

       

      Great script, mchelikani.


      Community, can anyone help Madhu and modify this script so that it compares two xml's ignoring text and attribute values?

       

  • gauravkhurana's avatar
    gauravkhurana
    Occasional Contributor

    Best code i was able to find on internet to compare 2 XMLs.

     

    Best thing is it will tell you where the difference is there in the 2 XMLs