XML Formatter
Tag-structure re-indentation with CDATA and comments preserved, plus a minifier.
API responses arrive as one line, SOAP envelopes drift out of alignment, Android layouts end up a mess after a edit — XML readability is all indentation, and hand-fixing it is slow and error-prone. This tool re-lays the document by tag structure: open tags, close tags and text nodes each land in place, with self-closing tags and declarations (<?xml ?>, <!DOCTYPE>) handled and CDATA/comments kept whole.
The implementation is tag-level lexing plus depth counting, not a full XML parse: mildly broken nesting still gets a sensible "self-healing" layout (close tags align to the nearest matching open tag) instead of a page-level error. The minifier emits a single-line version for logs and transport.
Why whitespace inside text nodes is untouched
The spaces in <title> Hello World </title> are content. Formatting adjusts only the whitespace between tags, keeping text-node interiors verbatim — unlike minidom-style libraries that normalize text, which is the safer behavior for config files and message payloads.
CDATA and comments are black boxes
CDATA may contain code with < > &, comments may hold commented-out old tags — running those through tag parsing would scramble them. The formatter treats each as an indivisible block and only indents at the block level.
Frequently asked questions
- Can it format XML with syntax errors?
- Usually yes. Tag-level lexing with depth self-healing handles unclosed and misnested tags gracefully; a misspelled tag name aligns its close tag to the nearest same-name open tag.
- Does it reorder attributes or touch the declaration?
- No. Tag interiors (attributes, declarations) are preserved verbatim; only indentation and line breaks between tags change.
- Is this the same as XML escaping?
- No. Formatting is layout only; turning & < > into entities is an escaping concern handled by the conversion tools.