Forum Discussion

mchelikani's avatar
mchelikani
Contributor
12 years ago

Compare 2 XML Skeletons Issue

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

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>

1 Reply

  • I'm sorry, but our support offering doesn't cover third party libraries like XmlUnit, and personally I don't know this part of XmlUnit well enough. Maybe you can ask a question on stackoverflow.com?

    Kind regards,
    Manne Fagerlind
    SmartBear Sweden