Skip to content

XML Formatter and Validator

Paste or open XML to indent it or minify it, and to check that it is well-formed. Each error comes with its line and column, while comments, CDATA, mixed text and attribute quotes stay exactly as written. Entities are never expanded.

Runs locally in your browser

Input

2 lines · 459 characters
<?xml version="1.0" encoding="UTF-8"?><library xmlns:media="urn:example:media"><!-- Exported from the catalog --><book id="b1" lang='en'><title>Field Notes on Rivers</title><author>Mira Kade</author><price currency="EUR">18.50</price><summary>A short guide to <em>reading</em> water &amp; weather.</summary></book><book id="b2" lang='tr'><title>Kıyıdaki Ev</title><media:cover href="covers/b2.svg"/><note><![CDATA[Ships in <2 days>]]></note></book></library>

Output

Formatted XML appears here

What the XML formatter does

The formatter checks that your XML is well-formed by the rules of XML 1.0 (opens in a new tab), then lays it out with one element per line, or minifies it into as few characters as the content allows. Every problem it finds is listed with its line and column, not just the first one. It changes only the whitespace between elements: text, attribute values, comments and CDATA sections keep their content, so a SOAP message or an SVG still means the same thing afterwards.

How to format XML

  1. Paste XML, or open or drop a file such as .xml, .svg, .rss or .xsd.
  2. If Problems lists errors, select one to jump to its place in the input, and fix it.
  3. Choose Mode: Format for reading, Minify for sending or storing. Pick the Indent for Format.
  4. Copy the result, or download it; a downloaded file keeps the extension of the file you opened, so an SVG stays an .svg.

Examples

A one-line order becomes indented

Input

<order id="7"><item sku="A1">2</item><item sku="B2">1</item><total>3</total></order>

Output

<order id="7">
  <item sku="A1">2</item>
  <item sku="B2">1</item>
  <total>3</total>
</order>
Minifying keeps the spaces inside text and CDATA

Input

<doc>
  <p>Use <code>x &lt; y</code>  here.</p>
  <script><![CDATA[if (a < b) go();]]></script>
</doc>

Output

<doc><p>Use <code>x &lt; y</code>  here.</p><script><![CDATA[if (a < b) go();]]></script></doc>
A closing tag in the wrong order

Input

<config><port>8080</config>

Problem reported

Line 1, column 19: </config> does not close <port>, which is open since line 1, column 9. Expected </port> here. (XML_MISMATCHED_TAG)

Well-formed is not the same as valid

Well-formed XML follows the grammar every XML document shares: one root element, every start tag closed by a matching end tag, attribute values in quotes, each attribute once per element, and & and < escaped in text. A parser refuses a document that breaks any of these rules, which is what this tool checks.

Valid XML is well-formed and also follows a schema, an XSD or a DTD, that says which elements may appear where. A <price> inside an <author> can be well-formed and still invalid. The formatter does not check schemas or namespaces: a namespace prefix that is never declared, like ord: without its xmlns:ord, passes here and fails in a namespace-aware parser.

Why some elements keep their own line breaks

XML has no rule for which whitespace matters, so the formatter changes it only where it cannot be part of the data. An element whose children are only other elements, comments and processing instructions gets one child per line. Every other element keeps its content exactly as written:

  • a text-only element, like <title>Release notes</title>, stays on one line, and its text is never trimmed;
  • mixed content, where text and elements sit side by side, as in <p>Hello <b>world</b>!</p>, keeps every space and line break, because indenting it would add words' worth of spaces to the text;
  • an element with xml:space="preserve", the standard way to say that whitespace matters, keeps everything inside it, child elements included.

Minify follows the same rule from the other side: it removes only the whitespace between elements of element-only content, and never touches text.

What stays exactly as written

The XML declaration, processing instructions such as <?xml-stylesheet ...?>, comments, the DOCTYPE with its internal subset, and CDATA sections are copied unchanged. Attributes keep their order and their quotes, single or double. An empty element keeps its form: <br/>, <br /> and <br></br> each come out as they went in. When attributes stand on lines of their own, as in Android layout files, each one stays on its own line, indented one level under its tag.

Line breaks come out as LF. XML parsers turn CRLF into LF when they read a document anyway, so the content is the same.

Entities are never expanded

An entity reference like &e; is a placeholder that a parser replaces with text defined in the DOCTYPE. Expanding them is what makes two classic attacks possible. In a XXE attack (opens in a new tab), an entity defined as SYSTEM "file:///etc/passwd" makes the parser read a local file or a URL. In a "billion laughs" attack, entities defined by ten other entities, nine levels deep, grow a few hundred bytes into gigabytes.

