Search code examples
javaxmlxmlunitxmlunit-2

XMLUnit-2 compare partial XMLs


I have a local test XML data as below

testLocalXML

<root>
  <elementA>something different</elementA>
  <elementB>something else</elementB>
  <elementC>yet another thing</elementC>
</root>

but the data I'm getting from server could be large and will keep dynamically increasing e.g.

Server Response

<root>
  <elementA>something different</elementA>
  <elementB>something else</elementB>
  <elementC>yet another thing</elementC>
  <elementD>next data</elementD>
  <elementE>another data</elementE>
  <elementF>F data</elementF>
  .
  .
  ... so on...
</root>

I don't know which node I can ignore using below logic. My node are dynamic.

.withNodeFilter(node -> !(node.getNodeName().equals("elementD") ||
                          node.getNodeName().equals("elementE"))

Here, as server response is dynamic and keep on increasing how would I validate whatever data I mentioned in the testLocalXML using XMLUnit?


Solution

  • Well, I do know the elements that I'm interested in, so I could easily do

      final Collection<String> expected = Arrays.asList("root", "elementA", elementB", "elementC");
    

    And can add filter as

      .withNodeFilter(n -> expected.contains(n.getNodeName()))