如何使用XMLUnit比较2个不同文件中的2个或更多节点(同名)?
我有2个XML文件,如下所示:
<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 this....</result>
</SearchResults>
到目前为止,这是我用来比较的方法。问题是我不想将具有结果标记的xml部分与它们上的任何类型的属性标记进行比较。我怎么能这样做?
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 );
}
我发现在XMLUnit手册的第5页上使用ElementQualifier的模糊建议,但我还不明白。我不知道如何告诉它要比较哪些节点。
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);
我应该遵循该路线并将此课程添加到我的项目中吗?
org.apache.wink.test.diff.DiffWithAttributeQualifier
我想到了将节点放入NodeList然后使用 org.custommonkey.xmlunit.compareNodeList 的想法,但感觉就像是黑客。还有比这更好的方法吗?
答案 0 :(得分:3)
使用XPath测试会不会更容易?我想像是这样的事情
//select all elements which don't have a type attribute
String xpath = "//result[not(@type)]";
assertXpathsEqual(xpath, file1sub, xpath, file1sub2)