How to Format and Validate XML
XML is still everywhere — config files, SOAP APIs, RSS feeds, office document formats. When a chunk of XML won't parse, it's almost always a well-formedness rule being broken. Here's what to check.
Well-formed vs valid
Two different things:
- Well-formed — the XML follows the basic syntax rules below. A parser can read it.
- Valid — the XML also conforms to a schema (DTD or XSD) that defines which elements and attributes are allowed.
Most everyday errors are about being well-formed; schema validation is a separate, stricter step.
The rules parsers enforce
- One root element wraps the whole document.
- Every tag closes —
<item>…</item>, or self-close with<item/>. - Tags nest, never overlap —
<a><b></b></a>, not<a><b></a></b>. - Attribute values are quoted —
id="1", neverid=1. - Case matters —
<Item>and<item>are different elements, and a closing tag must match its opening tag exactly. - Special characters are escaped —
<,>,&for<,>,&.
Why formatting helps
Minified or hand-edited XML hides its structure. Indenting it so each nested element lines up makes a missing or mismatched tag jump out immediately — which is usually how you find the one line breaking the parse.
Related
Working with data formats more broadly? See JSON vs XML vs YAML vs CSV for when XML is the right choice.
Try it
Paste XML to format and validate it — errors are reported clearly so you can fix them fast, all in your browser with nothing uploaded.