Skip to content

XML Formatter

Layout
Formatted XML —
Elements
—
Maximum nesting depth
—
Attributes
—
Comments
—
Output lines
—
Input characters
—
Output characters
—

An XML formatter puts each tag on its own line and indents it by nesting level. Paste the W3Schools note document (a note root holding to, from, heading and body) written on one line and you get six lines: the root, four children indented two spaces with their text inline, and the closing tag. Choose Minify to go the other way.

About this tool

XML from an API response, a SOAP log or a config export often arrives as a single long line, which makes a missing end tag nearly impossible to spot. Paste it here, or open a local .xml file, and the formatter rewrites it with one tag per line and consistent indentation. Elements holding only short text stay on one line, so a record reads like a table rather than a staircase. Alongside the output you see how many elements and attributes the document has, how deep it nests and how the character count changed. If tags cross or never close, the formatter names the offending tag instead of producing broken output. It checks well-formedness only: it does not validate against a DTD or XSD schema, resolve entities or namespaces, and text inside elements has runs of whitespace collapsed to single spaces.

How to use it

  1. Paste or open the XML

    Paste markup into the box, or use the file button to read a .xml or .txt file from your device.

  2. Pick a layout

    Choose 2 spaces, 4 spaces or tabs for indentation, or Minify to strip whitespace between tags.

  3. Read the checks

    Element, attribute and comment counts plus nesting depth appear under the output; a tag mismatch shows as an error naming the tag.

  4. Copy the result

    Copy the formatted XML from the result box into your editor or request body.

Examples

A one-line note document

XML
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
Layout
2 spaces

Result Formatted XML: <note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Elements: 5
Maximum nesting depth: 2
Attributes: 0
Comments: 0
Output lines: 6
Input characters: 112
Output characters: 125

  1. Split the input into 14 tokens (tags, text, comments, declarations).
  2. Checked that every start tag has a matching end tag: 5 elements, nested 2 deep.
  3. Wrote each tag on its own line, indented one level per nesting depth; short text-only elements stay on one line.

The root and its four children land on six lines, each child indented two spaces with its text kept inline.

Minify an indented snippet

XML
<a> <b id="1">x</b> <c/> </a>
Layout
Minify

Result Formatted XML: <a><b id="1">x</b><c /></a>
Elements: 3
Maximum nesting depth: 2
Attributes: 1
Comments: 0
Output lines: 1
Input characters: 33
Output characters: 27

  1. Split the input into 9 tokens (tags, text, comments, declarations).
  2. Checked that every start tag has a matching end tag: 3 elements, nested 2 deep.
  3. Removed whitespace between tags and joined everything on one line.

Whitespace between tags is dropped and the self-closing tag is normalised, giving a single line ready to embed.

How it is calculated

indent of a line = nesting depth × indent unit

nesting depth
number of start tags still open when the line is written
indent unit
2 spaces, 4 spaces or one tab; empty when minifying

The input is split into tokens: start tags, end tags, self-closing tags, text, comments, CDATA sections and declarations such as the XML prolog or DOCTYPE. A stack tracks open elements; every end tag must match the name on top of the stack, as the W3C XML 1.0 well-formedness rules require, otherwise the tool stops and reports the pair. Each token is then written on its own line, indented by the current depth. An element containing only text is kept on a single line.

When not to use it

  • Do not use it to validate XML against a schema; it checks tag nesting only.
  • Avoid it for documents where whitespace inside text is meaningful, such as xml:space="preserve" content.
  • It is not built for HTML, where unclosed tags such as br are legal.

Common mistakes

  • Leaving a bare ampersand or less-than sign in text instead of the & or < entity.
  • Closing elements in the wrong order, such as opening b then i but closing b first.
  • Pasting two sibling records with no shared root element.
  • Expecting attribute order or quote style to be changed; attributes are kept as written.

Frequently asked questions

Does formatting change the meaning of my XML?

Whitespace between tags is usually insignificant, so indenting does not change what a parser reads. Text inside elements has runs of spaces and line breaks collapsed to one space, which can matter for mixed content or preserved whitespace. CDATA sections and comments are copied unchanged.

Why does it report that a tag is never closed?

Every start tag needs a matching end tag with the same name, or the tag must be self-closing. The formatter tracks open elements and, at the end of the input, names the innermost one still open so you know where to look.

What does the warning about top-level elements mean?

A well-formed XML document has exactly one root element wrapping everything else. If you paste two self-closing elements a and b one after the other, both are at the top level. The output is still formatted, but most parsers will reject the document until you wrap the pieces in a single root.

What happens to self-closing tags?

A self-closing tag is kept self-closing and normalised with one space before the slash. An empty start and end pair stays as a pair on one line; the two forms mean the same thing to an XML parser.

Can I format a large file?

Yes, the file is read in your browser and nothing is uploaded. Files of a few megabytes format quickly; very large exports may feel slow because the whole document is re-written on every change.

How is nesting depth counted?

The root element is depth 1, its children depth 2, and so on. The maximum depth is the deepest element found. In the W3Schools note example the children of the note root sit at depth 2, so the maximum is 2.