XML Formatter
About the XML Formatter
This XML formatter pretty-prints, minifies and checks XML for well-formedness, keeping comments, CDATA sections, processing instructions and attributes intact. Paste a SOAP response, a sitemap or a Maven pom.xml and get consistently indented XML you can actually read.
What makes XML well-formed?
A well-formed XML document has exactly one root element, every start tag has a matching end tag (or is self-closing like <br/>), elements nest without overlapping, and attribute values are quoted. Tag names are case-sensitive, so <Item> cannot be closed with </item>.
Special characters in text must be escaped: & as & and < as <. A bare ampersand, such as Tom & Jerry, is one of the most common reasons an XML file fails to parse.
Well-formed vs valid XML
Well-formed means the syntax is correct. Valid means the document also follows a schema, such as a DTD or an XSD, that defines which elements and attributes are allowed.
This tool checks well-formedness using the browser's XML parser. It does not validate against a DTD or XSD, so a document can pass here and still be rejected by a system that enforces a schema.
How formatting handles whitespace
Elements that contain only child elements are placed on separate lines and indented. Elements that contain text, or text mixed with tags, are kept on one line, because whitespace inside text content can be meaningful and adding line breaks would change the data.
Empty elements are written in self-closing form, so <tag></tag> becomes <tag/>. Both forms mean the same thing to any XML parser. The <?xml ...?> declaration and any DOCTYPE, including an internal subset, are kept at the top.
How to Use the XML Formatter
- Choose an Indent Type of Spaces or Tabs and an Indent Size of 2, 4 or 8 spaces, then paste your XML into the Input XML box or click Load example.
- Click Format XML (or press Ctrl/Cmd + Enter) to pretty-print it, Minify to strip indentation, or Validate to only check the syntax. If the XML is invalid, the error under the buttons includes the line and column the parser reported.
- Click the Copy icon to put the result on your clipboard or the Download icon to save it as formatted.xml. The reset button next to Load example clears everything.
XML Formatter Examples
Format a one-line catalog
Indent Type Spaces, Indent Size 2, click Format XML. The declaration is kept and each element nests on its own line.
Input:
<?xml version="1.0" encoding="UTF-8"?><catalog><book id="1"><title>Dune</title><price currency="USD">9.99</price></book><book id="2"><title>Emma</title><price currency="GBP">7.50</price></book></catalog>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="1">
<title>Dune</title>
<price currency="USD">9.99</price>
</book>
<book id="2">
<title>Emma</title>
<price currency="GBP">7.50</price>
</book>
</catalog>
Minify an indented feed
Click Minify. Indentation between elements is removed and the comment is kept.
Input:
<feed>
<entry>
<title>Hello</title>
<!-- draft -->
<link href="/a"/>
</entry>
</feed>
Output:
<feed><entry><title>Hello</title><!-- draft --><link href="/a"/></entry></feed>
Mixed content and empty elements
Elements with text and inline tags stay on one line so no whitespace is added to the value. <tag></tag> becomes <tag/>.
Input:
<note><to>Tom & Jerry</to><body>Call <b>now</b>, it is <i>urgent</i></body><tag></tag></note>
Output:
<note> <to>Tom & Jerry</to> <body>Call <b>now</b>, it is <i>urgent</i></body> <tag/> </note>
Common Errors
- Invalid XML at an unescaped ampersand, for example <a>Tom & Jerry</a>.
- Write & instead of & in text and attribute values. < must be <.
- Invalid XML: unexpected closing tag, for example <Item>x</item>.
- Tag names are case-sensitive. The closing tag must match the opening tag exactly, and tags must close in the reverse order they opened.
- Invalid XML for <a/><b/>, with an unexpected token after the first element.
- An XML document has exactly one root element. Wrap the elements in a single parent such as <root>.
- Invalid XML for an unquoted attribute such as <a id=1/>.
- Quote every attribute value: <a id="1"/>.
- Invalid XML when you paste HTML such as <p>line<br>next</p>.
- XML needs every tag closed. Write <br/>, or use the code beautifier for regular HTML.
Frequently Asked Questions
- Is my XML uploaded anywhere?
- No. Parsing and formatting use the browser's built-in DOMParser, so your XML stays on your device.
- Does it validate against an XSD or DTD?
- No. It checks that the XML is well-formed: correct tags, nesting, quoting and escaping. Schema validation needs the XSD or DTD and a dedicated validator.
- Why do I get an error about an entity or "&"?
- A raw & in text or an attribute must be written as &. Only the five predefined entities (&, <, >, ", ') work without a DTD.
- Will formatting change my data?
- Only whitespace between elements changes. Text content, attribute values, comments and CDATA are preserved. Empty elements are written in the equivalent self-closing form, and an element that holds only whitespace becomes self-closing too, so <a> </a> becomes <a/>.
- Can I format HTML with this tool?
- Only if it is valid XHTML. Regular HTML allows unclosed tags like <br> and <li>, which an XML parser rejects. Use the code beautifier for HTML instead.