Java XML文件比较

时间:2013-02-04 11:58:40

标签: java xml java-api xmlunit

我正在尝试使用XMLUnit比较2个XML文件。我在eclipse中尝试使用以下代码,我已将xml文件放在本地目录中进行比较。

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

}

我收到错误

Exception in thread "main" java.lang.NullPointerException
at com.org.comparison.ComparisonTest.main(ComparisonTest.java:21)

我无法找出原因?

2 个答案:

答案 0 :(得分:2)

我不确定您是否可以将“D:/reference.xml”作为资源传递。只需将路径传递给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();
}

我很确定getResource会给你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"

答案 1 :(得分:0)

由于getResource()方法,您收到此错误。它不会将引用对象返回到xml文件。您可以直接将文件名传递给FileReader构造函数,也可以创建File类对象。我已经改编了你的代码块,你可以复制并运行它。它将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();
}
}

}