如何忽略xmlUnit中的一行

时间:2014-03-12 13:02:31

标签: eclipse eclipse-plugin xmlunit

您好,我想知道如何忽略XMLUnit中的一行。例如,如果我有以下标记总是不同的,例如文件1:8文件2:10,文件的其余部分应该匹配,我该怎么办?

我使用的示例代码是:

public class CommentDiffTest {

@Test
public void doTest() throws Exception{
    FileReader fr1 = null;
    FileReader fr2 = null;
    try {
        fr1 = new FileReader("file1.xml");
        fr2 = new FileReader("file2.xml");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("ERROR FILES FOR COMPARISON COULD NOT BE FOUND!");
    }

    //Add init code here.

    try {
        Diff diff = new Diff(fr1, fr2);
        System.out.println(diff.similar());
        System.out.println(diff.identical());

        DetailedDiff detDiff = new DetailedDiff(diff);
        List<String> 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();
    }
}
}

1 个答案:

答案 0 :(得分:1)

如果您知道它适用于您要忽略的代码,则您可以覆盖DifferenceListener并将差异降级为RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL。但是,您需要通过节点名称或XPath而不是行号来识别它。