【问题标题】:Exception in thread "main" while trying to compare two xml's using XML Unit尝试使用 XML 单元比较两个 xml 时线程“主”中的异常
【发布时间】:2021-12-23 19:06:00
【问题描述】:
 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)

【问题讨论】:

  • 您可以将您的项目上传到 Github 并在此处发布链接吗?

标签: java xml testing xmlunit xmldiff


【解决方案1】:

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

此行出现错误。

printDifferences((List) differences); 

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

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

 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); 

然后,一切都会好起来的。

这能解决您的问题吗?在 cmets 中告诉我。

【讨论】:

  • 它对我有用....谢谢
  • @Akshay 如果此答案对您有所帮助,请随时将其标记为已接受并投票。这将帮助其他人找到并重复使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
相关资源
最近更新 更多