XMLUnit Compare Xml by comparing invalid nodes

I have a problem when you have a parent node in a different order. For example:

XML1 Example

<level1>
   <name>FirstParent</name>
   <level2>
        <somefield>AAA</somefield>
   </level2>
   <level2>
        <somefield>BBB</somefield>
   </level2>       
</level1>
<level1>
   <name>SecondParent</name>
   <level2>
        <somefield>CCC</somefield>
   </level2>
   <level2>
        <somefield>DDD</somefield>
   </level2>
</level1>

XML2 example

<level1>
   <name>SecondParent</name>
   <level2>
        <somefield>DDD</somefield>
   </level2>
   <level2>
        <somefield>CCC</somefield>
   </level2>
</level1>
<level1>
   <name>FirstParent</name>
   <level2>
        <somefield>BBB</somefield>
   </level2>
   <level2>
        <somefield>AAA</somefield>
   </level2>          
</level1>

When I debug the RecursiveElementNameAndTextQualifier, I see that it gets FirstParent as controlnode and SecondParent as testnode. And, therefore, it is right to conclude that the nodes are different. But I need to compare both FirstParent and SeconParent with their correct match in another file.

So, it seems that I need to sort the top-level nodes (?) First.

Does anyone know how I can overcome this? What should I check / modify / implement differently to pass the correct parent tandem to the ElementQualifier?

Perhaps with the right directions, I can develop what I need.

+4
4

, , . level1 . , , name, . .

, RecursiveElementNameAndTextQualifier. . somefield , , , .

, XMLUnit level1 , , , . compareUnmatched false, CHILD_NODE_NOT_FOUND.

XMLUnit 1.x(2.x ) ElementQualifier, , , ElementQualifier, .

+1

, XMLUnit 1 2, 2 (2.0.0-alpha-04) DiffBuilder . , Diff, ElementSelector Default Matcher:

import org.xmlunit.diff.Diff;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.diff.ElementSelectors;
import org.xmlunit.diff.DefaultNodeMatcher;

. , .

Diff diff = DiffBuilder.
            compare(myExpectedResultXmlObj).
            withTest(myCompareToXmlObj).
            withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)). 
            build();

Assert.assertFalse(diff.toString(), diff.hasDifferences());

withNodeMatcher.

+1

ElementQualifier. RecursiveElementNameAndTextQualifier .

0

For me, you also need to add a method checkForSimilar()to DiffBuilder.

Without it, the statement was erroneous, saying that the sequence of nodes was not the same (the position in the child list was not the same)

My code is:

Diff diff = DiffBuilder
    .compare(myExpectedResultXmlObj)
    .withTest(myCompareToXmlObj)
    .checkForSimilar()
    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
    .build();
0
source

Source: https://habr.com/ru/post/1610606/


All Articles