简体   繁体   中英

XMLUnit Compare Xml Ignoring Sequence

I need to compare 2 XML's for which I am using my custom Difference Listener which is ignoring Child Node Sequence and Attribute List Sequence:

if (difference.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID ||
    difference.getId() == DifferenceConstants.ATTR_SEQUENCE_ID) 
return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; 

My code works well for :

    <xml>
        <elem1 att1="abc" att2="def"></elem1>
        <elem1></elem1>
    </xml>

and

    <xml>
        <elem1></elem1>
        <elem1 att2="def" att1="abc"></elem1>
    </xml>

However I need it to consider these XML's as identical as well:

<xml>
    <elem1>
      <elem2>
         <id>ABC</id>
         .....
      </elem2>
    </elem1>
    <elem1>
      <elem2>
         <id>DEF</id>
         .....
      </elem2>
    </elem1>
</xml>

and

<xml>
    <elem1>
      <elem2>
         <id>DEF</id>
         .....
      </elem2>
    </elem1>
    <elem1>
      <elem2>
         <id>ABC</id>
         .....
      </elem2>
    </elem1>
</xml>

Here I want comparison to consider both elem1's in both XML are identical as only sequence is mixed up. So I need my code to work for more than one nesting level. Is that possible with XMLUnit? Please help anyone ?

I found a temporary Solution (Cant add Comment on my own ques for 8 hrs):

I was earlier overriding the ElementQualifier with: ElementNameAndAttributeQualifier, If I change that to RecursiveElementNameAndTextQualifier, I can get my desired result

xmlDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());

You can also use multiLevelElementNameandTextQualifier for deepr level XML compare. Recursive is for level 1 only.

For further Details your can refer: http://xmlunit.sourceforge.net/userguide/html/ar01s03.html#ElementQualifier Example 20

I need to match Element Name and Attributes and this matches Name and Text .. but still .. for now its working so I am using it ..

Will update if I find better solution

Hope this is helpful for someone :)

ElementQualifier is what tells XMLUnit which elements to compare. If the built-in ElementQualifier s don't work for you, you can provide an implementation of the interface yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM