I'm looking for an API which compares two XML data. I've tried XMLUnit 2 but couldn't find a way to make it work properly with my example. Could you give me an example which works for my need?
My first XML data xml1
:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<urlset xmlns="http://www.sitemap.org/schemas/sitemap/0.9">
<url>
<loc>a1/</loc>
<lastmod>a2</lastmod>
</url>
<url>
<loc>b1</loc>
<lastmod>b2</lastmod>
</url>
<url>
<loc>c1</loc>
<lastmod>c2</lastmod>
</url>
</urlset>
My second XML data xml2
:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<urlset xmlns="http://www.sitemap.org/schemas/sitemap/0.9">
<url><lastmod>b2</lastmod><loc>b1</loc></url>
<url>
<lastmod>c2</lastmod>
<loc>c1</loc>
</url>
<url>
<loc>a1/</loc>
<lastmod>a2</lastmod>
</url>
</urlset>
Notice:
- Same size (here 3 children)
urlset
's child nodes (url
) may not be orderedurl
's elements (loc
andlastmod
) may not be ordered- White spaces are ignored
Looking for an API which returns true
like:
XMLUtils.isSimilar(xml1, xml2);
My unsuccessful attempts with XMLUnit 2 (tried with multiple "NodeMatcher"):
// Attempt with XmlAssert.assertThat:
XmlAssert.assertThat(xml1)
.and(xml2)
.ignoreChildNodesOrder()
.ignoreWhitespace()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
.areSimilar();
// Attempt with Diff
Diff myDiff = DiffBuilder.compare(xml1)
.withTest(xml2)
.ignoreWhitespace()
.checkForSimilar()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
.build();
myDiff.getDifferences();