XML formatter

Format XML instantly

Paste XML, choose indentation and collapse behavior, then copy the formatted output

Last updated: February 7, 2026
Frank Zhao - Creator
CreatorFrank Zhao
Collapse content :
Indent size :
<hello>
  <world>foo</world>
  <world>bar</world>
</hello>

Introduction / overview

The XML formatter takes raw XML on the left and produces a consistently-indented, easy-to-read version on the right. It’s ideal for debugging, code reviews, and quickly understanding a nested document.

Use it when your XML is hard to scan — especially when tags are nested several levels deep or when you need clean diffs in git.

What the options mean

  • Indent size controls how many spaces are used per nesting level.
  • Collapse content keeps simple text nodes compact so your output doesn’t explode into dozens of lines.

If your next step is converting data formats, try our JSON prettify tool for readable payloads.

How to use / quick start

1

Paste your XML

Paste raw XML into the left input. You can format tiny snippets or full documents.

2

Choose options

Set the indent size and decide whether to collapse text content.

3

Copy the formatted output

Use the copy button on the right to paste the result into your editor, ticket, or PR comment.

A quick way to reason about indentation

If your indent size is dd spaces and a tag is nested LL levels deep, the leading spaces are roughly:

S=dLS = d\,L==2×32\times 3==66

This isn’t “math you must do” — it’s just a mental model for why a larger indent can make deep XML very tall.

Step-by-step examples

Example 1: Compact snippet → readable XML

Input:<hello><world>foo</world><world>bar</world></hello>

Set Indent size to 22 and keep Collapse content enabled. The output becomes easy to scan line-by-line.

Example 2: Deep nesting with a larger indent

If you choose d=4d=4 spaces and your data is nested L=5L=5 levels, you’ll see up to S=dL=20S=d\,L=20 leading spaces at the deepest level.

S=dLS = d\,L

Practical takeaway: for very deep documents, a smaller indent usually stays more readable.

Real-world examples / use cases

Debugging SOAP or legacy APIs

Background: you receive a dense XML response in logs. Inputs: paste the response, set d=2d=2. Result: the formatted XML reveals the exact nested field that differs.

Reviewing XML config changes

Background: a config file was edited by multiple people. Inputs: format both versions to normalize whitespace. Result: diffs focus on semantic changes, not messy formatting.

Preparing examples for documentation

Background: you’re writing a README or a support article. Inputs: format snippets so indentation is consistent. Result: readers understand structure faster.

Quick conversions after formatting

Background: you want to compare XML data with JSON. After formatting, it’s often easier to extract fields or copy segments into other tools like JSON prettify.

Common scenarios / when to use

Unreadable single-line XML
Turn “one long line” into a readable tree.
PR reviews and diffs
Normalize whitespace before comparing versions.
API payload troubleshooting
Spot missing nodes or wrong nesting quickly.
Sharing snippets in tickets
Send formatted examples that teammates can understand fast.
Cleaning exported data
Make vendor exports readable before processing.
Learning XML structure
See how elements and nesting relate at a glance.

Not a good fit: if you need to validate against an XSD schema or canonicalize XML for cryptographic signatures, you’ll need specialized tooling.

Tips & best practices

  • Pick a consistent indent size (commonly 22 or 44).
  • Keep “Collapse content” on when your XML has many short text nodes (it reduces noisy line breaks).
  • When you see “Provided XML is not valid”, look for unescaped &\& or mismatched closing tags.
  • For huge documents, format smaller slices (a subtree) to keep your browser snappy.

Calculation method / formatting rules

Formatting is mainly about whitespace: newlines and indentation. The formatter reads the XML structure (elements, nesting, and text nodes) and then outputs the same structure with consistent spacing.

Indentation model

For a given nesting level LL and indent size dd:

S=dLS = d\,L

Where SS is the count of leading spaces before the tag.

What “collapse content” does (in plain terms)

When enabled, simple text-only nodes are kept compact so you don’t get extra whitespace in places where it doesn’t help readability.

Related concepts / background

Whitespace in XML

In many XML contexts, whitespace between elements is not semantically important — but in mixed-content documents it can be. That’s why “collapse content” is a preference: it optimizes for readability, not every edge case.

Frequently asked questions

Does formatting change my XML data?

It changes whitespace and line breaks. The tag structure and text content should remain the same for typical XML.

Why do I see an “XML is not valid” error?

Common causes include missing closing tags, unescaped characters like &\&, or an incomplete copy/paste.

What indent size should I use?

Most teams use 22 or 44 spaces. Smaller indents can be easier for deep XML.

Can I share my formatted result?

Yes — use the Share button below the calculator. You can choose whether to include your inputs/results.

Is my XML uploaded anywhere?

This formatter runs locally in your browser for formatting. Avoid sharing sensitive data in public links.

Limitations / disclaimers

  • Formatting optimizes readability, not canonicalization for signatures.
  • Whitespace can matter for certain mixed-content documents; review before using output in strict pipelines.
  • Do not use this tool as a substitute for schema validation (XSD) when correctness is critical.

External references / sources

XML formatter | CalculatorVast