XML Formatter

Converter

Pretty-print and validate XML with custom indentation and line-precise errors for unclosed tags. Handles declarations, CDATA blocks and namespace prefixes.

XMLInput
XMLOutput

About XML Formatter

XML is notoriously verbose, and output from APIs, logs, or legacy services usually arrives minified onto a single dense line. This tool formats (pretty-prints) and minifies XML entirely in your browser, and validates that tags are properly closed and nested so you catch malformed documents before they reach production. It handles XML declarations, processing instructions, CDATA blocks, namespace prefixes, comments, and self-closing tags. One important distinction: it performs well-formedness validation only — it will not enforce field types or business rules defined in an XML Schema (XSD) or DTD, which is the separate realm of validity checking. When a parse error occurs, the reported line and column help you locate the problem quickly. You can choose 2-space, 4-space, or tab indentation and switch between beautify and minify modes. Namespace prefixes are preserved so prefixed and default-namespaced children render as expected. Because everything runs locally, nothing is uploaded — safe for SOAP payloads, SVG files, Maven POMs, and Spring configs that contain sensitive data.

Whitespace is content here rather than decoration, the fundamental difference from most data formats. In one document, indentation between elements is ignorable whitespace, while a space separating words inside an element and a newline in mixed content can be significant. Confirm whether the document is whitespace-sensitive before formatting; reflowing such a document changes its meaning.

Special blocks need separate attention because their contents do not follow normal parsing rules. Text inside a literal block is preserved verbatim and any reflow breaks it, and comments behave the same way: harmless to parsing, yet moving one can change its intent, as with conditional comments. A formatter should keep these blocks intact and adjust indentation only around them.

Attribute order and namespaces are easily disturbed. Attributes are semantically unordered, so reordering does not affect parsing but floods a version diff with irrelevant changes, and a changed namespace prefix keeps the meaning yet breaks expressions that reference it. Where formatted output goes into version control, keep attribute order stable and avoid mixing formatting churn into real edits.

Compared with common data exchange formats, this one expresses documents with attributes and mixed content rather than plain data. In pure data work the alternative syntax is terser and parses more directly, while the markup form wins when a structure needs both text content and descriptive attributes, or must embed large verbatim blocks. Choose by structure, not by habit.

How large documents are handled decides usability. Full parsing builds the whole tree in memory and struggles past a few hundred megabytes, while streaming reads event by event with stable memory but cannot reflow globally. For very large documents formatting is often unnecessary; where indentation is required, pick a streaming implementation and accept that alignment cannot be global.

Finally, check well-formedness before beauty. Unclosed tags, unquoted attributes or undeclared namespaces stop strict parsing, and indenting then merely beautifies a broken structure while hiding the real problem. The sensible order is to validate the structure, confirm it is well-formed, and only then format; reversing it makes a bad file look fine.

How It Works Internally

Formatting re-indents by element nesting. The catch is that whitespace in XML can be data — spaces and newlines inside element content are preserved verbatim — so automatically reflowing a document with significant whitespace alters its content. Confirm no significant whitespace exists, then compare node text with a parser afterwards.

Input<a><b>1</b><b>2</b></a>
Output<a> <b>1</b> <b>2</b> </a> (node text unchanged)

Measured on This Page

CDATAThe CDATA section holding < and && keeps its content verbatim and is not re-escaped
Comments<!-- comment --> survives serialisation instead of being dropped
Entities and attributes&amp; in an attribute and &lt; in text come back as x & y and 5 < 6 — no double escaping to &amp;amp;
Chinese text<名称>码屋工具箱</名称> (Chinese tag name and text) content is intact with indentation added only between elements
Self-closing<br/> and <img src='a.png'/> stay self-closing; single-quoted attributes normalise to double quotes (semantically equal)
ImplementationDOMParser plus recursive serialisation (element/text/comment/cdata/PI) rather than string regex — which is why comments and CDATA survive

Two classic indentation accidents: double-escaped entities (re-escaping text already parsed to & produces &amp;amp; in the file) and silently dropped structure (regex-based formatters swallow comments, CDATA and processing instructions). This page goes through DOM parse and serialise, and all 5 structures survive. Note the trade-offs: attribute quotes normalise to double quotes and namespace prefixes are left untouched (no ns0 renaming) — semantically equal, but string comparisons downstream would see a difference.

How this was measured: Five XML inputs (CDATA, comment, entities with attributes, Chinese text, self-closing tags) were indent-formatted, then output and source were parsed with ElementTree and compared node by node (tag, attributes, text) ignoring indent whitespace. · Measured 2026-10-31

How to Use

  1. Open "XML Formatter"
  2. Paste the raw XML source
  3. Set the indent to 2, 3 or 4 spaces
  4. Confirm CDATA and comments are preserved
  5. Click the copy icon to take the formatted XML

Use Cases

  • Beautify API messages — Format single-line SOAP/XML responses into an indented hierarchy to locate nodes fast.
  • Validate well-formedness — Check that tags are paired and attribute quotes are complete to avoid runtime parse errors.
  • Reduce transfer size — Minify development-time formatted XML to a single line to cut bytes.
  • Analyze logs — Expand XML fragments copied from logs to inspect issues level by level.
  • Review configs — Format pom.xml, web.xml, and other configs for easier code review.
  • Compare XML diffs — Format both sides of an XML diff side-by-side to spot structural changes in config or payload files.
  • Strip comments — Minify XML to strip comments and whitespace, producing a compact payload for production messages.

FAQ

Can it validate against XML Schema (XSD)?

No. It only checks well-formedness — closed tags and correct nesting — not XSD/DTD-based validity.

How many spaces for indentation?

Two spaces by default, producing clean, consistent output suited to most styles.

Are CDATA sections damaged?

No. CDATA content is preserved verbatim, without escaping or line wrapping.

Why a format error?

Common causes: unclosed tags, missing attribute quotes, multiple root nodes, or unescaped special characters. The error gives a location.

What is the difference between DTD and a Schema?

DTD is XML's early mechanism for declaring document structure and entities (verbose and limited typing); XSD describes structure in XML itself and supports data-type constraints. The tool validates neither against business rules.

Want JSON?

Use our XML to JSON tool.

Is there an XXE risk when parsing XML here?

No. An XXE (XML External Entity) attack happens when a parser follows DOCTYPE external entities to load local files or make unintended network requests, which can leak file contents or be abused for SSRF. This tool parses entirely in your browser via DOMParser, makes no network requests, and never loads DTD external entities, so there is no XXE or data-exfiltration risk here. That said, when you parse untrusted XML on your own server or application, explicitly disable external entity loading (for example, turn off NOENT and DTDLOAD in libxml) so you don't carry that class of risk into your production code.

There is a blank line before the XML declaration and it errors — why?

The XML spec requires the declaration to be the very first thing in the file with no preceding whitespace. Keep the declaration on line one and format only what follows; if a blank line already exists, deleting it fixes the parse error.