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?

3
  • can you provide the actual xml? Commented Oct 12, 2012 at 13:37
  • This is the actual xml for a unit test that Im currently writing. So this is the exact code that I have tested. Commented Oct 12, 2012 at 13:41
  • Which ElementQualifier type are you using? Commented Oct 12, 2012 at 13:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.