XML formatter and validator, free, in your browser
An XML formatter re-indents an XML document so that each nested element sits on its own line at a consistent indent, making structure visible. It also checks well-formedness: every start tag must have a matching end tag, attributes must be quoted, and special characters must be escaped. This tool formats, validates and minifies XML locally in your browser, preserving comments, CDATA sections and processing instructions.
Your files never leave your device. Processing happens in your browser. How that works.
How it works
- Paste XML into the input box.
- Click Format to indent, or Minify to compact.
- Fix any well-formedness error shown with its line number, then copy or download.
Runs as JavaScript inside this tab. Turn off your connection after the page loads and it still works.
XML well-formedness rules
An XML document is well-formed when it follows the syntax rules below. It is valid when, in addition, it conforms to a DTD or XML Schema. This tool checks well-formedness; validity needs the schema.
The rules
- One root element. Everything sits inside a single top-level element. Text or a second element at the top level is an error.
- Every start tag has an end tag or is self-closing:
<br/>, not<br>. This is the most common difference from HTML. - Proper nesting.
<a><b></b></a>is fine;<a><b></a></b>is not. - Attributes are quoted with
"or', and an attribute name appears at most once per element:<img src="a.png"/>, never<img src=a.png>. - Case-sensitive names.
<Item>and<item>are different elements. - Names start with a letter or underscore and contain letters, digits, hyphens, underscores, dots and colons (for namespaces). They cannot start with
xmlin any case. - Reserved characters in text and attribute values must be escaped:
| Character | Entity |
|---|---|
< |
< |
& |
& |
> |
> (required only in ]]>) |
" |
" (inside double-quoted attributes) |
' |
' (inside single-quoted attributes) |
Only these five entities are predefined. and other HTML entities are errors unless a DTD declares them; use numeric references such as  .
Other constructs
| Construct | Syntax | Notes |
|---|---|---|
| Declaration | <?xml version="1.0" encoding="UTF-8"?> |
Optional; if present, must be first |
| Comment | <!-- text --> |
Cannot contain -- |
| CDATA | <![CDATA[ raw <text> ]]> |
Content is not parsed; for embedding code or markup |
| Processing instruction | <?target data?> |
For applications, e.g. stylesheets |
| DOCTYPE | <!DOCTYPE root SYSTEM "file.dtd"> |
Declares the DTD |
| Namespace | <svg xmlns="http://www.w3.org/2000/svg"> |
Prefixed: <x:item xmlns:x="…"> |
Whitespace
Whitespace between elements is not significant unless the application says so (xml:space="preserve"). Whitespace inside text content is significant. The formatter normalises the former and keeps the latter, collapsing internal runs only when the “keep short text inline” option is on.
XML vs HTML
HTML is forgiving: unclosed <li>, unquoted attributes and case-insensitive tags all work. XHTML is HTML written under XML rules. To format ordinary HTML, use an HTML formatter or convert it with the HTML to Markdown tool.
Worked examples
Format a one-line RSS feed
Input: <rss version="2.0"><channel><title>TabbyTools</title><item><title>Morse</title></item></channel></rss>
<rss version="2.0">
<channel>
<title>TabbyTools</title>
<item>
<title>Morse</title>
</item>
</channel>
</rss>
Elements with only text stay on one line; elements with children are expanded.
Catch an unquoted attribute
<root><item id=1>text</item></root> → Not well-formed: Attribute value for “id” in
Catch a mismatched tag
<a><b></a></b> → Closing tag does not match (line 1, column 7). The checker reports the first problem; fix it and re-run to find the next.
Minify the formatted feed above back to a single line, removing the whitespace between elements but leaving <title>TabbyTools</title> untouched.
Comments and CDATA are preserved
<script><![CDATA[ if (a < b) alert("ok"); ]]></script>
<!-- keep me -->
Both pass through unchanged, correctly indented.
Frequently asked questions
What is the difference between well-formed and valid XML?
Well-formed means the syntax is correct: balanced tags, quoted attributes, one root element. Valid means it also conforms to a DTD or XML Schema. This tool checks well-formedness; schema validation requires the schema file.
Why does the formatter keep some whitespace?
Whitespace inside elements that contain text is significant and is preserved. Only whitespace between elements is normalised.
Can it format HTML?
Only if the HTML is well-formed XHTML. Regular HTML with unclosed tags such as <br> or <li> will fail the XML check.
Does it handle namespaces and CDATA?
Yes. Namespace prefixes, CDATA sections, comments and processing instructions are passed through unchanged.
XML formatter · last updated · changelog · part of Canned Tuna