package util;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.SAXException;
/** * * Java program to compare two XML files using XMLUnit example * @author Javin Paul */
public class XMLComparator { public static void main(String args[]) throws
FileNotFoundException, SAXException, IOException
{ // reading two xml file to compare in Java program
FileInputStream fis1 = new
FileInputStream("C:\\Users\\akshay_gawand\\Desktop\\8.3\\Transfer\\testcasemcs6.0");
FileInputStream fis2 = new
FileInputStream("C:\\Users\\akshay_gawand\\Desktop\\8.3\\Transfer\\testcasemcs7.0");
BufferedReader source = new BufferedReader(new InputStreamReader(fis1));
BufferedReader target = new BufferedReader(new InputStreamReader(fis2)); //configuring
XMLUnit to ignore white spaces
XMLUnit.setIgnoreWhitespace(true); //comparing two XML using XMLUnit in Java
List differences = compareXML(source, target); //showing differences found in two xml
files
printDifferences((List) differences);
}
public static List compareXML(Reader source, Reader target) throws SAXException,
IOException
{ //creating Diff instance to compare two XML files
Diff xmlDiff = new Diff(source, target); //for getting detailed differences between
two xml files
DetailedDiff detailXmlDiff = new DetailedDiff(xmlDiff);
return detailXmlDiff.getAllDifferences();
}
public static void printDifferences(List<String> differences)
{
int totalDifferences = differences.size();
System.out.println("===============================");
System.out.println("Total differences : " + totalDifferences);
System.out.println("================================");
for(String difference : differences)
{
System.out.println(difference );
}
}
}
以上是我使用的代码,请帮助我,我试图使用 XML 单元比较两个 XMLS 文件,但在线程“main”中出现异常异常 总差异:23
错误跟踪:
java.lang.ClassCastException: class org.custommonkey.xmlunit.Difference cannot be cast to class java.lang.String (org.custommonkey.xmlunit.Difference is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
at util.XMLComparator.printDifferences(XMLComparator.java:42)
at util.XMLComparator.main(XMLComparator.java:27)