XML gets a bad rap these days, usually from people who've only ever touched JSON. But it's still everywhere: Maven POMs, Android layouts, SVGs, RSS feeds, SOAP payloads, SAML assertions, and half the enterprise integrations you'll ever debug. When you get handed a 4KB blob of it on a single line, readable formatting is the difference between finding the bug in 30 seconds and 30 minutes.
What "formatting" actually means here
Formatting (or pretty-printing) XML means taking valid markup and laying it out with consistent indentation and line breaks so a human can read the structure. It doesn't change the meaning of the document — the same elements, attributes, and text nodes are there. It just makes the nesting visible.
The reverse operation, minifying, strips that whitespace back out to shave bytes off the wire. Both are useful at different points in a workflow.
Here's the before and after. Nobody wants to read this:
<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><price>44.95</price></book></catalog>And the formatted version, which you can actually scan:
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<price>44.95</price>
</book>
</catalog>When you'd reach for it
- Debugging API responses. SOAP and other XML services love to return everything on one line. Formatting reveals the envelope, headers, and body structure instantly.
- Reviewing generated files. Tools that emit XML — serializers, build plugins, export features — rarely indent nicely. Format it before you commit or diff.
- Writing config by hand. A well-indented
pom.xmlor Spring context file is far easter to edit without breaking nesting. - Comparing two documents. Diffing minified XML is miserable. Format both sides first and your diff tool becomes useful again.
Common gotchas
Whitespace is sometimes significant
This is the big one. XML has xml:space="preserve", and in mixed-content elements, whitespace between inline tags can matter. Consider:
<p>Click <b>here</b> now</p>If a naive formatter inserts line breaks around <b>, you may end up with extra spaces in the rendered text. For data-oriented XML (configs, feeds) this is almost never a problem. For document-oriented XML (XHTML, DocBook), be careful and verify the output reads the same.
CDATA sections
Content inside <![CDATA[ ... ]]> must be left completely untouched. A formatter that indents the inside of a CDATA block is corrupting your data. Good tools treat CDATA as opaque.
Entities and encoding
Reserved characters — &, <, > — have to stay escaped. If your document declares <?xml version="1.0" encoding="UTF-8"?>, make sure whatever you use to format it doesn't mangle non-ASCII characters. Copy-pasting through a tool that assumes Latin-1 is a classic way to turn é into garbage.
Comments and processing instructions
Comments (<!-- -->) and the XML declaration should be preserved. Some stripped-down formatters drop comments as "noise." That's rarely what you want when you're reading someone else's config.
It has to be well-formed first
Formatting is not validation against a schema, but most formatters do require the input to be well-formed — every tag closed, properly nested, one root element. If you get an error, that's often a genuine problem in the source: a missing </tag>, an unescaped ampersand, or two root elements. Treat the error as a free syntax check.
A worked example
Say an endpoint hands you this and you need to find the order total:
<order id="A-7781"><customer><name>Jo Blake</name></customer><items><item sku="X1" qty="2"><price>9.99</price></item><item sku="X2" qty="1"><price>19.50</price></item></items><total>39.48</total></order>Formatted, the shape is obvious: a customer, two line items, and a total.
<order id="A-7781">
<customer>
<name>Jo Blake</name>
</customer>
<items>
<item sku="X1" qty="2">
<price>9.99</price>
</item>
<item sku="X2" qty="1">
<price>19.50</price>
</item>
</items>
<total>39.48</total>
</order>Now you can see the two prices (9.99 × 2 and 19.50) and sanity-check the total against the line items in seconds.
Doing it in code
If you need it programmatically, most languages ship a serializer. In Python with lxml:
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.fromstring(raw_xml.encode(), parser)
print(etree.tostring(tree, pretty_print=True).decode())The remove_blank_text=True step matters — without it, existing insignificant whitespace confuses the pretty-printer and you get inconsistent indentation.
When you just want it done
For a quick one-off — paste, format, copy — a browser tool beats writing a script. Our XML Formatter handles indentation, preserves CDATA and comments, and flags malformed input so you're not guessing why something won't parse. It's the fastest path when you're mid-debug and just need to read a payload.
A few habits worth keeping
- Pick one indent style and stick to it. Two spaces is the common default; whatever you choose, be consistent across a repo.
- Format before committing generated XML. Line-by-line diffs stay meaningful, and reviewers thank you.
- Don't reformat files that are version-controlled by a tool that owns their layout. Some generators rewrite their own files; reformatting just creates diff churn.
- Keep a minify option handy too. For config you ship or payloads you send over the wire, strip the whitespace back out.
XML isn't glamorous, but it's not going anywhere. Spend thirty seconds formatting it properly and the rest of your debugging session gets a lot shorter.
Photo by Ilya Pavlov on Unsplash