XML Formatter
Indent and format well-formed XML documents.
- Runs locally in your browser
- No upload required
Try it
Supported formats
Privacy
Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.
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
- Tokenise elements, attributes, comments, CDATA and processing instructions with a quote-aware scanner.
- Maintain an element stack; a close tag must match the most recent open tag and the stack must be empty at the end.
- Emit each element on its own line at a depth-based indent.
Usage
- Paste or type a well-formed XML document.
- Choose a 2- or 4-space indent.
- 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
{
"text": "<root><item id=\"1\">x</item></root>",
"indentSize": 2
}Self-closing tag
{
"text": "<root><a/><b/></root>",
"indentSize": 4
}CDATA preserved
{
"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
Send feedback
Tell us if this tool helped, report a problem, or suggest a tool. Only the message you type is sent — never the input you used in the tool.
Was this tool useful?
Prefer to write to us directly? See the contact page.