XML Formatter and Validator: Fix Well-Formedness Errors
Short answer: paste XML into the XML Formatter & Validator to pretty-print it with your choice of indentation, minify it to one line, and find well-formedness errors with line and column numbers. It uses your browser's XML parser, so if the document is well-formed it is rewritten cleanly, and if not you see where the first error is.
Format and minify
Formatting <a><b>text</b></a> with a 2-space indent gives:
<a>
<b>text</b>
</a>
Minify does the reverse: <a>\n <b id="1">x</b>\n</a> becomes <a><b id="1">x</b></a>.
The four most common errors
The messages come from your browser's XML parser, so the wording can differ a little between browsers. The line and column point to the first problem.
| Mistake | Example | Message | Fix |
|---|---|---|---|
| Mismatched tags | <a><b></a> |
Opening and ending tag mismatch: b line 1 and a | Close b before closing a |
| Unescaped ampersand | <a>1 & 2</a> |
xmlParseEntityRef: no name | Write &, and < for < |
| Two root elements | <a/><b/> |
Extra content at the end of the document | Wrap both in one parent |
| Unquoted attribute | <a x=1/> |
AttValue: " or ' expected | Write <a x="1"/> |
For escaping characters in HTML as well as XML, see HTML entities.
What is preserved
Attributes, namespaces, comments, CDATA sections, processing instructions and the XML declaration at the top are kept. Formatting trims whitespace around text and drops whitespace-only text between elements. That is safe for most data files, but if your document depends on exact whitespace inside mixed content, compare before and after with the Diff Checker.
Why a DOCTYPE is rejected
DOCTYPE declarations with an internal subset and ENTITY declarations are rejected on purpose, because they are a common source of security problems such as entity expansion attacks.
Well-formed is not valid
The tool checks that the syntax is correct. It does not validate against an XSD or a DTD. XML is also stricter than HTML: every tag must be closed, attribute values must be quoted and there is exactly one root element, so many HTML pages are not valid XML.
How to use the XML Formatter
- Open the XML Formatter & Validator.
- Paste your XML and choose Format or Minify, and the indent.
- If there is an error, go to the reported line and column, fix it and run again.
- Copy or download the result.
For the same kind of check on other formats, see JSON syntax errors and the YAML syntax errors guide.
Limits to know
- No schema (XSD or DTD) validation.
- Whitespace-sensitive mixed content may change.
- Wording of errors depends on your browser.
Frequently asked questions
Does it validate against an XSD? No, only well-formedness.
Why is my DOCTYPE rejected? Internal subsets and entities are blocked for security.
How do I escape & in XML?
Write &.
Can an XML document have two root elements? No, exactly one.
Is my XML uploaded? No.