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
- Paste XML, or open or drop a file such as
.xml,.svg,.rssor.xsd. - If Problems lists errors, select one to jump to its place in the input, and fix it.
- Choose Mode: Format for reading, Minify for sending or storing. Pick the Indent for Format.
- Copy the result, or download it; a downloaded file keeps the extension of the file you opened, so an SVG stays an
.svg.
Examples
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>
Input
<doc>
<p>Use <code>x < y</code> here.</p>
<script><![CDATA[if (a < b) go();]]></script>
</doc>
Output
<doc><p>Use <code>x < y</code> here.</p><script><![CDATA[if (a < b) go();]]></script></doc>
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, &, <, >, ' and ", are allowed; a from HTML is an error, and   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
| Option | Values | Default | When to change it |
|---|---|---|---|
| Mode | Format, Minify | Format | Minify 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. |
| Indent | 2 spaces, 4 spaces, Tab | 2 spaces | Match your project's style; Android projects usually use 4 spaces. Minify does not indent. |
Common errors and fixes
| Code | What it means | How to fix it |
|---|---|---|
XML_Error | A 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_Error | An 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_Error | A & starts no reference, as in Tom & Jerry, or names an entity that is not defined, like without a DOCTYPE. | Write & as &, and use numeric references such as   for other characters. |
XML_Error | Two 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_Error | An 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 <. |
XML_Error | The same attribute appears twice on one element. | Keep one of them; the related place shows the first. |
XML_Error | Something 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_Error | The 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  can write it. |
XML_Error | A comment, CDATA section, processing instruction, tag or attribute value never ends. | Add its closing -->, ]]>, ?>, > or quote. |
XML_Error | A 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 <. |
XML_Error | The input has no element at all, only a declaration, comments or text. | Put the content in a root element. |
XML_Error | Another 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_Note | The 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_Note | The 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. |