java - Compare two XML strings ignoring element order -
support have 2 xml strings
<test> <elem>a</elem> <elem>b</elem> </test> <test> <elem>b</elem> <elem>a</elem> </test> how write test compares 2 strings , ignores element order?
i want test short possible, no place 10-line xml parsing etc. i'm looking simple assertion or smt similar.
i have (which doesnt work)
diff diff = xmlunit.comparexml(expectedstring, actualstring); xmlassert.assertxmlequal("meh", diff, true);
public static document loadxmlfromstring(string xml) throws exception { documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); inputsource = new inputsource(new stringreader(xml)); return builder.parse(is); } @test public void test() throws exception { document doc = loadxmlfromstring("<test>\n" + " <elem>b</elem>\n" + " <elem>a</elem>\n" + "</test>"); xpathfactory xpathfactory = xpathfactory.newinstance(); xpath xpath = xpathfactory.newxpath(); xpathexpression expr = xpath.compile("//test//elem"); nodelist = (nodelist) expr.evaluate(doc, xpathconstants.nodeset); set<string> values = new hashset<>(); if (all != null && all.getlength() > 0) { (int = 0; < all.getlength(); i++) { values.add(all.item(i).gettextcontent()); } } set<string> expected = new hashset<>(arrays.aslist("a", "b")); assertequals(expected, values); } and compare sets
Comments
Post a Comment