What XML Formatter does
This XML indenter puts every tag on its own line and indents it by its depth in the tree, using 2 or 4 spaces per level. An element that holds only text, such as <title>Dune</title>, stays on a single line so lists of simple fields remain compact. The XML declaration, DOCTYPE and comments are kept, each on a line of their own. Tick Minify instead of format to go the other way and strip the whitespace between tags. It works on any XML-based text: RSS and Atom feeds, sitemaps, SOAP envelopes, SVG and config files. It re-indents by pattern and does not validate, so it will not report a document that is not well-formed.
XML indentation with 2 spaces and 4 spaces
Input: <catalog><book id="1"><title>Dune</title><year>1965</year></book></catalog>. Each row is one line of the output and the number of spaces in front of it.
| Output line | Depth | Leading spaces at 2 | Leading spaces at 4 |
|---|---|---|---|
| <catalog> | 0 | 0 | 0 |
| <book id="1"> | 1 | 2 | 4 |
| <title>Dune</title> | 2 | 4 | 8 |
| <year>1965</year> | 2 | 4 | 8 |
| </book> | 1 | 2 | 4 |
| </catalog> | 0 | 0 | 0 |
Total size is 92 characters with 2 spaces and 104 characters with 4 spaces. The tool reports 4 opening tags and 4 closing tags for both.
Indenting common XML documents
Each input was pasted as a single line and formatted with 2 spaces.
RSS feed
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>News</title><item><title>First post</title><link>https://example.com/1</link></item></channel></rss>
10 lines. The declaration and <rss> sit at depth 0, <channel> at 1, the channel <title> and <item> at 2, and the item's <title> and <link> at 3. Opening tags 6, Closing tags 6, Size 210 chars.
XML sitemap
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://example.com/</loc><lastmod>2026-01-15</lastmod></url></urlset>
7 lines. <url> is indented 2 spaces and <loc> and <lastmod> 4 spaces. Opening tags 4, Closing tags 4, Size 196 chars.
SOAP envelope with a namespace prefix
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetPrice><Item>Apples</Item></GetPrice></soap:Body></soap:Envelope>
7 lines, with <Item>Apples</Item> at depth 3. Size 173 chars with 2 spaces and 191 chars with 4 spaces.
Prefixes and xmlns attributes are left exactly as written.
SVG icon
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g fill="none"><circle cx="12" cy="12" r="10"/><path d="M8 12h8"/></g></svg>
6 lines: <svg>, <g> at depth 1, <circle/> and <path/> at depth 2, then the two closing tags. Opening tags 2, Closing tags 2, Size 153 chars.
Self-closing tags are not included in either count.
Minifying an indented file
<catalog>, <book id="1">, <title>Dune</title>, </book> and </catalog> on five indented lines, with Minify instead of format ticked
<catalog><book id="1"><title>Dune</title></book></catalog> on one line. Size 58 chars.
What the formatter changes, and where to check the result
The tool splits the text at tag boundaries. That is fast and predictable, but it is not a full XML parser.
- Whitespace between tags is replaced by the new line breaks and indentation.
- Spaces at the start and end of an element's text are trimmed: <name> padded </name> comes out as <name>padded</name>. If that padding matters to your application, do not reformat that document here.
- Mixed content is broken up. <p>Hello <b>bold</b> world</p> becomes five lines, with Hello, <b>bold</b> and world each on their own line.
- An empty pair such as <empty></empty> is written as an opening tag and a closing tag on two lines.
- A > character inside an attribute value, a comment that contains tags, or a CDATA section that contains > is split in the wrong place. Review those sections after formatting.
- Attributes always stay on the same line as their tag, in their original order and quoting.
- Minify removes only the whitespace between a > and the next <. Comments and the text inside elements are kept.
- Indentation is 2 or 4 spaces. Tabs are not offered.
What the opening and closing tag counts tell you
The Summary counts opening tags and closing tags separately. In a well-formed document made of ordinary elements the two numbers are equal, so a difference is a quick hint that something was not closed. The sample catalog with its </book> deleted reports 4 opening tags and 3 closing tags.
Treat the figures as a hint and not as validation. Self-closing tags, comments, the XML declaration and DOCTYPE are left out of both counts. An opening tag made of a single character with no attributes, such as <g> or <p>, is also not counted, so SVG and XHTML fragments can show fewer opening tags than closing tags even when they are correct. A closing tag with the wrong name, as in <item>x</itme>, still counts as one closing tag, so the totals match and the mistake passes unnoticed.
To check that a document is well-formed, open it in a browser or run it through an XML parser such as xmllint. To check it against a schema you need a validating parser and the XSD or DTD.
Other ways to indent or tidy XML
For files on disk, large documents or automated jobs, a parser-based tool is the better fit.
| Tool | Command or action | Notes |
|---|---|---|
| xmllint (libxml2) | xmllint --format file.xml | Indents with 2 spaces by default and reports documents that are not well-formed |
| XMLStarlet | xmlstarlet fo -s 4 file.xml | The -s option sets the number of spaces per level |
| HTML Tidy | tidy -xml -i file.xml | The -xml flag treats the input as XML, -i turns on indentation |
| Python | python3 -c "import sys, xml.dom.minidom as m; print(m.parse(sys.argv[1]).toprettyxml(indent=' '))" file.xml | Uses the standard library, no install needed |
| Code editors | The Format Document command | Most editors need an XML extension or plugin installed for this |
Step by step
- 1
Paste your markup into the XML box.
- 2
Set Indent size to 2 spaces or 4 spaces.
- 3
Leave Minify instead of format unticked to indent, or tick it to remove the whitespace between tags.
- 4
Read the Formatted XML (or Minified XML) output and the Summary of Opening tags, Closing tags and Size.
- 5
Copy the result or download it as document.xml.