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
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()));
}
Discussions

Diff Files and Dirs: Add Ignore Element order for XML documents - Oxygen XML Forum
Hello, I would like to ask You to add a feature to Diff Files and Dirs. When perfoming diff of XML documents, I would like to be able to check Ignore Element order in the same style as Ignore Attribute order now. Use case: The comparsion of XML documents produced by e. More on oxygenxml.com
🌐 oxygenxml.com
Feature request: Ignore elements order when comparing xml - Scooter Forums
I compare xml files. Application writes xml-nodes in different order and I get changes each time I compare it. Is it possible in BCompare to ignore changes of element order when comparing xml-files? More on forum.scootersoftware.com
🌐 forum.scootersoftware.com
June 4, 2024
diff - Compare XML ignoring element order - Stack Overflow
With XMLUnit 2 how do you compare two documents without taking the element order into account? I got this question for XMLUnit 1, but apparently the new API in v2 doesn't have the mentioned method More on stackoverflow.com
🌐 stackoverflow.com
Comparing XML files ignoring elements attribute Order
And i need to compare these xml files using java and return boolean value as true if there are no differences. And the challenge here is the Second Xml file (Test2.xml) the elements order is always jumbled I tried XMLUnit2 for comparing the 2 xml strings but it is failing if there are multiple ... More on github.com
🌐 github.com
11
August 30, 2018
🌐
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%
🌐
JSoftwareLabs
jsoftwarelabs.com › compare-diff-online › xml-comparison
Compare xml online - XML diff tool
Best online XML diff and compare tool with ignoring order of elements and namespaces capability. Supports large files and different xml objects and code compare
🌐
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
🌐
Xlcompare
xlcompare.com › compare-xml-files.html
Compare XML Files Online. Free. No Upload. Get the Semantic XML diffs.
October 3, 2009 - This function enables you to compare XML documents efficiently, even when the elements and attributes are presented in a different sequence. By selecting the Ignore Order option, you ensure that the comparison focuses solely on the content, ...
🌐
Scooter Forums
forum.scootersoftware.com › home › beyond compare 4 discussion › general
Feature request: Ignore elements order when comparing xml - Scooter Forums
June 4, 2024 - I compare xml files. Application writes xml-nodes in different order and I get changes each time I compare it. Is it possible in BCompare to ignore changes of element order when comparing xml-files? So that these files would be treated as the same (or even better = as a minor change so that you can toggle between strict and
Find elsewhere
🌐
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 ...
🌐
GitHub
github.com › xmlunit › xmlunit › issues › 139
Comparing XML files ignoring elements attribute Order · Issue #139 · xmlunit/xmlunit
August 30, 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
🌐
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
🌐
dale lane
dalelane.co.uk › blog
Comparing XML files ignoring order of attributes and child elements « dale lane
October 6, 2014 - Diff tools are complex, and I’m in a hurry without time to implement one. 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.
🌐
GitHub
github.com › prettydiff › prettydiff › issues › 89
Xml comparison ignoring child element order · Issue #89 · prettydiff/prettydiff
May 18, 2015 - 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
🌐
Lightrun
lightrun.com › answers › xmlunit-xmlunit-comparing-xml-files-ignoring-elements-attribute-order
Comparing XML files ignoring elements attribute Order
Compare 2 xml files ignoring elements/attribute order and list out only the differences in the report using c#Read more >
🌐
JSoftwareLabs
jsoftwarelabs.com › jslutils › xml-comparison
Compare XML code online - XML diff checker and diff finder ...
Best online XML diff and compare tool with ignoring order of elements and namespaces capability. Supports large files and different xml objects and code compare