0

I tried to write a test case in JUnit for comparing two xml files. Using xmlunit, I should be able to find all the differences. But while comparing, I'm getting false all the time, though I am giving similar kind of files. Timestamp is different for each xml file. So, though all the other contents in xml file are equal, it is returning false. Please provide me any proper way to handle this problem.

I need to ignore the timestamp tag in the xml file when comparing two xml files. And how do I ignore self closing tags?

<XML>
<TIMESTAMP>KALA</TIMESTAMP>
<B>JIHN</B>
<C>Lion</C>
</XML>

<XML>
<TIMESTAMP>QWER</TIMESTAMP>
<B>JIHN</B>
<C NAME =LION/>
</XML>
<B>JIHN</B>
1
  • Could you pass the XML through an XSLT template that strips out all the unwanted parts, before you do the comparison? Commented Jun 4, 2017 at 4:43

1 Answer 1

1

XMLUnit 2.x has the concept of NodeFilters, using that you can easily ignore the TIMESTAMP elements. Something like

String control = "<XML>\n"
    + "<TIMESTAMP>KALA</TIMESTAMP>\n"
    + "<B>JIHN</B>\n"
    + "<C>Lion</C>\n"
    + "</XML>";
String test = "<XML>\n"
    + "<TIMESTAMP>QWER</TIMESTAMP>\n"
    + "<B>JIHN</B>\n"
    + "<C NAME=\"LION\"/>\n"
    + "</XML>";
Diff d = DiffBuilder.compare(Input.fromString(control))
    .withTest(Input.fromString(test))
    .withNodeFilter(n -> !(n instanceof Element && Nodes.getQName(n).getLocalPart().equals("TIMESTAMP")))
    .build();

could be a start.

I'm not sure what you mean by self-closing tags. <x></x> and <x/> are identical as far as XMLUnit is concerned. The C elements in your example clearly are not. The first one has no attributes but a nested text, the second one has an attribute named NAME but no nested test.

Sign up to request clarification or add additional context in comments.

4 Comments

I couldn't undetstand the code.Please explain me in detail about how to implement in code. when I give string for TEST , I got an error. Test doesn't accepting the string."Diff d = DiffBuilder.compare(CONTROL).with(TEST)" . I would like to ignore two different elements of the xml file other than timestamp. Waiting for your help. Thanks in advance
@Teena I've edited the post to contain your full examples rather than placeholders (and made your second example well-formed XML by adding quotes around the NAME attribute's value and omitting the second B). The code compiles and if you run it d will not contain any differences about TIMESTAMP.
Thanks for the help. it was helpful. one more added to this I want to ignore more than one tags ,how can I achieve this? and the result also its returning identical. How can I print other differences too
@Teena the NodeFilter really only is a boolean expression on Nodes. So if you want to ignore other nodes as well, just create write a predicate that selects all of them, like looking up whether the local name is member inside a set of black-listed names. I'm not sure I understand the second part of your question. If you tell DiffBuilder to checkForSimilar you get all the differences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.