Skip to content

XML Formatter & Validator

Paste XML to indent it with 2 spaces, 4 spaces or tabs, squeeze it onto one line, or find out why it won't parse. Well-formed XML comes back with its element count, nesting depth and size; malformed XML comes back with the parser's message. Free, no sign-up, and the XML never leaves your browser.

Mode

How to Use the XML Formatter

  1. 1. Choose a mode. Format pretty-prints; Minify removes the whitespace between elements.
  2. 2. Pick an indent. For Format, choose 2 spaces, 4 spaces or a tab.
  3. 3. Paste your XML and press the button. The result appears below with three counts: elements, maximum depth (the root is level 1) and output size in characters.
  4. 4. Fix errors if shown. If the XML is not well-formed, nothing is output and your browser's parser reports what it found. Correct the XML and press the button again.

Well-Formed Versus Valid

Two different things get called "validating" XML. A document is well-formed if it follows the syntax rules of the W3C's XML 1.0 Recommendation; it is valid if it also obeys a schema such as a DTD or an XSD. This tool checks well-formedness only, using the XML parser built into your browser. It does not read DTDs or schemas, so a document that is well-formed but wrong for your application — a missing required element, say — passes. The rules it enforces:

RuleBreaks when
One root elementTwo top-level elements, or text before the root
Elements nest properly<a><b></a></b>
Every start tag has an end tag<a><b></a>, or <br> instead of <br/>
Names are case-sensitive<Title>…</title>
Attribute values are quoted<a id=1/>
No repeated attribute on an element<a id="1" id="2"/>
< and & are escaped in text<a>5 < 6 & 7</a> should be 5 &lt; 6 &amp; 7

XML defines exactly five predefined entities — &lt;, &gt;, &amp;, &apos; and &quot; — so an HTML entity such as &nbsp; is an error unless a DTD declares it. For an unclosed tag the message, which is the browser's wording and varies by browser, names the mismatched pair: in Chrome, <a><b></a> reports an error on line 1 at column 11 with "Opening and ending tag mismatch: b line 1 and a".

What the Formatter Changes

The tool parses your XML into a tree and writes the tree back out, so the output is equivalent XML in a tidier layout rather than a copy of your text. Expect these differences:

  • Declaration and DOCTYPE kept. A leading <?xml …?> line and a <!DOCTYPE …> declaration, internal subset included, are written back as you typed them.
  • Attributes keep their order but are rewritten with double quotes.
  • Entities are resolved. &apos; and character references such as &#169; come back as the characters themselves; only &, < and > in text, and &quot; in attributes, are escaped again.
  • Empty elements self-close. <a></a> becomes <a/> when it has no content, though one holding only a space becomes <a></a>.
  • Comments, CDATA sections and processing instructions are kept.

Worked Example

This one-line document has a declaration, a single-quoted attribute, a comment and an empty element:

Input

<?xml version="1.0" encoding="UTF-8"?><library><book id='1'><title>APA Manual</title><year>2020</year><!-- note --></book><book id="2"/></library>

Format, 2 spaces

Nine lines and 172 characters: the declaration, then library at the margin, each book two spaces in, and the title, year and comment four spaces in. The first book's id is now written id="1".

Minify

One line of 146 characters.

Statistics

5 elements and a maximum depth of 3 (library, book, title).

Minify keeps the declaration and puts the whole tree on one line, so the same document shrinks from 172 characters to 146. The output-size card shows these character counts.

Which Tool Do You Need?

This page is for XML. For HTML, which tolerates unclosed tags and keeps inline spacing, use the HTML Formatter. To turn XML into JSON data, the planned XML to JSON Converter shows how attributes and repeated elements map; for JSON itself, use the JSON Formatter.

Frequently Asked Questions

What does an XML formatter do?
It re-indents XML so that nesting is visible, usually with one element per line. This one also checks that the XML is well-formed, because it has to parse the document before it can write it back out in a new layout.
How does XML validation work here?
Your browser's built-in XML parser reads the text. If the XML breaks a syntax rule, the parser's error is shown and no output is produced. It checks well-formedness only — it does not validate against a DTD or XSD schema.
Are comments, CDATA sections and attributes preserved?
Yes: comments and CDATA sections are written back as they were, and attributes keep their order, though quotes are normalised to double quotes. A DOCTYPE declaration is written back as you typed it.
What does minify do?
It removes the whitespace and line breaks between elements to give the shortest equivalent text, keeping the XML declaration if there is one. Text inside elements such as <title> keeps its spaces, and so does text beside child elements, as in a paragraph with a bold word.
Why does formatting change my text spacing?
Only when formatting mixed content — text sitting next to child elements, like a paragraph with a bold word. Format trims that text and puts each piece on its own line, which changes the whitespace; Minify keeps it. Data-style XML, where elements hold either text or other elements, is unaffected.
Why does my XML with &nbsp; fail?
XML has only five predefined entities, and &nbsp; is not one of them. Replace it with the numeric reference &#160;, or declare the entity in a DTD.
Is my XML uploaded to a server?
No. The parsing and formatting happen in your browser and nothing you paste is sent anywhere.

Related Tools

More in Data, Markup & Code

See all Data, Markup & Code