How to compare two XML files and find the differences
XML Compare — open the free tool →XML files rarely change in a tidy way. One version comes from an API as a single minified line, the other was saved by an editor with tabs, and a third tool writes the same attributes in a different order. A plain line-by-line diff then marks almost everything as changed, and the one value you care about gets lost.
The fix is simple: normalize first, then compare. This guide shows how, with four methods.
Why a plain diff struggles with XML
- Formatting:
<a><b>1</b></a>on one line and the same thing indented over three lines mean exactly the same, but look completely different to a line diff. - Attribute order:
<item id="7" status="new"/>and<item status="new" id="7"/>are equivalent XML. - Element order does matter: unlike attributes, the order of child elements is part of the document, so a moved element is a real change.
Method 1: Compare XML online (fastest)
- Open XML Compare.
- Paste the original XML on the left and the changed XML on the right.
- Leave Format XML ticked. Both documents are checked for well-formedness and pretty-printed with the same indentation.
- Tick Sort attributes if the files come from different tools that order attributes differently.
- Click Compare now and use Next change to step through changed elements, attribute values and text.
If one side is not well-formed (for example an unclosed tag or a bare &), the tool tells you which side and roughly which line, and compares that side as plain text. The XML is processed in your browser and not uploaded.
Limits: it does not validate against an XSD schema, and a moved element shows as removed in one place and added in another.
Method 2: Notepad++ with the ComparePlus plugin
- In Notepad++ go to Plugins → Plugins Admin, install ComparePlus (or the older Compare plugin) and XML Tools.
- Open both files and run Plugins → XML Tools → Pretty print on each one.
- With the first file active, choose Plugins → ComparePlus → Compare.
Good for: Windows users who already work in Notepad++ and want to edit while comparing.
Method 3: VS Code
Select both files in the Explorer, right-click and choose Compare Selected, or run code --diff old.xml new.xml from a terminal. Format both files first with Format Document (this needs an XML extension installed) so indentation matches.
Method 4: Command line with xmllint
On Linux and macOS, xmllint (from libxml2) can normalize both files before a normal diff:
xmllint --format old.xml > old.fmt.xml
xmllint --format new.xml > new.fmt.xml
diff -u old.fmt.xml new.fmt.xml
To also ignore attribute order, canonicalize first (Canonical XML puts attributes in a fixed order), then format:
xmllint --c14n old.xml | xmllint --format - > old.norm.xml
xmllint --c14n new.xml | xmllint --format - > new.norm.xml
diff -u old.norm.xml new.norm.xml
Good for: scripts, CI checks and very large files.
Which method should you use?
- Quick check of two snippets or API responses: online XML Compare.
- Editing config files on Windows: Notepad++ with ComparePlus.
- Files in a code project: VS Code or
git diff. - Automation: xmllint + diff.
Tips for a clean XML comparison
- Compare like with like: if one file is a sitemap.xml and the other a feed, the diff will not be meaningful.
- Remove volatile values first (timestamps, generated IDs, signatures) if you only care about structure.
- Working with JSON or YAML configs instead? Use JSON Compare or YAML Compare; for HTML markup use HTML Compare.
Frequently asked questions
Why does my XML diff show every line as changed?
Usually one file is minified (all on one line) or uses different indentation. Format both files with the same indentation first; the online XML Compare does this automatically when Format XML is on.
Does attribute order matter when comparing XML?
No. In XML, attribute order has no meaning, so <item id="1" type="a"/> and <item type="a" id="1"/> are equivalent. Sort attributes (or canonicalize) before comparing so this is not reported as a change.
Can I compare XML files in Notepad++?
Yes. Install the ComparePlus (or Compare) plugin from Plugins → Plugins Admin, open both files and run Plugins → ComparePlus → Compare. Pretty-print both files first with the XML Tools plugin for a cleaner result.
Is it safe to compare XML online?
It depends on the tool. CompareOnline4U formats and compares the XML in your browser, so the files are not uploaded to a server.