This tool never expands entities and never reads anything outside the text you give it. The DOCTYPE is kept as text, a note says so, and every reference stays in the output as it was written. Without a DOCTYPE, only the five built-in entities, &amp;, &lt;, &gt;, &apos; and &quot;, are allowed; a &nbsp; from HTML is an error, and &#160; is the reference that works everywhere.

Encoding declarations and Latin-1 files

An XML file can name its encoding in the declaration, like <?xml version="1.0" encoding="ISO-8859-1"?>. When you open such a file, the formatter reads its bytes in that encoding, so accented letters come out right. Files with a UTF-8 or UTF-16 byte order mark are read by the mark. Pasted text is already text and needs no encoding.

The result is always UTF-8. If the declaration names another encoding, a note says so, and the declaration is left as it is: change it to encoding="UTF-8" before you save the result, or programs that trust the declaration will misread the letters.

Options

Options
OptionValuesDefaultWhen to change it
ModeFormat, MinifyFormatMinify for a message body or a file that only programs read; it keeps comments, which you can remove by hand if they are not needed.
Indent2 spaces, 4 spaces, Tab2 spacesMatch your project's style; Android projects usually use 4 spaces. Minify does not indent.

Common errors and fixes

Common errors and how to fix them
CodeWhat it meansHow to fix it
XML_MISMATCHED_TAGErrorA closing tag names a different element than the one that is open, often because a tag in between was never closed.Close the inner element first; the related place shows where it opened.
XML_UNCLOSED_TAGErrorAn element is still open when the document ends.Add its closing tag where its content ends, or write it as an empty element, like <item/>.
XML_INVALID_ENTITYErrorA & starts no reference, as in Tom & Jerry, or names an entity that is not defined, like &nbsp; without a DOCTYPE.Write & as &amp;, and use numeric references such as &#160; for other characters.
XML_MULTIPLE_ROOTSErrorTwo elements stand at the top level; an XML document has exactly one root.Wrap them in one element, or format each document on its own.
XML_INVALID_ATTRIBUTEErrorAn attribute has no quotes, no = or no value, as HTML allows, or its value contains <.Write every attribute as name="value", and < inside a value as &lt;.
XML_DUPLICATE_ATTRIBUTEErrorThe same attribute appears twice on one element.Keep one of them; the related place shows the first.
XML_DECLARATION_POSITIONErrorSomething comes before <?xml ...?>, even an empty line or a space, or the DOCTYPE comes after the root element.Put the declaration at the very start, and the DOCTYPE before the root element.
XML_INVALID_CHARErrorThe text holds a control character, often pasted from a terminal or a binary file, that XML 1.0 does not allow at all.Remove the character; not even a reference like &#1; can write it.
XML_UNTERMINATEDErrorA comment, CDATA section, processing instruction, tag or attribute value never ends.Add its closing -->, ]]>, ?>, > or quote.
XML_INVALID_TAG_NAMEErrorA tag name starts with a digit, a space or a hyphen, or a < in text is read as a tag.Start names with a letter, _ or :, and escape a < in text as &lt;.
XML_NO_ROOTErrorThe input has no element at all, only a declaration, comments or text.Put the content in a root element.
XML_SYNTAXErrorAnother rule is broken: text outside the root element, -- inside a comment, or ]]> in text.Follow the message and its hint at the marked place.
XML_DTD_NOT_PROCESSEDNoteThe document has a DOCTYPE, which the formatter keeps as text without reading its definitions.Nothing to fix: references stay as written, which is the safe choice.
XML_ENCODING_DECLARATIONNoteThe declaration names an encoding other than UTF-8, but the result is UTF-8.Change it to encoding="UTF-8" before you save the result.

Privacy

XML Formatter & Validator runs entirely in your browser. What you type, paste or open is processed on your device: it is not sent to Toolzami, and Toolzami does not store it. Only the options you choose are remembered, in this browser.

Frequently asked questions

Can I format HTML with this?

Only XHTML, the strict form of HTML that is also XML. Ordinary HTML allows tags that never close, such as <br> or <li>, and attributes without values, so the formatter reports them as errors.

Can I convert XML to JSON here?

Not yet; this tool formats and checks XML. XML has attributes, mixed text and repeated elements that JSON has no direct form for, so a conversion needs choices that a dedicated tool will offer.

Last updated