Skip to main content

XML Formatter

Run Format to see the result here.

Format XML with clear indentation while preserving its content and meaning.

What it does

XML Formatter lays out an XML document with indentation so you can see how its elements nest. It changes how the document is laid out, not what it says: text, attributes, comments, CDATA sections, processing instructions and namespaces are kept as written.

Use it when XML arrives on one long line or is indented unevenly. Your XML is read as plain text: nothing in it is run, and nothing it refers to is loaded or looked up.

What it supports

  • One well-formed XML document with a single root element. An XML declaration, a DOCTYPE, comments and processing instructions before the root element are accepted, and so are comments and processing instructions after it.
  • A DOCTYPE, including one with a DTD written inside the document, is kept exactly as written, line breaks included. It is only text to the tool: nothing is loaded, fetched or looked up from it, and no entity it declares is expanded. Not accepted: fragments with more than one root element, a second DOCTYPE, a DOCTYPE after the root element, and a DOCTYPE that never ends.
  • The five predefined entities (&, <, >, " and ') and numeric character references (A or A) are always known. Any other entity, such as  , is refused when the document has no DOCTYPE, because nothing declares it. When the document has a DOCTYPE, such references are accepted and left as written; they are never expanded.
  • Namespaces: prefixes must be declared. Prefixes, xmlns declarations and xml: attributes such as xml:lang and xml:space are kept as written.
  • Comments, CDATA sections, processing instructions and the XML declaration are kept as written, and attribute values keep the quotes, single or double, that you used.
  • Text is never reflowed or trimmed. The text of an element that holds only text, text mixed with elements, and everything inside an element marked xml:space="preserve" is kept exactly as written, spaces and line breaks included.
  • Indentation of 2 or 4 spaces, never tabs. Line breaks in the result are always the same kind, whatever your text used.

Good to know

  • XML is strict. A tag that is never closed, closing tags in the wrong order, an unquoted attribute value, a bare & or a literal < in text, a repeated attribute, an undeclared namespace prefix and a character that XML does not allow are all refused. The tool points at the earliest problem it finds, and it never repairs XML.
  • Besides indentation, the tool makes two changes: a run of blank lines between elements becomes one blank line, and an element with many attributes can have them wrapped onto separate lines. An element with nothing between its tags, such as <a></a>, stays as you wrote it.
  • Whitespace that only separates elements is replaced by the indentation. Where text sits next to elements, the tool cannot tell whether the spaces matter, so it leaves them alone: a document written as one long line of mixed content stays on one line.
  • The tool checks that your XML is well-formed, not that it is valid: it never checks the document against a DTD or a schema, so a DOCTYPE is carried through as it is.
  • If indentation would make the result far larger than the size limit, the tool stops before formatting. Use 2 spaces or less nesting.

How to use it

  1. Paste or type one XML document in the input box. Nothing runs while you type.
  2. Choose the indentation (“2 spaces” or “4 spaces”) and press “Format”.
  3. Read the result next to your input. If your XML is refused, the message says what is wrong and where.
  4. Press “Copy” to put the result on your clipboard, or “Download” to save it as formatted.xml.

Examples

Format a document

Paste this XML and press “Format” with “2 spaces” selected. Elements that only contain other elements are indented. The note element mixes text with an element, so it stays on one line, exactly as written. The empty element <book id="2"></book> also stays as you wrote it.

<?xml version="1.0" encoding="UTF-8"?><library><book id="1"><title>Dune</title><note>Read <i>first</i>.</note></book><book id="2"></book></library>
<?xml version="1.0" encoding="UTF-8"?>
<library>
  <book id="1">
    <title>Dune</title>
    <note>Read <i>first</i>.</note>
  </book>
  <book id="2"></book>
</library>

A closing tag that does not match

The element book is never closed, so the closing tag that comes next, </library>, does not match the element that is open. The tool does not guess where a missing tag belongs. It refuses and points at the closing tag it could not match.

<library>
  <book>
    <title>Dune</title>
</library>

This closing tag does not match the element that is open. Closing tags must be identical to the opening tags, including upper and lower case, and must close in reverse order.

Line 4, column 1

A DOCTYPE declaration

Paste this XML and press “Format”. The DOCTYPE declares an entity named who, and it is kept exactly as written. It is only text to the tool: nothing is loaded from it, and the reference &who; stays as written instead of being replaced by Ana.

<!DOCTYPE note [<!ENTITY who "Ana">]>
<note><to>&who;</to></note>
<!DOCTYPE note [<!ENTITY who "Ana">]>
<note>
  <to>&who;</to>
</note>

A name with accented letters

Paste this XML and press “Format”. XML allows accented letters in element and attribute names, and they are formatted like any other name.

<livro><título>Dom Casmurro</título><autor ano="1899">Machado de Assis</autor></livro>
<livro>
  <título>Dom Casmurro</título>
  <autor ano="1899">Machado de Assis</autor>
</livro>

Limits and privacy

Size limits

  • Input: up to 2,000,000 bytes of UTF-8 text (about 2 MB). Accented letters and emoji take more than one byte each. Larger input is rejected.
  • Result: also up to 2,000,000 bytes. If the complete result would be larger, the tool shows only an incomplete preview of the first 100,000 bytes, and that preview cannot be copied or downloaded.
  • Nesting: up to 128 levels of elements (the root is level 1; self-closing elements count too) inside each other. Deeper XML is refused because it is beyond what the tool processes.
  • Time: a run that takes longer than its time limit is stopped. Try a smaller input.

Your privacy

The text you type or paste, or open from a local file, is processed locally in your browser, in a dedicated Web Worker. It is never uploaded or sent to the server (a file you open is read in your browser only), and it is not written to storage, cookies or the address bar.

“Copy” puts the result on your clipboard and “Download” saves it as a file, but only when you press the button. The file is created in your browser, so nothing is uploaded.

For the full details, see the Privacy Policy

Related tools and pages

Your text stays in the page when you switch tools in the same tab, so you can try the same text in another tool. Reloading or closing the tab ends it.

All tools