1

Hi I was wondering how I can ignore a line in XMLUnit. For instance if I had the following tag which would always be different e.g file 1: 8 file 2: 10 and the rest of the file should match, how could I do this?

The sample code I was using is:

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();
    }
}
}
Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32
  • Did you check this link?http://stackoverflow.com/questions/1241593/java-how-do-i-ignore-certain-elements-when-comparing-xml – Mouna Apr 30 '14 at 13:16

1 Answers1

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.

Stefan Bodewig
  • 3,260
  • 15
  • 22