How XML Indentation Works (and Why It Matters)
Whitespace between XML elements is usually ignored by parsers, but inside elements it can be data. Here's what's actually happening when you indent XML.
Whitespace in the XML specification
The XML 1.0 specification treats whitespace more carefully than JSON does. The four whitespace characters — space (U+0020), tab (U+0009), carriage return (U+000D), and line feed (U+000A) — can appear between markup such as tags, and in most documents this inter-element whitespace carries no meaning. When an element contains only child elements, the spaces and newlines between those children are there purely for human readability.
The important difference from JSON: in XML, whitespace inside an element that holds text is significant by default. The content <name>Alice Smith</name> contains a space that is part of the data, and a parser must preserve it. This is why blindly adding indentation to XML is riskier than with JSON — you can change the data if you reformat the inside of a text element.
XML also gives authors explicit control with the xml:space attribute. Setting xml:space="preserve" on an element tells processors to keep all whitespace inside it exactly as written — useful for things like code samples or poetry embedded in XML.
The two extremes: minified and formatted
Consider this XML describing a user, with all insignificant whitespace removed:
Minified — one line:
<user><name>Alice</name><age>30</age><roles><role>admin</role><role>editor</role></roles></user>
Formatted with 2 spaces:
<user>
<name>Alice</name>
<age>30</age>
<roles>
<role>admin</role>
<role>editor</role>
</roles>
</user>
Both parse to the same element tree. Notice the formatter put each <role> on its own line but kept the text admin tight against its tags — it indents element-only content but never reflows text content, because that text could be significant.
How indentation scales with nesting depth
Each nesting level adds one unit of indentation. With 2-space indentation, the root has zero leading spaces, its children have 2, grandchildren 4, and so on. XML tends to nest deeply — namespaced configuration files and SOAP envelopes routinely reach 6–10 levels — so the indent unit compounds quickly. At depth 8 with 4-space indentation, the innermost elements already carry 32 leading spaces before any content appears.
Because XML markup (the tags themselves) is already verbose, many developers prefer 2-space indentation to keep deeply nested documents readable on a normal screen width. Four-space indentation is still common in Java and .NET ecosystems where it matches the surrounding source code style.
How XML formatters add indentation
A formatter doesn't add spaces to raw text — it has to understand the document structure first. The process is always:
- Parse the XML into an in-memory node tree (elements, attributes, text nodes, comments, processing instructions).
- Serialize the tree back to a string, inserting line breaks and indentation between element nodes while leaving text content intact.
In the browser this maps directly to DOMParser for the parse step and XMLSerializer (plus a recursive walk that adds indentation) for the output. Because the document is fully parsed first, the parse step is also where malformed XML is caught — a formatter that won't format your input is usually telling you the XML isn't well-formed.
Formatting also normalizes incidental details: attribute quoting becomes consistent, self-closing vs. empty-element forms may be standardized (<br></br> → <br/>), and redundant inter-element whitespace is replaced with the chosen indent.
Choosing an indentation style
- 2 spaces: Common for XML in web tooling, config files, and VS Code. Keeps deeply nested, verbose XML readable.
- 4 spaces: The Visual Studio default and common in Java/.NET projects, where it matches surrounding source.
- Tab: Lets each developer pick their own display width; sometimes preferred in mixed-language repositories.
As with any code formatting, consistency matters more than the specific choice. If the XML lives alongside source in a repository, follow whatever the project's .editorconfig already enforces.
Indent your XML instantly
Paste any XML and choose your indent size — 2 spaces, 4 spaces, or tab. Formats automatically and flags errors as you type.
Open XML Formatter →Frequently Asked Questions
Does indentation change the meaning of an XML document?
Usually not. Whitespace between elements is normally insignificant. But whitespace inside an element with text content is significant by default, so a formatter that reflows text could change the data. Good formatters only re-indent element-only content and leave text and mixed content untouched.
What is the standard indentation for XML?
There is no single mandatory standard. Two spaces is common in web and config contexts; four spaces is common in Java and .NET projects. Visual Studio defaults to four spaces, while VS Code and most web tooling default to two.
Why does my formatted XML have different whitespace than the original?
A formatter parses the XML into a node tree, drops insignificant inter-element whitespace, and re-serializes with consistent indentation. The output is functionally identical for element-only content but the original layout is replaced. Use xml:space="preserve" to keep significant whitespace.
Can I indent XML that has mixed content?
Mixed content — text and child elements together inside one element, like an HTML paragraph with inline tags — generally should not be re-indented, because the surrounding text is significant. A safe formatter detects mixed content and leaves it on a single logical block rather than breaking it across indented lines.