Skip to content
PC Moment

Free tool

XML Formatter and Well-Formedness Checker

Paste XML to pretty-print or minify it and find well-formedness errors. Comments, CDATA sections and the XML declaration are kept.

An XML formatter takes a config file, SOAP message or sitemap that arrives as one long line and indents it so you can see the structure. This one also minifies XML and tells you when a document isn’t well-formed, using the XML parser built into your browser. Comments, CDATA sections, processing instructions and the XML declaration all survive the round trip.

How to use the XML formatter

  1. Paste your XML into the text box.
  2. Choose the indent: 2 spaces, 4 spaces or Tab.
  3. Select Format to indent the document, or Minify to remove the whitespace between tags.
  4. If the XML isn’t well-formed, read the error message. It comes from your browser’s parser and includes the line and column when the browser provides them.
  5. Use Copy to copy the result, or Download .xml to save it as a file.

How it works

The tool hands your text to the browser’s DOMParser as application/xml. If the document breaks any of the well-formedness rules in the XML 1.0 specification, the parser doesn’t throw an exception: it returns a document containing a parsererror element, and the tool shows you that message. Because each browser writes its own message, the wording differs between Chrome, Firefox and Safari.

If the XML parses, the tool writes the tree back out:

  • Format puts each element on its own line and indents every nesting level.
  • Minify drops the whitespace-only text between tags.
  • Kept as they are: comments (<!-- … -->), CDATA sections (<![CDATA[ … ]]>), processing instructions (<?xml-stylesheet … ?>) and the XML declaration (<?xml version="1.0" encoding="UTF-8"?>).

Well-formed is not the same as valid

The XML specification defines two levels. A well-formed document follows the basic syntax rules, which is what this tool checks. A valid document is also well-formed and conforms to the rules in its document type declaration (or, in practice, an XSD schema): which elements are allowed, in what order, with which attributes. This tool doesn’t check validity, so a well-formed file can still be rejected by an application that expects a particular schema.

Worked examples

Format a one-line document (2 spaces). This input:

<?xml version="1.0" encoding="UTF-8"?><catalog><!-- prices in USD --><book id="bk101"><title>XML Basics</title><price>29.99</price></book></catalog>

becomes:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <!-- prices in USD -->
  <book id="bk101">
    <title>XML Basics</title>
    <price>29.99</price>
  </book>
</catalog>

The declaration and the comment are still there. Minify turns it back into a single line.

Find a mismatched tag. In <book><title>XML Basics</book>, the <title> element is never closed before </book>. The XML specification requires the name in an end-tag to match its start-tag and elements to nest properly, so the parser reports an error at the </book> tag.

Escape an ampersand. <company>Smith & Sons</company> is not well-formed, because a literal & must start an entity reference. Write Smith &amp; Sons, or wrap the text in a CDATA section: <company><![CDATA[Smith & Sons]]></company>.

Well-formedness rules at a glance

Rule (XML 1.0)Not well-formedWell-formed
Exactly one root element<a/><b/><root><a/><b/></root>
End-tag matches start-tag<a><b></a></b><a><b></b></a>
Attribute values in quotes<book id=1><book id="1">
No literal & or < in text<p>a < b</p><p>a &lt; b</p>
No -- inside a comment<!-- a -- b --><!-- a - b -->
Declaration at the very starta blank line, then <?xml …?><?xml …?> on line 1

Frequently asked questions

Does the XML formatter validate against a schema or DTD?

No. It checks that the XML is well-formed: one root element, matching and properly nested tags, quoted attributes and escaped special characters. Checking against an XSD schema or a DTD needs a validating parser.

Is my XML uploaded anywhere?

No. The tool uses the XML parser built into your browser, so the text you paste stays on your device.

Will formatting remove my comments or CDATA sections?

No. Comments, CDATA sections, processing instructions and the XML declaration are kept in both the formatted and the minified output.

Why is my XML not well-formed?

The usual causes are a missing or mismatched closing tag, a bare & or < in text, an attribute value without quotes, two root elements, or text before the XML declaration. The error message from the browser points to the problem.

Can formatting change what my XML means?

Formatting only changes the whitespace between tags. Most applications ignore that whitespace, but if yours treats it as data, keep the original layout.

Related guides

PC Moment title card for JSON Formatter

Free tool

JSON Formatter and Validator

Paste JSON to pretty-print or minify it, sort the keys and find syntax errors by line and column. Everything runs in your browser.

Free calculator

Free Developer and PC Tools

Free formatters, decoders and converters for developers and PC users. Everything runs in your browser, and nothing you type is sent anywhere.

2 min read