我试图使用XML1.3来比较两个类似的XMLUnit文件。基本上每件事都是一样的。file1是file2的一个副本。
但是在File2中,我改变了一个节点中某些值/元素的顺序。
file1.xml
<root>
<ent>
<value>
<string>ada,cmb</string>
</value>
</ent>
</root>file2.xml
<root>
<ent>
<value>
<string>cmb,ada</string>
</value>
</ent>
</root>在我的例子中,两个文件必须是相等的。是否有可能通过XMLUnit实现这一目标?
我的代码
public void testXml() throws ParserConfigurationException, SAXException, IOException {
String refXmlPaht = "../test1.xml";
String testXmlPaht = "../test2.xml";
Document doc1 = TransformXML.convertXmlToDom(refXmlPaht);
Document doc2 = TransformXML.convertXmlToDom(testXmlPaht);
Diff myDiff = new Diff(doc1, doc2);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreAttributeOrder(true);
assertXMLEqual("pieces of XML are not similar ", myDiff, true);
assertTrue("but are they identical? " + myDiff, myDiff.identical());
}XMLUnit response
junit.framework.AssertionFailedError: but are they identical?
org.custommonkey.xmlunit.Diff
[different] Expected text value 'ada,cmb' but was 'cmb,ada' - comparing <string ...>ada,cmb</string> at /root[1]/ent[1]/value[1]/string[1]/text()[1] to <string ...>cmb,ada</string> at /root[1]/ent[1]/value[1]/string[1]/text()[1]...谢谢你的帮助。
诚挚的问候,
科班
发布于 2011-03-18 10:55:51
您可以创建自己的DifferenceListener,它实现了以下方法:
int differenceFound(Difference difference);无论何时检测到差异,都将调用此方法,然后您可以对控件和测试节点的内容执行一些字符串检查,以确定它们对您是否意味着相同。如果是,则可以返回一个RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL值,以便XMLUnit将节点视为相同的。
有关更多信息,请查看用户指南。
https://stackoverflow.com/questions/5350807
复制相似问题