I wrote a simple python tool for this called xmldiffs:

Compare two XML files, ignoring element and attribute order.

Usage: xmldiffs [OPTION] FILE1 FILE2

Any extra options are passed to the diff command.

Get it at https://github.com/joh/xmldiffs

Answer from joh on Stack Overflow
🌐
GitHub
github.com › joh › xmldiffs
GitHub - joh/xmldiffs: Compare two XML files, ignoring element and attribute order.
-o, --output - Name of the output file. Default value is minus sign, which prints the diff to the stdout. -x, --xml - instead of comparing two xml files, write sorted contents of FILE1 to FILE2. In this mode the --output option is ignored.
Starred by 96 users
Forked by 27 users
Languages   Python 100.0% | Python 100.0%
Top answer
1 of 9
30

For xmlunit 2.0 (I was looking for this) it is now done, by using DefaultNodeMatcher

Diff diff = Diffbuilder.compare(Input.fromFile(control))
   .withTest(Input.fromFile(test))
   .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
   .build()

Hope this helps this helps other people googling...

2 of 9
19

My original answer is outdated. If I would have to build it again i would use xmlunit 2 and xmlunit-matchers. Please note that for xml unit a different order is always 'similar' not equals.

@Test
public void testXmlUnit() {
    String myControlXML = "<test><elem>a</elem><elem>b</elem></test>";
    String expected = "<test><elem>b</elem><elem>a</elem></test>";
    assertThat(myControlXML, isSimilarTo(expected)
            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)));
    //In case you wan't to ignore whitespaces add ignoreWhitespace().normalizeWhitespace()
    assertThat(myControlXML, isSimilarTo(expected)
        .ignoreWhitespace()
        .normalizeWhitespace()
        .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)));
}  

If somebody still want't to use a pure java implementation here it is. This implementation extracts the content from xml and compares the list ignoring order.

public static Document loadXMLFromString(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = 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 all = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    List<String> values = new ArrayList<>();
    if (all != null && all.getLength() > 0) {
        for (int i = 0; i < all.getLength(); i++) {
            values.add(all.item(i).getTextContent());
        }
    }
    Set<String> expected = new HashSet<>(Arrays.asList("a", "b"));
    assertThat("List equality without order",
            values, containsInAnyOrder(expected.toArray()));
}
🌐
dale lane
dalelane.co.uk › blog
Comparing XML files ignoring order of attributes and child elements « dale lane
October 6, 2014 - Instead, to compare two of my XML files, my approach is to sort them both so they have a consistent order, and then diff the sorted files using an existing visual diff tool. (On Windows, I prefer vsdiff from SlickEdit. On Mac, I prefer diffmerge.
🌐
Oxygen XML
oxygenxml.com › home › board index › oxygen xml editor/author/developer › feature request
Diff Files and Dirs: Add Ignore Element order for XML documents - Oxygen XML Forum
Maybe one solution that we can provide more easily would be to rearrange the order of the elements in the editor based on the values of a specified attribute. We can make the "Format and Indent" action to sort the elements in a specific order. Regards, Octavian · Octavian Nadolu <oXygen/> XML Editor http://www.oxygenxml.com
🌐
GitHub
github.com › xmlunit › xmlunit › issues › 139
Comparing XML files ignoring elements attribute Order · Issue #139 · xmlunit/xmlunit
July 11, 2018 - import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLAssert; import org.testng.annotations.Test; import org.xmlunit.builder.DiffBuilder; import org.xmlunit.builder.Input; import org.xmlunit.diff.DefaultNodeMatcher; import org.xmlunit.diff.ElementSelectors; import org.xmlunit.matchers.CompareMatcher; public class TestNg { @test public void testXmlUnit() { String ControlXML = "CCST810000510DRAGON76810000510500227681000051050024510DRAGON76810000510500117681000051050014510RKH5D10"; String testXml = "CCST810000510DRAGON76810000510500117681000051050014510DRAGON76810000510500227681000051050024510FHMTN10"; assertThat(testXml, CompareMatcher.isSimilarTo(ControlXML).ignoreWhitespace().normalizeWhitespace().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText, ElementSelectors.byName)));
Author   vramsprsk
Find elsewhere
🌐
Xlcompare
xlcompare.com › compare-xml-files.html
Compare XML Files Online. Free. No Upload. Get the Semantic XML diffs.
October 3, 2009 - Comparing XML nodes by key sub-items of attributes is the ultimate option to determine if two nodes are matching. It detects additions, deletions and updates of the XML nodes. Ignore Order option allows you to ignore moves of the XML elements.
🌐
Altova
altova.com › xmlspy-xml-editor › compare-xml
Compare XML Files | Altova
The order of XML attributes is irrelevant because XML processors do not consider the sequence that attributes appear in a particular element. XMLSpy accounts for this and intelligently ignores the attribute order, but a conventional differencing utility cannot and would therefore report every ...
🌐
Scooter Forums
forum.scootersoftware.com › home › beyond compare 4 discussion › general
Ignore XML attribute order when comparing XML - Scooter Forums
Hello, The Text Compare considers the positioning as important, but can align character by character within a line. If components are out of order, this will detect as a difference, but we do have File Formats which can sort or tidy the XML files, including resorting the attributes, available for download here: http://www.scootersoftware.com/downl..._moreformatsv4
🌐
JSoftwareLabs
jsoftwarelabs.com › compare-diff-online › xml-comparison
Compare xml online - XML diff tool
JSoftwareLabs' free XML comparator tool allows you to compare XML nodes visualize the semantic differences. Paste your XML code,compare the code side-by-side and see a detailed table with change locations. Our online diff checker tool boasts the capability to ignore the order & namespaces of XML elements or nodes, enhancing its versatility and accuracy.
🌐
GitHub
github.com › prettydiff › prettydiff › issues › 89
Xml comparison ignoring child element order · Issue #89 · prettydiff/prettydiff
October 2, 2014 - Hi. I would find a lot of use comparing two xml files while ignoring the order of child elements (with the logical extension of attributes), if that's a possibility for v2.0. I know that's ...
Published   May 18, 2015
Author   brettmorien
🌐
Jan Katins
katzien.de › en › posts › 2022-12-13-diffing-big-xml-ignoring-order
Diffing XML and ignoring element and attribute order · Jan Katins
December 13, 2022 - Given that I didn’t want a readable diff, just some confirmation that the file content was the same, I settled for this pipeline: Make a canonical XML all attributes are in the same order · Pretty print it so there is one element per line and the indention is the same ... # Prepare the original file tmp/orig_sorted.xml: data/orig.xml mkdir -p tmp/ xmllint --exc-c14n data/orig.xml | xmllint --format /dev/stdin | sort > tmp/orig_sorted.xml .PHONY: test-export test-export: tmp/orig_sorted.xml do-export -o exports/whatever.xml xmllint --exc-c14n exports/whatever.xml | xmllint --format /dev/stdin | sort > tmp/whatever_sorted.xml diff -u --speed-large-files tmp/orig_sorted.xml tmp/whatever_sorted.xml > tmp/diff.txt || true head -n 30 tmp/diff.txt