What this XML formatter does
An xml formatter takes XML that is hard to read, often a single long line from an API, a build tool or an export, and lays it out with one element per line and an indent for each level. This one also works the other way: Minify strips the layout whitespace so the file is as small as it can be without changing its meaning. A third mode, Validate only, checks the XML and tells you what is in it without writing anything new.
Here is a short RSS feed pasted as one line, and what Beautify gives back with the default 2-space indent:
<rss version="2.0"><channel><title>Shop news</title><item><title>Spring sale</title><description>Up to <b>30%</b> off, ends Friday.</description></item></channel></rss><rss version="2.0">
<channel>
<title>Shop news</title>
<item>
<title>Spring sale</title>
<description>Up to <b>30%</b> off, ends Friday.</description>
</item>
</channel>
</rss>Notice that the description element stayed on one line with its <b> tag inside. That is on purpose, and the next sections explain why.
What formatting changes and what it never changes
Formatting only touches whitespace that has no meaning: the spaces and line breaks between tags in an element that holds only other elements, and the spacing inside a tag between attributes. Everything else is copied as written.
- Text inside elements is kept character for character, including leading and trailing spaces.
- Attribute values and their quote style stay as they are.
a='x'is not rewritten asa="x". - Entity and character references such as
&and are not expanded or re-encoded. - CDATA sections, processing instructions and the DOCTYPE, including its internal subset, are copied exactly.
- Namespace prefixes and
xmlnsdeclarations are never renamed, moved or removed. - Element order and attribute order stay the same, unless you turn on Sort attributes.
Two things do change across the whole file. Line endings are written as LF or CRLF, as you choose, which is also what every XML parser does when it reads a file. And the extra spaces inside a tag are tidied, so <server host="db" /> becomes <server host="db"/>.
Whitespace pitfalls in mixed content
Mixed content means an element holds text and child elements side by side, as in <p>Price: <b>9</b> <i>EUR</i></p>. The single space between </b> and <i> is part of the text. A formatter that puts <b> and <i> on their own indented lines adds new spaces and line breaks to that text, so the sentence reads differently and a signed document no longer matches its signature.
This tool uses a simple rule. If an element contains any text that is not only whitespace, or any CDATA section, the element and everything inside it are written on one line exactly as they were. The same applies to an element whose only content is whitespace, such as <a> </a>, and to every element under xml:space="preserve" until an xml:space="default" switches it back. Only elements that hold nothing but other elements, comments and processing instructions get new line breaks.
Each option in plain terms
- Mode: Beautify, Minify or Validate only. The other options turn off when they do not apply.
- Indent: 2 spaces, 4 spaces or a tab per level.
- Attributes: keep every tag on one line, or put each attribute on its own line once a tag has more than 1, 2, 3 or 5 of them.
- Empty elements: keep them as written, write all of them as
<a/>, or write all of them as<a></a>. Both forms mean the same thing to an XML parser. - XML declaration: keep the
<?xml ...?>line, remove it, or add<?xml version="1.0" encoding="UTF-8"?>when it is missing. - Line endings: the same as the input, LF, or CRLF.
- Sort attributes: orders attributes from A to Z, with
xmlnsdeclarations first. Useful before comparing two files. - Keep comments: turn off to drop every comment. CDATA and processing instructions are always kept.
- Final newline: ends beautified output with a line break, which most editors and Git expect.
With Attributes set to Wrap if more than 2, a tag with four attributes is split while a text element next to it stays on one line:
<order>
<note> Leave at door </note>
<line
sku="A-1"
qty="2"
price="9.50"
currency="EUR"/>
</order>Well-formed XML rules and common errors
Well-formed means the XML follows the grammar of the XML 1.0 specification, so any parser can read it. It is a lower bar than valid, which means it also matches a schema or DTD. The checker in this tool is written for this page and does not depend on the browser, so every mode reports the same errors. It stops at the first problem and gives the line, the column and how to fix it. These are the rules people break most often, with the message the tool prints:
- Every opening tag needs a matching closing tag with the same name and case.
<order>...</ordr>gives: Line 3, column 1: The closing tag </ordr> does not match <order> opened at line 1, column 1. - A bare ampersand is not text.
<total>3 & 4</total>gives: Line 1, column 10: A bare & is not allowed. Write & for an ampersand. - An attribute can appear once per element.
<item sku="A-1" sku="A-2">gives: Line 1, column 17: The attribute sku appears twice in <item>. - There is exactly one root element.
<a/><b/>gives: Line 1, column 5: A second root element <b> was found. - Only five entities are built in:
&<>"'. comes from HTML, so the tool suggests instead. - Names cannot start with a digit, a hyphen or a period.
<1st>gives: Line 1, column 2: Element names cannot start with "1". - Attribute values need quotes, comments cannot contain
--, the<?xml ...?>declaration must be the very first thing in the file, an entity cannot refer to itself directly or through other entities, and text cannot contain the characters ]]> in a row or control characters.
Spaces or blank lines before the <?xml ...?> declaration are an error in Validate only. Beautify and Minify remove them and say so in the summary. The checker also warns about things that are well-formed but likely to cause trouble later: a DOCTYPE that names a different root element, entities that may come from an external DTD, a byte order mark, or an encoding other than UTF-8 in the declaration.
Minify or beautify for sitemaps, RSS, SOAP and config files
Beautify is for people: reading an API response, reviewing a change in a pull request, or finding the one wrong value in a long config. Minify is for machines: it trims bytes from files that are sent often and rarely read by hand.
- Sitemaps: search engines read either form. Minify a large sitemap to save bytes, beautify it when you want to check the URLs and dates.
- RSS and Atom feeds: minify the published feed, beautify a copy to debug it. Descriptions with HTML inside keep their exact text either way.
- SOAP messages: beautify a request or response from a log to see its structure. If the message carries an XML signature, do not reformat the signed part, because any change to whitespace in signed content can break the signature.
- Config files such as pom.xml, web.config, .csproj or Android layouts: beautify with the indent your team uses, and leave comments on, since they often explain settings.
Here is the result of minifying an indented feed. Only the whitespace between tags is gone:
<rss version="2.0"><channel><title>Shop news</title></channel></rss>Namespaces, DOCTYPE and entities
Namespaces let one document mix vocabularies, such as SOAP envelopes around your own elements. The formatter treats a prefixed name like soap:Body as a plain name and leaves every xmlns attribute where it is. The summary lists each declaration it found with its URI. The tool checks XML 1.0 well-formedness, not the separate Namespaces in XML rules, so namespace problems are warnings, never errors: a prefix such as x:item used without a matching xmlns:x declaration in scope still counts as well-formed, and the summary lists the prefix so you can add the declaration. Namespace-aware parsers will reject such a file.
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<m:Ping xmlns:m="urn:example:ping"/>
</soap:Body>
</soap:Envelope>A DOCTYPE is copied as text, internal subset and all. Entities declared there, such as <!ENTITY company "Example Ltd">, make &company; valid in the document, but the tool never expands them and never fetches an external DTD. That is also why a file built to blow up when its entities are expanded, often called a billion laughs file, does no harm here: the references stay as short text.
Limits worth knowing
- It checks well-formedness, not validity. It does not test the XML against an XSD schema or a DTD.
- It reports the first error only. Fix it and the next one, if any, appears.
- Input is limited to 2,000,000 characters and to 100,000 levels of nesting. Beautified output stops at 12,000,000 characters, which very deep files can reach because of the indent. Minify handles them.
- Entities from an external DTD cannot be checked, so they are reported as warnings rather than errors.
- The encoding in the declaration is not used to decode anything. The text you paste is already decoded, and Copy and Download give UTF-8.
For other XML jobs, the XML to YAML converter maps elements to YAML keys, YAML to XML goes the other way, the SVG to XML converter turns SVG into an Android vector drawable, and the HTML encoder escapes <, > and & when you need to show XML as text on a web page.
Privacy
The XML is parsed and formatted by code running in your browser. It is not uploaded, and the DOCTYPE never triggers a download of a DTD. The Privacy Policy describes what the site does store.