当然,如果您想比较两个 XML,您有一个 XML 单元,您可以逐个字符地进行比较。
XML 单元
编辑:
您必须从这里XML Sourceforge下载 de XMLUnit JAR ,然后像 JUnit 一样简单地将其添加到您的类路径(通过 Eclipse / NetBeans / 等等...),然后编写一个测试,如 JUnit,但从 XMLTestCase 扩展,如这个:
import org.custommonkey.xmlunit.*;
public class XMLTesting extends XMLTestCase {
public void testForEquality() throws Exception {
String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";
String myTestXML = "<msg><localId>2376</localId></msg>";
assertXMLEqual("comparing test xml to control xml", myControlXML, myTestXML);
assertXMLNotEqual("test xml not similar to control xml", myControlXML, myTestXML);
}
public void testIdentical() throws Exception {
String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";
String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";
Diff myDiff = new Diff(myControlXML, myTestXML);
assertTrue("pieces of XML are similar " + myDiff, myDiff.similar());
assertTrue("but are they identical? " + myDiff, myDiff.identical());
}
public void testAllDifferences() throws Exception {
String myControlXML = "<news><item id=\"1\">War</item>"
+ "<item id=\"2\">Plague</item><item id=\"3\">Famine</item></news>";
String myTestXML = "<news><item id=\"1\">Peace</item>"
+ "<item id=\"2\">Health</item><item id=\"3\">Plenty</item></news>";
DetailedDiff myDiff = new DetailedDiff(compareXML(myControlXML, myTestXML));
List allDifferences = myDiff.getAllDifferences();
assertEquals(myDiff.toString(), 0, allDifferences.size());
}
所以你可以看到比较两个 XML 的简单方法