Skip to content
XML Formatter

XML Formatter

Format XML online. Paste your XML, click format, then copy or download the prettified output.

Formats XML with indentation Validates XML and shows clear errors Copy and download formatted XML Import XML from URL or file Theme switcher Fullscreen editor mode

Input XML

Paste your XML here

Formatted Output

Copy or download the formatted XML

Key Features

  • Formats XML with indentation
  • Validates XML and shows clear errors
  • Copy and download formatted XML
  • Import XML from URL or file
  • Theme switcher
  • Fullscreen editor mode

How to Use

  1. Paste your XML into the input editor.
  2. Click Format to prettify your XML.
  3. Use Validate to check for errors.
  4. Copy or download the formatted output.

How the XML formatter works

Paste XML into the left editor, pick an indent of 2 or 4 spaces, and click Format (or press Ctrl + Enter, Cmd + Enter on a Mac). Two things happen, both in your browser:

  1. The text is parsed with the browser's built-in DOMParser as application/xml. If the document is not well-formed, formatting stops and the parser's error message appears above the editors. Nothing broken is ever written to the output.
  2. If parsing succeeds, the formatter works on your original text, not on the parsed tree. It inserts a line break wherever one tag ends and the next begins, then indents each line by its nesting depth. Because it rewrites only the whitespace between tags, your attribute order, quote style, entity references such as &amp;, comments, and the <?xml ...?> declaration come through exactly as you wrote them.

The line under the toolbar compares input and output size. Formatting a minified file usually makes it larger, since every indent is extra bytes. The Validate button runs only the parsing step and reports "Valid XML" without touching the output.

Example: a minified API response

A single-line response from a product feed like this:

<?xml version="1.0" encoding="UTF-8"?><catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer&apos;s Guide</title><price>44.95</price><tags><tag>xml</tag><tag>dev</tag></tags><cover/></book><!-- more --></catalog>

comes out with Indent: 2 as:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer&apos;s Guide</title>
    <price>44.95</price>
    <tags>
      <tag>xml</tag>
      <tag>dev</tag>
    </tags>
    <cover/>
  </book>
  <!-- more -->
</catalog>

Elements that contain only text stay on one line, the self-closing <cover/> does not add a level, and &apos; is left as an entity rather than decoded. Namespaced documents such as SOAP envelopes format the same way: a prefix like soap:Body is treated as part of the tag name.

Errors that stop formatting

The formatter needs well-formed input, so these are the problems you will hit most often. The messages below are the ones Chrome and Edge show; Firefox and Safari word them differently but point to the same spot.

  • A bare ampersand. <q>Tom & Jerry</q> fails with "xmlParseEntityRef: no name". Write &amp;, or wrap the text in a CDATA section.
  • HTML entities. &nbsp;, &copy; and friends do not exist in XML ("Entity 'nbsp' not defined"). Use a numeric reference such as &#160; instead. Only &lt;, &gt;, &amp;, &quot; and &apos; are built in.
  • Tag name case. <Item>...</item> is a mismatch, because XML names are case-sensitive.
  • Content before the declaration. Any text in front of <?xml, such as a log prefix or a second declaration from two pasted files, gives "XML declaration allowed only at the start of the document". Leading blank lines are fine, because the tool trims them before parsing.
  • Two root elements. Concatenated log entries like <event/><event/> produce "Extra content at the end of the document". Wrap them in one parent element.

Limitations to know about

Because the formatter indents text rather than rebuilding the document from the parsed tree, a few inputs come out wrong. Check the output if your XML contains any of these:

  • Start tags split across lines. If attributes are already on separate lines (<server on one line, host="a" on the next), the indentation of that element and everything after it will be off by one level.
  • CDATA containing markup. <![CDATA[<b>x</b> <i>y</i>]]> gets a line break inserted inside the CDATA, which changes its content.
  • Mixed content. In <p>Hello <b>big</b> <i>world</i></p> the space between the two inline tags is replaced by a line break. For XHTML or DocBook-style prose, that whitespace can matter.
  • Multi-line text. Each line of a text node is trimmed and re-indented, so leading spaces inside text (for example in an embedded script) are not preserved.

The tool also does not check your document against a DTD or XSD schema, does not sort attributes, and has no tab-indent or minify mode.

When to use a different tool

  • To find out exactly why a document is rejected, and get the parser's own serialized copy, use the XML Validator.
  • To turn a formatted feed or SOAP response into an object you can use in JavaScript, use XML to JSON.
  • For HTML pages, which are rarely well-formed XML (think <br> without a slash), use the HTML Beautifier instead.
  • To build XML from an existing JSON payload, use JSON to XML.

Frequently asked questions

Why does the formatter refuse to format my XML?

The input is parsed with your browser's DOMParser before any indentation is added. If the XML is not well-formed (an unclosed tag, a bare ampersand, two root elements), the parser error is shown instead of output. Fix the reported line and column, then click Format again.

Does formatting change my XML data?

For typical element-only documents such as config files, feeds, and SOAP messages, no: only whitespace between tags changes, and attributes, entities, and the XML declaration are kept as written. Documents with mixed content (text and tags side by side), CDATA sections containing markup, or text spanning several lines can have their whitespace changed, so check those before replacing the original.

Can I indent with tabs or change the indent size?

The Indent menu offers 2 or 4 spaces. Tabs are not available. To switch an already indented file from 4 to 2 spaces, paste it in and format it again with Indent: 2.

Can this tool minify XML?

No. It only adds line breaks and indentation. It never removes whitespace inside text, so it cannot produce a single-line minified file.

Is my XML uploaded anywhere?

No. Parsing and formatting run in your browser. Pasted text and imported files stay on your device. Import URL fetches the file directly from your browser, so the remote server must allow cross-origin requests.

Latest from Our Blog

Tips, tutorials, and insights about web development