How to use XML Formatter
- Paste your XML (or click Load example).
- Choose the indentation, attribute wrapping and whether to collapse empty tags.
- The formatted result appears as you type; click Minify for a compact single line instead.
- Copy or download the result with the buttons above the output.
XML Formatter features
- Pretty-prints XML with 2-space, 4-space or tab indentation
- Preserves CDATA sections, comments, processing instructions and DOCTYPE exactly
- Keeps short text-only elements on one line (<title>Text</title>)
- Optional attribute wrapping and collapsing of empty elements into <tag/>
- Minify mode strips insignificant whitespace and, optionally, comments
- Well-formedness check first, with line and column of any error
- Document statistics: root element, element and attribute counts, depth
XML Formatter example
Format a compact catalog fragment
Input:
<catalog><book id="bk101"><title>XML Guide</title><price>44.95</price></book></catalog>Output:
<catalog>
<book id="bk101">
<title>XML Guide</title>
<price>44.95</price>
</book>
</catalog>Frequently asked questions about XML Formatter
Does the formatter change my data?
No. It only adjusts whitespace between elements and normalises spacing inside tags. Text content, CDATA sections, comments, processing instructions and entity references are copied through verbatim.
What happens if my XML is not well-formed?
The tool validates first and reports the problem with a line and column number instead of producing broken output. Fix the reported location and the formatter runs again automatically.
Can it minify XML too?
Yes — click Minify to strip insignificant whitespace (and optionally comments) and collapse empty elements into <tag/>.
Is xml:space="preserve" respected?
Yes. Whitespace inside any element that declares xml:space="preserve" is left exactly as written.
Is my XML uploaded anywhere?
No. Formatting runs entirely in your browser; nothing is sent to a server.
Technical notes
The formatter is a token-based printer rather than a parse-and-rebuild: it scans markup tokens (declaration, PI, comment, CDATA, DOCTYPE, tags, text) and re-emits them with indentation, so nothing is lost or reordered. Elements carrying xml:space="preserve" keep their whitespace untouched.
Validation uses fast-xml-parser's XMLValidator, which reports unmatched tags, invalid attribute syntax and illegal characters with line/column positions.