XML isn't dead. It quietly runs Maven builds, Android layouts, SVG files, SOAP services, RSS feeds and half the enterprise world's config. And every one of those files eventually lands on your screen as a single unreadable line, usually right when you're trying to debug something urgent.

Formatting XML — pretty-printing it with consistent indentation and line breaks — is one of those small tasks that saves a surprising amount of time. Let's talk about when it matters and where it quietly goes wrong.

What "formatting XML" actually means

At its core, formatting takes semantically valid XML and rewrites the whitespace between elements so a human can read it. The data doesn't change — element names, attributes, and text content stay identical. Only the insignificant whitespace gets adjusted.

That word "insignificant" is doing a lot of work, and it's where most of the gotchas live. More on that below.

A minified payload like this:

<order id="42"><customer><name>Ada Lovelace</name><email>[email protected]</email></customer><items><item sku="ABC" qty="2"/></items></order>

becomes something you can actually scan:

<order id="42">
  <customer>
    <name>Ada Lovelace</name>
    <email>[email protected]</email>
  </customer>
  <items>
    <item sku="ABC" qty="2"/>
  </items>
</order>

When you'll reach for it

  • Debugging API responses. SOAP and legacy REST endpoints love returning one-line XML. Pretty-printing makes a 400-character blob navigable in seconds.
  • Reviewing diffs. A consistently formatted file produces clean, minimal diffs in version control. One attribute change shouldn't show up as a whole line rewrite.
  • Hand-editing config. Spring XML, web.xml, Maven POMs — all much easier to edit when the structure is visible.
  • Learning an unfamiliar schema. Nesting becomes obvious when it's indented. You can see parent/child relationships at a glance.

The flip side: for transmission over the wire, you usually want the opposite — minified XML with no extra whitespace to shave off bytes. Keep a tool handy that does both.

The gotchas that bite

Whitespace isn't always insignificant

By default, whitespace between elements is ignorable. But the moment an element contains mixed content — text and child elements together — reformatting can corrupt the data. Consider:

<p>Click <a href="/x">here</a> to continue</p>

If a naive formatter puts <a> on its own indented line, you've just inserted spaces around "here" and changed the rendered output. Any formatter worth using treats mixed-content elements carefully. XHTML and DocBook are full of this pattern.

xml:space="preserve"

The spec gives you an explicit way to say "don't touch the whitespace in here." If an element carries xml:space="preserve", a correct formatter must leave its contents exactly as they are. This matters hugely for things like code listings or poetry embedded in documents.

CDATA sections

Content inside <![CDATA[ ... ]]> is literal. Script blocks, embedded SQL, or raw HTML often live here. Reindenting that content changes it. A good formatter passes CDATA through untouched.

Encoding and the declaration

If your file declares <?xml version="1.0" encoding="UTF-8"?>, make sure whatever you use actually reads and writes that encoding. Mangling a UTF-8 file into Latin-1 will turn your accented characters into garbage, and the problem won't surface until much later.

Not-actually-valid XML

Formatters aren't repair tools. If you've got an unclosed tag or an ampersand that should be &amp;, formatting will fail (or should fail) rather than guess. That's a feature — an error that points at line 37 is far more useful than silently "fixing" your document into something you didn't mean.

A quick worked example

Say you pull this response from a service and need to understand it fast:

<?xml version="1.0"?><catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><price>44.95</price></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><price>5.95</price></book></catalog>

Paste it into a tool like the XML Formatter and you get a structured, two-space-indented version instantly:

<?xml version="1.0"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <price>44.95</price>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <price>5.95</price>
  </book>
</catalog>

Now the two <book> entries and their shared structure are obvious. If you needed to spot a missing <price> or compare attributes, it would jump out immediately.

Doing it in code

For one-off inspection, a browser-based tool is the fastest path. For pipelines, format programmatically. In Python:

from xml.dom import minidom

raw = "<catalog><book id='1'><title>Example</title></book></catalog>"
print(minidom.parseString(raw).toprettyxml(indent="  "))

One warning: minidom can add blank lines when your source already contains whitespace between elements. lxml with pretty_print=True tends to behave better, and it respects more edge cases. Pick the library, not just the one-liner.

A few habits worth keeping

  • Standardize indentation across a team. Two spaces or four — pick one and enforce it, so diffs stay quiet.
  • Format before committing config files, not in a separate noisy commit later.
  • Validate first if the file came from outside. Format is for humans; validation is for correctness. They're different jobs.
  • Never reformat files protected by xml:space="preserve" or heavy with CDATA unless your tool explicitly handles them.

Formatting XML is a tiny thing, but it's the difference between squinting at a wall of text and actually reading your data. Keep a reliable formatter in reach, understand what it won't touch, and it'll quietly pay for itself every time a one-line payload lands in your terminal.

Photo by Florian Olivo on Unsplash