Reputation: 1
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 );
}
}
}
Above is the code I used,Please help me I was trying to compare two XMLS file using XML unit, but getting below exception Exception in thread "main" Total differences : 23
Error trace:
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)
Upvotes: 0
Views: 264
Reputation: 2819
Exception in thread "main" while trying to compare two xml's using XML Unit
The error occurs in this line.
printDifferences((List) differences);
What objects are located in the object called differences ? These are some XML Unit
classes. Its likely to look like List<XmlUnitClass>
and what does the method expect as input ? A list of strings !
So you need to massage your code a bit, like this ...
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);
Then, everything will work.
Does this solve your problem ? Tell me in the comments.
Upvotes: 0