使用XMLUnit查找两个XML文件中的差异 [英] Finding differences in two XML files using XMLUnit
问题描述
在使用XMLUnit的DetailDiff函数时如何仅检查特定节点而不是所有节点是否存在差异
How to check for only particular nodes and not all nodes for difference while using the DetailedDiff function of XMLUnit
这是我的代码:
public static void main(String[] args)
{
//URL url1 = xmlunittest.class.getResource("MSHIS1.xml");
// URL url2 = xmlunittest.class.getResource("MSHIS.xml");
Logger L = new Logger();
FileReader fr1 = null;
int countofdifferences = 0;
FileReader fr2 = null;
try {
fr1 = new FileReader("MSHIS.xml");
fr2 = new FileReader("MSHIS1.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
Diff diff = new Diff(fr1, fr2);
L.writeToBVTLOG("Similar? " + diff.similar());
L.writeToBVTLOG("Identical? " + diff.identical());
DetailedDiff detDiff = new DetailedDiff(diff);
List differences = detDiff.getAllDifferences();
for (Object object : differences) {
Difference difference = (Difference)object;
L.writeToBVTLOG("------------------------");
L.writeToBVTLOG(difference.toString());
L.writeToBVTLOG("------------------------");
countofdifferences++;
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
L.writeToBVTLOG(countofdifferences);
}
但是事实是,我只想让程序告诉我4个特定节点是否发生了任何更改.那么如何列出所有差异.
But the thing is that, I just want the program to tell me if 4 particular nodes have undergone any changes. So how to get there from listing all the differences.
推荐答案
您可以先使用javax.xml.transform.TransformerFactory
转换两个文档,使其仅包含您感兴趣的节点.或者,您可以在Difference上使用getControlNodeDetail()和getTestNodeDetail()来查看差异是否适用于所需的节点.
You might transform both documents first with javax.xml.transform.TransformerFactory
so that it only contains the nodes you are interested. Or you could use getControlNodeDetail() and getTestNodeDetail() on Difference to see if the difference applies to the nodes you want.
这篇关于使用XMLUnit查找两个XML文件中的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!