DEVELOPER PRECISION ENGINE

XML Formatter

Format and beautify XML data with proper indentation. Two previously-confirmed bugs are now fixed: an element containing direct text content (e.g. <price>19.99</price>) no longer throws off the indentation of subsequent closing tags — the bug that affected most real-world XML, not just rare edge cases — and a CDATA section containing a literal > character (common in script snippets using comparison operators) is now correctly preserved as one unbroken block instead of being split and corrupted mid-content.

How to Use This Tool

1

Paste your XML data into the input textarea.

Paste your XML data into the input textarea.

2

Click "Format" to beautify or "Minify" to compress

both buttons are safe to click, since they replace the input with valid, re-formattable XML rather than corrupting it (unlike some sibling tools in this category).

Pro Tip

CDATA sections (used to embed raw HTML or script content that shouldn't be XML-parsed) are now preserved byte-for-byte between <![CDATA[ and ]]>, including any > characters inside — such as comparison operators in an embedded script snippet — without reindenting, escaping, or splitting the content.

About This Tool

When to Use

  • An enterprise integration developer, Cormac, formats a minified SOAP request XML dumped from a network trace to read the nested namespace structure while debugging a failed call, trusting the indentation to correctly reflect nesting depth even with text values like an id element's number.
  • An Android developer, Yolanda, formats a layout.xml file that got minified by a build tool artifact, restoring readable indentation before making a manual UI tweak.
  • A healthcare IT specialist, Ademola, formats an HL7 FHIR XML patient record export to visually verify all required clinical fields are present before an audit.

Key Details

An XML formatter reindents minified or single-line XML markup with nested tag indentation and line breaks between elements, making deeply nested document structures like SOAP envelopes, RSS feeds, or ...

Worked example

Cormac pastes a single-line SOAP envelope: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetUser><id>42</id></GetUser></soap:Body></soap:Envelope>. The output now correctly opens each element one level deeper than its parent, and the text node "42" inside <id> no longer throws off the count — </id> correctly lines up one level back from "42", and every closing tag after it (</GetUser>, </soap:Body>, </soap:Envelope>) correctly matches its opening tag's indentation, with </soap:Envelope> ending up back at the left margin exactly where <soap:Envelope> started.

Interpreting your result

A "Valid XML" badge with a node count means the browser's own DOMParser accepted the document. Indentation is now reliable for both structural and text-containing XML — closing tags correctly match their opening tags' depth throughout the document, including around CDATA sections containing > characters. An "Invalid" badge with an error message means the XML itself has a real structural problem (mismatched tags, invalid characters).

XML Formatter FAQ

Does the formatter produce correct indentation for all XML?

Yes, now genuinely — this was previously a confirmed bug where <root>Hello</root> formatted with </root> incorrectly indented one level deeper than <root>, because the text node "Hello" added an indent level with no matching closing tag to remove it again, an effect that compounded with every text-containing element in a document. That's fixed: a direct text node no longer increments the indent level, so closing tags correctly line up with their opening tags regardless of how much element text the document contains — verified against both simple cases and a realistic SOAP envelope example.

Does this tool validate XML?

The formatter attempts to indent valid XML and uses the browser's own DOMParser to flag malformed XML (mismatched tags, invalid characters) with an error message. That validation is genuinely separate from indentation — both are now reliable.

What XML features are supported?

The formatter handles XML declarations, nested elements, attributes on any element, self-closing tags, comments, CDATA sections, and processing instructions correctly. Mixed content — elements that contain direct text alongside or instead of child elements — is now handled correctly too, following the indentation fix.

Can this handle XML namespaces?

Yes. XML namespaces (xmlns attributes) are preserved exactly as provided, since the formatter treats them as standard attributes and doesn't modify namespace declarations or prefixes.

What is the difference between Format and Minify?

Format adds indentation and line breaks for readability. Minify removes whitespace between tags to produce more compact XML. Both buttons are safe to use repeatedly — clicking Format or Minify replaces the input with the tool's own valid output rather than breaking it.

Can the XML formatter handle CDATA sections?

Yes, now fully, including a previously-confirmed gap that's fixed: a CDATA section containing a literal > character (e.g. <![CDATA[if (a > b) { return true; }]]>, valid and common in CDATA-wrapped script snippets) used to get incorrectly split at that >, inserting a newline mid-content and corrupting the raw payload the CDATA block exists to protect. The tokenizer now recognizes a CDATA section's actual boundary — the literal ]]> terminator — and treats everything up to that point as one opaque, unbroken block, regardless of any > characters inside it.

Master Your Entire Toolkit

Access 500+ specialized calculators and tools designed for precision. No sign-up required, 100% free.

Built and reviewed by the Toolzip Team · Based on W3C XML Specification