0
 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)
4

1 回答 1

0

尝试使用 XML 单元比较两个 xml 时线程“主”中的异常

错误发生在这一行。

printDifferences((List) differences); 

哪些对象位于对象中,称为差异?这是一些XML Unit类。它可能看起来像List<XmlUnitClass>什么,该方法期望输入什么?字符串列表!

所以你需要稍微修改一下你的代码,就像这样......

 List differences = compareXML(source, target); //showing differences found in two xml 
      files 
 List<String> diffStrings = new ArrayList<String>()
  for(var diff: differences) { 
       diffStrings.add(diff.toString())
  }   
     printDifferences((List) diffStrings); 

然后,一切都会奏效。

这能解决你的问题吗?在评论中告诉我。

于 2021-11-11T09:48:34.827 回答