I'm trying to compare 2 XML files using XMLUnit. I tried with the following code in eclipse, I have placed the xml files to be compared in the local directory.
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.xml.sax.SAXException;
public class ComparisonTest {
public static void main(String[] args) {
URL url1 = ComparisonTest.class.getResource("D:/reference.xml");
URL url2 = ComparisonTest.class.getResource("D:/comparison.xml");
FileReader fr1 = null;
FileReader fr2 = null;
try {
fr1 = new FileReader(url1.getPath());
fr2 = new FileReader(url2.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
Diff diff = new Diff(fr1, fr2);
System.out.println("Similar? " + diff.similar());
System.out.println("Identical? " + diff.identical());
DetailedDiff detDiff = new DetailedDiff(diff);
List differences = detDiff.getAllDifferences();
for (Object object : differences) {
Difference difference = (Difference)object;
System.out.println("***********************");
System.out.println(difference);
System.out.println("***********************");
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I'm getting an error as
Exception in thread "main" java.lang.NullPointerException
at com.org.comparison.ComparisonTest.main(ComparisonTest.java:21)
I'm not able to figure out why?
I'm not sure that you can pass "D:/reference.xml" as resource. Just pass paths to constructor of FileReader
:
FileReader fr1 = null;
FileReader fr2 = null;
try {
fr1 = new FileReader("D:/reference.xml");
fr2 = new FileReader("D:/comparison.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
I'm pretty sure that getResource
gives you null.
URL url1 = ComparisonTest.class.getResource("D:/reference.xml");
URL url2 = ComparisonTest.class.getResource("D:/comparison.xml");
System.out.println(url1 + " " + url2); // I suppose output is "null null"