By Mian Ali Khalid · Published · How we build and test tools
How to use the xml diff
- Paste your original XML into the Original XML field.
- Paste the modified XML into the Modified XML field.
- The xml diff results appear automatically as you type or paste.
- If either document has a syntax error, the first error is reported with its line and column number.
How the xml diff works
The comparison uses a hand-written linear tokenizer rather than the browser DOMParser. It scans each character exactly once, recognizing elements, attributes, text nodes, CDATA sections, comments, processing instructions, and the five predefined entities (&, <, >, ", ') along with numeric character references like A and A.
After both inputs are parsed into trees, a recursive walk compares them node by node. For example, if the left tree has <price>10</price> and the right has <price>12</price>, the algorithm visits the price element, finds matching names and attributes, then compares the child text nodes and reports the difference at that path.
Inputs are limited to 2 MB each and 200 levels of nesting, and the comparison stops after the first 1,000 differences, so the page stays responsive on unusual input. Whitespace-only text between elements (indentation and line breaks) is ignored, so a pretty-printed file matches its compact version. Adjacent text and CDATA are merged before comparing.
Supported XML constructs
The tokenizer handles the core XML building blocks needed for most data-oriented documents:
| Construct | Example | Notes |
|---|---|---|
| Elements | <item/>, <item></item> | Self-closing and paired tags |
| Attributes | id="1", name='test' | Double or single quotes |
| Text | Hello world | Entities resolved before comparison |
| CDATA | <![CDATA[data]]> | Treated as raw text content |
| Comments | <!-- note --> | Included in the tree and compared |
| Processing instructions | <?xml version="1.0"?> | Compared by target and content |
| Entities | & A A | Five built-in plus numeric references |
DOCTYPE declarations and external entity references are not supported and are reported as errors. If your document contains them, remove them before running the xml diff. Each document must have exactly one root element, attribute names cannot repeat on one element, and only the five predefined entities and numeric references are accepted.
Error reporting with line and column
When the tokenizer encounters malformed markup, it halts immediately and returns the position of the first error. Line numbers start at 1 and columns start at 1. For instance, given this input:
<root>
<item>
</itm>
</root>
The mismatched closing tag </itm> on line 3 would be flagged because the parser expects </item>. This precise location makes it straightforward to fix issues before comparing again. If you need deeper analysis, a dedicated xml validator can check additional constraints.
Practical use cases
Comparing XML arises frequently when working with configuration files, API responses, and data exports. Common scenarios include:
- Reviewing changes between two versions of a web.config or pom.xml file.
- Verifying that an API response matches the expected structure after a deployment.
- Finding discrepancies between exported database records in XML format.
- Checking that a transformation script produced the correct output.
Because the comparison resolves entities before matching text, & and & are treated as identical. This avoids false positives that a plain string comparison would produce. For other formats, tools like json diff or yaml diff apply the same approach to JSON and YAML respectively.
Limitations and edge cases
This xml diff tool intentionally omits several XML features to keep the parser fast and safe. DOCTYPE declarations, external DTDs, and external entity references are rejected. Namespace prefixes are compared literally: <ns:item> and <item xmlns="ns"> are treated as different elements because full namespace resolution requires schema context beyond a lightweight tokenizer.
Whitespace inside text that also contains other characters is preserved exactly as written. If one document has <a> hello </a> and another has <a>hello</a>, the extra spaces are reported as a difference. If you need normalized whitespace, run the documents through an xml formatter first to standardize indentation and spacing.
Input size is capped at 2 MB per document and nesting at 200 levels, so the comparison finishes promptly even on adversarial input. Child elements are compared by position, so reordering siblings is reported as differences, while attribute order does not matter. When several siblings share a name, paths carry an index such as /list/item[2].
Worked examples
| Example | Input | Result |
|---|---|---|
| Attribute change | Original XML: <root><item id="1"/></root>; Modified XML: <root><item id="2"/></root> | Differences found: 1 |
| Added element | Original XML: <catalog><book>A</book></catalog>; Modified XML: <catalog><book>A</book><book>B</book></catalog> | Differences found: 1 |
| Text and CDATA | Original XML: <msg><![CDATA[old]]></msg>; Modified XML: <msg><![CDATA[new]]></msg> | Differences found: 1 |
FAQ
Does this tool upload my XML to a server?
No. The xml diff runs entirely in your browser using JavaScript. Your documents never leave your device.
What happens if my XML is not well-formed?
The tokenizer stops at the first error and reports the exact line and column where parsing failed, so you can fix the issue before comparing.
Are DOCTYPE declarations supported?
No. DOCTYPE, external DTDs, and external entity references are not supported. Remove them before pasting your XML into the tool.
Does the comparison ignore whitespace differences?
Whitespace-only text between elements (indentation and line breaks) is ignored. Whitespace inside text that has other characters is compared exactly. Use an xml formatter to normalize text spacing if needed.
How are XML entities handled during comparison?
The five predefined entities (amp, lt, gt, quot, apos) and numeric character references are resolved to their actual characters before comparison, so equivalent representations match correctly.
Is there a maximum file size?
Each input is limited to 2 MB and 200 levels of nesting. At most the first 1,000 differences are listed.
Can I compare XML namespaces semantically?
Namespace prefixes are compared as literal strings. Full semantic namespace resolution is not performed.