XML Formatter

Indent and format well-formed XML documents.

  • Runs locally in your browser
  • No upload required

Try it

Paste well-formed XML. Malformed nesting is reported with a location.

Formatted XML
<root> <item id="1"> x </item> </root>
Lines
5
<root>
  <item id="1">
    x
  </item>
</root>

Supported formats

Input
text, number
Output
text

Privacy

Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.

Processing
In your browser
Privacy level
Browser only

Indented 1 line(s) of XML at 2-space depth.

How it works

  • Tokenised elements, attributes, comments, CDATA and processing instructions.
  • Tracked the element stack and reported the first mismatched or unclosed tag.
  • Emitted each element on its own line at a depth-based indent.
  • XML is strictly nested, so the formatter can prove well-formedness while formatting.
  • Output is 5 line(s); formatting it again changes nothing.

Methodology

  1. Tokenise elements, attributes, comments, CDATA and processing instructions with a quote-aware scanner.
  2. Maintain an element stack; a close tag must match the most recent open tag and the stack must be empty at the end.
  3. Emit each element on its own line at a depth-based indent.

Usage

  1. Paste or type a well-formed XML document.
  2. Choose a 2- or 4-space indent.
  3. Run the formatter; malformed nesting is reported with a location.

Examples

These are examples, not current values: they use fixed inputs, while the interactive tool computes live results in your browser.

Nested elements

jsonSelect to copy
{
  "text": "<root><item id=\"1\">x</item></root>",
  "indentSize": 2
}

Self-closing tag

jsonSelect to copy
{
  "text": "<root><a/><b/></root>",
  "indentSize": 4
}

CDATA preserved

jsonSelect to copy
{
  "text": "<root><![CDATA[a < b]]></root>",
  "indentSize": 2
}

Limitations

Well-formedness is the contract

Unlike HTML, XML must nest exactly. The formatter uses that strictness to validate structure while it indents, so a syntax error is surfaced rather than hidden.

Frequently asked questions

What happens if my XML is malformed?
The formatter reports the first mismatched or unclosed tag with its line and column and does not produce a repaired document.
Are CDATA and comments preserved?
Yes. CDATA sections and comments are treated as opaque content and copied exactly.
Is my XML uploaded?
No. The document is processed entirely in your browser.

Sources

  1. Extensible Markup Language (XML) 1.0 — W3C
  2. MDN Web Docs — XML introduction — MDN