3

Comparing these two snippets of XML:

testXml:
<ELEMENT1>
    <CHILD1></CHILD1>
</ELEMENT1>

actualXml:
<ELEMENT1>
    <CHILD1>notEmpty</CHILD1>
</ELEMENT1>

using:

Diff diff = new Diff(testXml, actualXml);
Detailed detailedDiff = new DetailedDiff(diff);

Now detailedDiff.getAllDifferences(); will return a DifferenceConstants.HAS_CHILD_NODES_ID difference and if you print the difference to the console it looks like this:

Expected presence of child nodes to be 'false' but was 'true' - comparing <CHILD1...> at /ELEMENT1[1]/CHILD1[1] to <CHILD1...> at /ELEMENT1[1]/CHILD1[1]

My question is, why is the difference of type DifferenceConstants.HAS_CHILD_NODES_ID and not DifferenceConstants.TEXT_VALUE_ID? The structure of the two XML-snippets are the same, but the text value of the two differs.

So, why doesn't that trigger a difference?

Olle Söderström
  • 690
  • 1
  • 7
  • 15

1 Answers1

2

Try to use this ElementQualifier:

Diff diff = new Diff(testXml, actualXml);
diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier() );
Detailed detailedDiff = new DetailedDiff(diff);

here is the description from javadoc:

public RecursiveElementNameAndTextQualifier()

Uses element names and the text nested an arbitrary level of child elements deeper into the element to compare elements. Checks all nodes, not just first child element. Does not ignore empty text nodes.

The interested thing here is the "does not ignore empty text nodes".

It seems that the default ElementQualifier treats empty nodes as a missing node, and only checks for the first error related to one node. So in your case, possibly solely the "HAS_CHILD_NODES_ID" is thrown instead of including also "TEXT_VALUE_ID".

At least, RecursiveElementNameAndTextQualifier goes deeper.

Mik378
  • 21,881
  • 15
  • 82
  • 180
  • Yes I've tried that one, but it didn't work the way I wanted just as you stated. I think I will try to implemented a DifferenceListener that ignores this case...somehow. However, it feels like there should be a cleaner way to solve this issue? – Olle Söderström Oct 12 '12 at 15:02