Skip to content
Format XML

XML Tools

How to Compare XML Files

Six practical ways to diff two XML documents — from a quick terminal one-liner to semantic comparison libraries that ignore attribute order and whitespace.

Why comparing XML is harder than comparing plain text

A regular diff works line by line. XML does not care about line breaks. Two documents can be semantically identical yet look completely different as text — one indented with tabs, the other minified to a single line; attributes listed in a different order; an empty element written as <x/> versus <x></x>.

A plain diff will flag all of these as changes. A semantic XML diff understands the tree structure and reports only real data differences. The right method depends on whether you need textual or semantic comparison.

Before comparing, it helps to format both files with the same indentation style. This eliminates whitespace noise and makes even a basic line-level diff far more useful.

Method 1: Format first, then diff

The simplest approach: normalize both files to the same pretty-printed format, then run a standard diff. On macOS or Linux with xmllint installed:

diff <(xmllint --format a.xml) <(xmllint --format b.xml)

This handles whitespace and self-closing tag differences. It does not handle attribute reordering — if a.xml has <item id="1" type="A"/> and b.xml has <item type="A" id="1"/>, diff will report a change even though the XML is equivalent.

When to use it: you control the files and know attributes are always in the same order. Quick, zero-install on most systems.

Method 2: Canonical XML with xmllint

XML Canonicalization (C14N) is a W3C standard that defines a single normalized form for any XML document. It sorts attributes alphabetically, expands empty elements, normalizes namespace declarations, and uses consistent whitespace. This makes line-level diff reliable even when the original files differ cosmetically.

diff <(xmllint --c14n a.xml) <(xmllint --c14n b.xml)

The output is not pretty-printed (C14N doesn’t indent), so you can pipe each side through xmllint --format afterwards for readability:

diff <(xmllint --c14n a.xml | xmllint --format -) \
     <(xmllint --c14n b.xml | xmllint --format -)

When to use it: you need to compare files that may have different attribute order, namespace prefixes, or whitespace, and you want a proper semantic check with only standard tools.

Method 3: Python with xmldiff

The xmldiff library compares two XML trees and returns a list of edit actions (insert, delete, rename, move) that transform one into the other:

pip install xmldiff
from xmldiff import main, formatting

# Get a list of edit actions
diff = main.diff_files("a.xml", "b.xml")
for action in diff:
    print(action)

# Or produce a patched XML with change annotations
result = main.diff_files(
    "a.xml", "b.xml",
    formatter=formatting.XMLFormatter()
)
print(result)

You can also use it from the command line: xmldiff a.xml b.xml.

When to use it: you want true semantic diff that understands element moves and renames, or you need to integrate XML comparison into a Python script or CI pipeline.

Method 4: Java with XMLUnit

XMLUnit is the standard library for XML comparison in Java and is commonly used in test suites to assert that API responses or generated documents match expected output.

import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.diff.Diff;

Diff diff = DiffBuilder
    .compare(controlXml)
    .withTest(testXml)
    .ignoreWhitespace()
    .ignoreComments()
    .checkForSimilar()   // attribute order doesn't matter
    .build();

if (diff.hasDifferences()) {
    diff.getDifferences().forEach(System.out::println);
} else {
    System.out.println("Documents are equivalent.");
}

When to use it: you are in a Java/JVM environment, especially for automated testing where you need assertions like “these two XML documents are semantically equal.”

Method 5: VS Code or any diff-capable editor

Most code editors have built-in file comparison. In VS Code, select two files in the Explorer sidebar, right-click, and choose “Compare Selected.” Or from the command palette:

code --diff a.xml b.xml

This is a textual diff with syntax highlighting. For better results, format both files with the same indentation before comparing. You can do this by pasting each file into the Format XML tool, copying the formatted output, and saving it back before running the diff.

IntelliJ, Sublime Merge, and Beyond Compare all offer similar side-by-side views. Beyond Compare has an XML-aware mode that ignores attribute order.

When to use it: you want a visual, side-by-side view and are comfortable with a textual (not semantic) comparison.

Method 6: Online XML diff tools

Several websites let you paste two XML documents and see the differences highlighted in a browser. This works well for quick, one-off checks on non-sensitive data.

Before using any online comparison tool, watch out for:

  • Privacy: your XML is sent to a server. Do not paste files with credentials, PII, or proprietary data.
  • Size limits: most online diff tools cap input at a few hundred KB.
  • Semantic vs. textual: check whether the tool does a real tree diff or just a line-by-line text comparison.

Formatting both documents with the same settings first removes whitespace noise from the results. Paste each file into the XML formatter to normalize indentation before comparing.

Which method should you choose?

MethodTypeHandles attr orderSetup
Format + diffTextualNoxmllint
C14N + diffCanonicalYesxmllint
Python xmldiffSemanticYespip install
Java XMLUnitSemanticYesMaven/Gradle
VS Code diffTextualNoNone
Online diffVariesVariesNone

For a quick manual check, format both files and use diff or your editor. For CI pipelines or test suites, use xmldiff or XMLUnit so attribute order and whitespace variations do not cause false failures. For the most robust command-line check, canonicalize with xmllint --c14n before diffing.

Tips for cleaner XML diffs

  • Normalize indentation first. The single biggest source of false positives in XML diffs is inconsistent whitespace. Format both files with the same indent (2 spaces, 4 spaces, or tabs) before comparing.
  • Strip comments if irrelevant. Comments often change between versions (timestamps, generator info). Most diff tools have an option to ignore them. In xmldiff, comments are ignored by default.
  • Watch for namespace prefix changes. Renaming ns1: to foo: changes nothing semantically, but text-based diffs flag every element. C14N and semantic diff tools handle this correctly.
  • Validate before comparing. If one file has a syntax error, diff tools may crash or produce misleading output. Run both files through an XML validator first.
  • Version-control XML as formatted text. If you store XML in git, always commit it pretty-printed. This makes git diff useful out of the box and keeps commit history meaningful.

Format your XML before comparing

Paste both XML documents into the formatter one at a time, pick the same indent style, and copy the output. With consistent formatting, even a basic diff gives clean, accurate results — no semantic diff library needed.

Open Format XML