Toolerax logoToolerax
By ··6 min read

How to Format XML

How to Format XML

XML never died — it just moved into the plumbing. SOAP payloads, RSS feeds, sitemaps, Android layouts, Maven builds, invoice standards: all XML, and nearly all of it arrives as an unreadable single line. Formatting it is easy; formatting it safely requires knowing which whitespace is decoration and which is data.

What a pretty-printer does

Good XML formatting follows a few conventions:

  • One element per line, children indented one level deeper
  • Elements with a single text value stay on one line: <title>Clean Code</title>
  • Empty elements collapse to self-closing form: <br/>
  • Attributes stay on the opening tag; comments and CDATA are preserved

Two or four spaces per level is convention; deeper matters less than consistency.

The whitespace rule that keeps you safe

Whitespace between elements is structural and safe to reflow. Whitespace inside text content is real data — reformatting <name>Ada Lovelace</name> must not touch the space in the name. Documents with mixed content (text and tags interleaved, like HTML paragraphs) are the risky case: aggressive reformatting can change meaning, which is why careful formatters leave single-text elements inline.

Validate first, format second

A formatter that doesn't parse can't warn you. Real parsing catches the classic XML errors before they reach production:

  • Mismatched or unclosed tags (<a><b></a>)
  • Unescaped special characters — a bare & is the all-time champion
  • Multiple root elements, or stray text outside the root
  • Unquoted attribute values

Remember the five entities: &amp; &lt; &gt; &quot; &apos;. Everything else can be literal (in UTF-8) or numeric references.

When to minify instead

The reverse operation — stripping inter-element whitespace — matters for payloads sent over the wire and for large feeds where indentation can add 20–30% of file size. Beautify for humans reading; minify for machines transferring. Both directions must preserve the actual data exactly.

XML vs JSON vs YAML, in one breath

JSON won APIs with less syntax; YAML won config files with even less; XML keeps the territories where attributes, namespaces, schemas and document-style mixed content earn their verbosity — publishing, enterprise integration, office formats. Odds are you touch all three weekly.

Format yours now

The XML formatter validates with a real parser (clear error messages included), beautifies with your chosen indent or minifies, and preserves namespaces, comments and CDATA — entirely in your browser, so enterprise payloads stay private. For the sibling formats, see the JSON formatter and the YAML ↔ JSON converter.

Photo of S.R. Shohan

Written by

Healthcare Software Developer who builds hospital and laboratory systems in .NET — and the developer behind every tool on Toolerax. Each guide is written alongside the tool it explains and checked against how that tool actually behaves. More about S.R. Shohan

Tools mentioned in this article