[Quote=引用 8 楼 huntor 的回复:]
xmlunithttp://xmlunit.sourceforge.net/
Java codepublicvoid 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);
}publicvoid 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());
}publicvoid 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());
}
XMLUnit for Java provides two JUnit extension classes, XMLAssert and XMLTestCase, and a set of supporting classes (e.g. Diff, DetailedDiff,Transform,SimpleXpathEngine,Validator,NodeTest) that allow assertions to be made about:
* The differences between two pieces of XML
* The outcome of transforming a piece of XML using XSLT
* The evaluation of an XPath expression on a piece of XML
* The validity of a piece of XML
* Individual nodes in a piece of XML that are exposed by DOM Traversal
XMLUnit for Java can also treat HTML content (even badly-formed HTML) as valid XML to allow these assertions to be made about the content of web pages too.
[/Quote]