开发者

How do I use XMLUnit to compare only certain parts of files?

How do I use XMLUnit to compare 2 or more nodes (of the same name) in 2 different files?

I have 2 XML files that look like this:

<SearchResults>
  <result type="header"> ...ignore this.... </result>
  <result type="secondheader">...ignore this....</result>
  <result>....data1....</result>
  <result>....data2....</result>
  <result>....data3....</result>
  <result type="footer">...ignore 开发者_JAVA技巧this....</result>
</SearchResults>

And here is my method that I use to compare so far. The problem is that I do not want to compare the parts of the xml that have a result tag with any kind of attribute flag on them. How can I do this?

public void compareXMLEqualityToLastTest() throws Exception { 
  System.out.println("Checking differences.");
  File firstFile = new File("C:\\Eclipse\\workspace\\Tests\\log\\" +
              "Test_2.xml");
  String file1sub = readXMLFromFile(firstFile);
  File secondFile= new File("C:\\Eclipse\\workspace\\Tests\\log\\" +
              "Test_1.xml");
  String file2sub = readXMLFromFile(secondFile);
  assertXMLNotEqual("files are equal", file1sub, file2sub );
  assertXMLEqual("files are not equal", file1sub, file2sub );
}

I found a vague suggestion to use a ElementQualifier on page 5 of the XMLUnit manual, but I don't understand it yet. I wouldn't know how to tell it which nodes to compare.

Diff myDiff = new Diff(file1sub, file2sub);
      myDiff.overrideElementQualifier(new ElementNameAndTextQualifier());
      assertXMLEqual("But they are equal when an ElementQualifier controls " +
    "which test element is compared with each control element", myDiff, true);

Should I follow that route and add this class to my project?

org.apache.wink.test.diff.DiffWithAttributeQualifier

The thought crossed my mind to put the nodes into a NodeList and then use org.custommonkey.xmlunit.compareNodeList but that feels like that is a hack. Is there a better way than that?


Wouldn't it be easier to use XPath Tests? I imagine something like this to work

//select all elements which don't have a type attribute
String xpath = "//result[not(@type)]";
assertXpathsEqual(xpath, file1sub, xpath, file1sub2)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