DeveloperMar 25, 2026 ยท Updated Oct 13, 2026 ยท 3 min read

How to Format and Pretty-Print XML

How to pretty-print XML without changing its data. We ran our formatter on seven risky documents (mixed content, xml:space, CDATA, namespaces, SVG text) and re-parsed the results: all seven kept identical data.

๐Ÿ“„
Try the XML Formatter
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01Why Format XML02Pretty-Printing Without Changing the Data03The Errors That Stop a Parser

Why Format XML

Feeds, sitemaps, SOAP responses, SVG exports and Office files (a .docx is a ZIP of XML parts) often arrive as one long line. Indentation shows the nesting, which is what you need to find an element, spot a misplaced closing tag or diff two versions.

Pretty-Printing Without Changing the Data

Formatting adds whitespace between elements. Most of the time that whitespace is meaningless, but XML 1.0 says a parser must pass all text, including whitespace, on to the application, so a careless formatter can change content. The risky cases are mixed content (text and elements together, as in SVG <text> or XHTML paragraphs), elements marked xml:space="preserve", and CDATA sections.

We ran the XML Formatter's own serializer in Chromium on seven such documents and re-parsed every result with lxml, comparing it with the input after removing whitespace-only text (October 13, 2026; cases and script in our test folder):

CaseWhat the formatter didSame data?
Mixed content: <p>Hello <b>world</b>, and <i>you</i>!</p>kept the paragraph on one lineyes
xml:space="preserve" with leading spaces and a line breakleft the element's content untouchedyes
CDATA, a comment and a processing instructionkept all three as writtenyes
Entities and quotes in text and attributesre-escaped them correctlyyes
Default and prefixed namespaceskept both declarations and prefixesyes
SVG <text> with an inline <tspan>kept it inline (spaces around B survive)yes
RSS item with escaped HTML in the descriptionindented, escaping unchangedyes

All seven came back with an identical element tree, attributes and text, and the output was character-for-character identical to xmllint --format (libxml2 2.9) on all seven, apart from xmllint adding an XML declaration. If you use another formatter, test it on a mixed-content sample first: a formatter that puts <b>world</b> on its own line has changed the text.

The Errors That Stop a Parser

XML parsers must stop at the first well-formedness error rather than guess. The usual culprits:

The formatter reports the parser's error instead of formatting a broken document. The options it does have are indentation (2 spaces, 4 spaces or tabs), minify, and syntax highlighting.

Frequently Asked Questions

How do I format XML online?+
Paste it into the XML Formatter and choose 2 spaces, 4 spaces or tabs. It parses the document first, so a well-formedness error is reported instead of formatted, and it leaves mixed content, xml:space="preserve" elements and CDATA as they are.
Is XML still used in 2026?+
Yes. XML remains essential for RSS/Atom feeds, SOAP web services, SVG graphics, Android UI layouts, Microsoft Office file formats (.docx is XML), and enterprise data exchange. JSON has replaced it for most APIs, but XML is far from obsolete.
What is the difference between XML and HTML?+
XML is strict โ€” every tag must be closed, attribute values must be quoted, and tag names are case-sensitive. HTML is forgiving and has predefined elements. XML allows custom element names; HTML has a fixed vocabulary.
Try it yourself

Use the XML Formatter โ€” free, no signup required.

โšก Open XML Formatter
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.