How do I use XMLUnit to compare 2 or more nodes (of the same name) in 2 different files?
如何使用XMLUnit比较2个不同文件中的2个或更多节点(同名)?
I have 2 XML files that look like this:
我有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>
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?
这是我到目前为止用来比较的方法。问题是我不想比较具有结果标记的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 );
}
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.
我发现在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);
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?
这个想法让我想到将节点放入NodeList,然后使用org.custommonkey.xmlunit.compareNodeList,但感觉就像是黑客。还有比这更好的方法吗?
1 个解决方案
#1
3
Wouldn't it be easier to use XPath Tests? I imagine something like this to work
使用XPath测试不是更容易吗?我想这样的事情可行
//select all elements which don't have a type attribute
String xpath = "//result[not(@type)]";
assertXpathsEqual(xpath, file1sub, xpath, file1sub2)
#1
3
Wouldn't it be easier to use XPath Tests? I imagine something like this to work
使用XPath测试不是更容易吗?我想这样的事情可行
//select all elements which don't have a type attribute
String xpath = "//result[not(@type)]";
assertXpathsEqual(xpath, file1sub, xpath, file1sub2)