Free Converter

XML Formatter & Validator

Format, prettify, and validate XML instantly in your browser. No upload required — completely private and free.

Drag & Drop an XML file here

Supports .xml files. Or paste your XML in the area above.

Or

About XML Formatting

XML formatting indents nested elements so the document hierarchy becomes visible. Compact XML — everything on one line — is technically valid and useful for transmission, but unreadable for humans. Formatted XML adds line breaks between elements and indents children inside their parents, making it scannable, diffable, and editable by hand.

This formatter parses the input using DOMParser to confirm well-formedness, then walks the resulting DOM to emit indented output. Comments, processing instructions, and CDATA sections are preserved. Self-closing tags (those without children) are kept compact on a single line; tags with children are spread across multiple lines with the children indented.

Two-space indentation is the default, matching the most common XML convention. The formatter is conservative about reformatting: existing structure is preserved, only whitespace between elements changes. The data round-trips through any XML parser identically before and after formatting.

Why Format XML

Compact XML is unreadable. SOAP responses, RSS feeds, and many configuration files arrive as single-line XML that is technically valid but practically opaque. Formatting reveals the document structure, lets you locate specific elements, and makes diffs in version control meaningful.

Formatting also serves as a validity check. If the XML fails to parse during formatting, the error message identifies the problem — usually unclosed tags, mismatched element names, or invalid characters. Catching XML errors before sending to a strict consumer saves debugging time.

How to Format XML

Paste XML, click format.

  1. Add XML input: Paste XML text or drop a .xml file. The formatter accepts any well-formed XML — documents, fragments, RSS feeds, SOAP envelopes.
  2. Choose indentation: Default is 2 spaces. Use 4 spaces or tabs if your team's convention prefers them.
  3. Format: DOMParser builds a DOM, the formatter walks it, and indented XML is emitted. Comments, CDATA, and processing instructions are preserved exactly. Whitespace inside text nodes is preserved (significant whitespace cannot be safely added or removed).
  4. Copy or download: Copy the formatted XML to clipboard or save as .xml. The output is well-formed and parses identically to the input.

Common Use Cases

Technical Details

DOMParser produces an XML DOM. The formatter walks the tree depth-first, emitting opening tags, indented children, and closing tags. Element attributes are preserved on the opening tag; attribute order matches the source.

Significant whitespace inside text nodes is preserved — the formatter cannot safely change content whitespace because it may be meaningful (XML space preservation rules differ across schemas). Only whitespace between elements changes.

Self-closing elements (no children) emit as <tag attr="val"/>. Elements with only text content emit as <tag>text</tag>. Elements with element children emit on multiple lines with indented children. CDATA sections, comments, and processing instructions emit with their original delimiters and content.

Best Practices

Frequently Asked Questions

Will the formatted XML parse identically to the source?
Yes. The data round-trips through any XML parser unchanged. Only inter-element whitespace differs.
What indentation should I use?
2 spaces is the most common XML convention. 4 spaces or tabs are also acceptable; pick what your team uses.
Are comments and CDATA preserved?
Yes. Comments, CDATA sections, and processing instructions are preserved exactly. Their content is not modified during formatting.
What happens with malformed XML?
DOMParser produces a parse error. The formatter cannot guess at the intended structure of malformed input. Fix the source XML and try again.
Are namespaces preserved?
Yes. xmlns and xmlns:prefix attributes are preserved on the elements where they appear in the source.
Can I minify XML to a single line?
Some XML formatters offer minification. This tool focuses on formatting; minified XML can be produced by stripping whitespace between elements with a regex or dedicated tool.
Is my XML uploaded to a server?
No. DOMParser runs in your browser; the formatter runs in your browser.
Will whitespace inside text nodes change?
No. Text-node whitespace is preserved because XML space-preservation rules vary by schema. The formatter changes only inter-element whitespace.