Reputation: 3272
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?
Upvotes: 1
Views: 2902
Reputation: 1
FileReader fr1 = new FileReader("/home/Downloads/xml1.xml");
FileReader fr2 = new FileReader("/home/Downloads/xml2.xml");
Try passing xml files using FileReader
.
Upvotes: 0
Reputation: 1
You are getting this error becouse of getResource() method. It does not return the reference object to your xml file. You can directly pass the file name to FileReader contructor or you can create a File class object. I have imporoved your block of code, you can copy and run it. It will work 100 % :) :)
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) {
File f1 = new File("D:/reference.xml");
File f2= new File("D:/comparison.xml");
FileReader fr1 = null;
FileReader fr2 = null;
try {
fr1 = new FileReader(f1);
fr2 = new FileReader(f2);
} 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();
}
}
}
Upvotes: 0
Reputation: 15434
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"
Upvotes: 2