XML formatter and validator

Pretty print, minify and check XML, with the exact line and column of any error.

Output

What this tool does

Paste XML, or open an .xml file, and the tool does two things at once. It checks that the document is well-formed, and it prints it either indented so a person can read it or squeezed onto one line so a machine can ship it. Everything happens in your browser. The text is never sent anywhere, which matters when the XML is a configuration file, an invoice or a SOAP message with real data in it.

How to use it

  1. Paste or type XML into the left box, press "Open a file", or drop a file on the page. A small example is one click away.
  2. Choose Pretty print or Minify. For pretty printing, pick two spaces, four spaces or a tab, and optionally put each attribute on its own line or sort them A to Z.
  3. Read the result on the right, then copy it or download it as formatted.xml. "Use as input" feeds the result back in, which is handy for minifying what you just formatted.

If something is wrong, the tool does not guess. It stops at the first problem, says in plain words which rule was broken, shows the line and column, prints that line with a caret under the spot, and offers a button that puts the cursor there.

What "well-formed" means here

The checker follows XML 1.0. There must be exactly one root element and no text outside it. Every start tag needs a matching end tag in the right order, attribute values must be quoted, an attribute name can appear only once per element, and < and & may not appear in text or values as themselves: write &lt; and &amp;. Entity references must be one of the five built in (&lt; &gt; &amp; &quot; &apos;), a numeric reference such as &#169;, or a name that the document's own DOCTYPE declares. A comment may not contain two hyphens in a row, the XML declaration must be the first thing in the file, and a namespace prefix such as s: must be declared with xmlns:s on the element or one of its parents. Control characters such as U+0001 are not allowed anywhere.

This is a syntax check, not validation. The tool does not compare the document with a DTD or an XML Schema, and it never fetches external entities or schemas.

Pretty printing without changing your data

An XML formatter has one dangerous temptation: adding line breaks inside text. In <p>Hello <b>world</b></p> the space after "Hello" is content, and re-indenting the tag would add a space that was never there. So this tool only adds or removes whitespace where the element holds nothing but other elements, comments and processing instructions. An element with text or a CDATA section in it is written exactly as it was, children included. The XML declaration, the DOCTYPE, comments, processing instructions and CDATA are copied character for character, attributes keep their order and their quote marks, and namespace prefixes stay as written. Minifying the pretty printed result gives the same text as minifying the original, and the tests check that on many documents.

Minify

Minify removes the whitespace that only separates things: indentation and line breaks between elements, and blank lines around comments and the declaration. Text inside elements is kept as it is, so a text element such as <name> Ann </name> is not trimmed. Minifying is useful for payloads and for comparing two files, and a pretty printed copy is easier to compare with the diff checker.

Common errors and what they mean

  • The closing tag does not match. <a><b></a></b> closes the tags in the wrong order. The message names the tag it expected and the one it found.
  • The element is never closed. A start tag with no end tag. The position points at the unclosed start tag.
  • The value must be in quotes. HTML allows width=10; XML does not.
  • A bare ampersand. fish & chips must be fish &amp; chips. The same goes for < in text.
  • An undeclared namespace prefix. <s:Body> without xmlns:s anywhere above it.
  • Two root elements or text after the root. Often a sign that two documents were pasted together.

Good to know

Browsers also have a built-in XML parser, but each browser words its errors differently and none names the rule or gives a clean line and column. This tool uses its own checker, so the answer is the same everywhere. Size limit: five million characters. For data in other formats, see the JSON formatter, the CSV and JSON converter and the SQL formatter.

Frequently asked questions

Does it validate against a schema or DTD?

No. It checks that the document is well-formed XML 1.0: the syntax rules every XML parser enforces. Checking that elements appear in the order a schema demands is a different job. DOCTYPE declarations are kept and entities declared in them are accepted, but nothing is fetched.

Will formatting change my data?

Not the data. Indentation is added only between elements that contain nothing but other elements. Text, CDATA, comments, attributes and namespaces are kept as written, and an element with text in it is never re-spaced.

Why is my XML rejected when the browser opens it fine?

Browsers are strict about XML files but forgiving about HTML, so a page that looks fine may be HTML. The usual XML-only problems are unquoted attributes, a bare ampersand, tags closed in the wrong order, an undeclared namespace prefix and several root elements.

Is my XML uploaded anywhere?

No. The checking and printing run in your browser, and nothing leaves your device. That makes it fine for configuration files and messages that contain real data.

How large a file can I use?

Up to five million characters, whether you paste or open a file. Very long results are shown in part in the output box, while Copy and Download always contain the whole result.