1

嗨,我想知道如何忽略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();
    }
}
}
4

1 回答 1

1

You'd override the DifferenceListener with one that downgrades the difference to RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL if you know it applies to the tag you want to ignore. You'll need to identify it by node name or XPath rather than line number, though.

于 2015-02-03T19:50:40.943 回答