4


I have the following testXML

<root>
   <a>test</a>
   <b>bee</b>
   <d/>
</root>

While the templateXML looks like this

<root>
  <a/>
  <b/>
  <c/>
  <d>
    <e/>
  </d>
</root>

I want XMLUnit to return the missing elements, in this case 'c' and 'e' are missing

/root[1]/c[1] is missing
/root[1]/d[1]/e[1] is missing

My code looks like this

public static ArrayList<Difference> testCompareToSkeletonXML(String xml, String template) throws Exception {

    XMLUnit.setCompareUnmatched(false);
    XMLUnit.setIgnoreAttributeOrder(true);
    XMLUnit.setIgnoreComments(true);
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);     

    DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
    DetailedDiff myDiff = new DetailedDiff(new Diff(template, xml));
   // myDiff.overrideDifferenceListener(myDifferenceListener);
    myDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
    ArrayList<Difference> returnList = new ArrayList<Difference>();
    List<Object> allDifferences = myDiff.getAllDifferences();
    for(Object obj: allDifferences){
        Difference dif = (Difference) obj;          
        if(dif.getTestNodeDetail().getNode()== null){
            //returnList.add(dif);          
            System.out.println(dif.getControlNodeDetail().getXpathLocation());
        }   
    }
    return returnList;
}

The output generated looks like this

/root[1]/a[1]
/root[1]/b[1]
/root[1]/c[1]
/root[1]/d[1]

Thanks for the help
--SD

SDS
  • 57
  • 2
  • 8
  • This is the Question, I want XMLUnit to return the missing elements, in this case 'c' and 'e' are missing /root[1]/c[1] is missing /root[1]/d[1]/e[1] is missing – SDS Jun 12 '12 at 16:00

1 Answers1

2

You are telling XMLUnit to only match elements with each other if their element names and the nested text are the same. This is not the case for your a or b elements (they differ in nested text), that's why you see them in your list.

You see d rather than the nested e as the top level ds are different according to the rules of RecursiveElementNameAndTextQualifier. If you remove the line setting the RecursiveElementNameAndTextQualifier your list reduces to

/root[1]/c[1]
/root[1]/d[1]/e[1]

which is what you were expecting.

Stefan Bodewig
  • 3,260
  • 15
  • 22