</> XML Formatter, Beautify and Minify XML Online
By Shihab Mia ยท Updated 2026-07-06
This XML formatter takes raw, unindented XML and pretty-prints it with clean, nested indentation so the document structure is instantly readable, or minifies it down to a single line by stripping the whitespace between tags. You pick the indent style (2 spaces, 4 spaces, or a tab) and the tool re-emits every element on its own line, increasing the indent after each opening tag and decreasing it before each closing tag. It also runs a quick well-formedness check and warns you if your opening and closing tags are unbalanced, and everything runs locally in your browser so your data is never uploaded.
What is the XML Formatter?
XML (Extensible Markup Language) stores data in a tree of nested elements, but the indentation that makes that tree readable is not part of the data. Parsers ignore the whitespace between tags, so a valid document can arrive as one long unbroken line. That is efficient for machines and for transfer over a network, but almost impossible for a human to read or debug. An XML formatter, also called an XML beautifier or pretty printer, adds that whitespace back in a consistent way so you can see at a glance which element contains which.
The beautify logic works by tokenizing the input into a stream of pieces: opening tags, closing tags, self-closing tags, text content, comments, CDATA sections, and the XML declaration. It then walks that stream while tracking an indent depth. Every opening tag is printed at the current depth and then the depth increases by one, so its children are indented further in. Every closing tag first decreases the depth and then prints, so it lines up with its matching opening tag. Self-closing tags such as <br/> and the XML declaration <?xml ... ?> print at the current depth but do not change it, because they have no children. When an element holds only a short piece of text, like <to>Tove</to>, the text is kept on the same line as its tags instead of being split across three lines, which keeps leaf values compact and the output easy to scan.
Minifying is the reverse operation. The same tokenizer runs, but instead of adding newlines and indentation the formatter simply concatenates every tag back to back and drops the whitespace that sits purely between tags. Text content inside an element is trimmed but preserved. The result is the smallest faithful representation of the document, which is what you want when embedding XML in a config value, storing it in a database column, or shipping it in an API payload where every byte counts.
Beyond formatting, the tool does a light well-formedness check. As it tokenizes, it pushes every opening tag name onto a stack and pops it when it sees the matching closing tag. If a closing tag does not match the tag on top of the stack, or a tag is never closed, or a closing tag appears with nothing open, the tool shows a friendly warning naming the tag involved. This is not a full schema or DTD validation, it is a structural sanity check that catches the most common copy-paste and hand-editing errors. Importantly, the tool still formats your input even when it warns, so you can see the shape of the document and spot exactly where the mismatch is.
When to use it
- Reading and debugging an XML API response, RSS feed, or SOAP message that arrived as a single unbroken line.
- Cleaning up hand-edited configuration files such as web.xml, pom.xml, or a .csproj before committing them.
- Minifying an XML payload to shrink it for storage in a database column or transfer over a slow connection.
- Preparing a readable XML snippet to paste into documentation, a bug report, or a Stack Overflow question.
- Quickly spotting an unbalanced or mistyped tag in a document that a strict parser rejects with a cryptic error.
- Reformatting exported data (sitemaps, SVG, Android layout XML, XLIFF) to a consistent indent before diffing in version control.
How to use the XML Formatter
- Paste or type your XML into the input box; a sample note document is loaded so you can see the tool working right away.
- Pick your indent style from the dropdown: 2 spaces, 4 spaces, or a tab.
- Click Beautify to pretty-print with indentation, or Minify to collapse the document to a single line.
- Check the warning line; if any tags are unbalanced the tool tells you which one and still formats the output.
- Click Copy to put the formatted XML on your clipboard.
Formula & method
Worked examples
Beautify a one-line note document with 2-space indent.
- Input: <note><to>Tove</to><from>Jani</from><body>Call me</body></note>
- The tokenizer produces: open <note>, open <to>, text Tove, close </to>, open <from>, text Jani, close </from>, open <body>, text Call me, close </body>, close </note>.
- <note> prints at depth 0, then depth becomes 1. <to>Tove</to> is an open-text-close trio on one line, printed at depth 1.
- <from>Jani</from> and <body>Call me</body> each print as single lines at depth 1.
- The final </note> drops depth back to 0 and lines up with the opening <note>.
Result: <note> <to>Tove</to> <from>Jani</from> <body>Call me</body> </note>
Minify a formatted document back to one line.
- Input is the four-line indented <note> document from the previous example.
- The tokenizer sees the same tags plus the newline-and-space text that sits purely between them.
- Whitespace-only text between tags is dropped; real text such as Tove is kept and trimmed.
- All tokens are concatenated with nothing between the tags.
Result: <note><to>Tove</to><from>Jani</from><body>Call me</body></note>
How each token type affects indentation during beautify
| Token | Example | Prints at | Depth change |
|---|---|---|---|
| Opening tag | <note> | current depth | increases by 1 after |
| Closing tag | </note> | depth minus 1 | decreases by 1 before |
| Self-closing tag | <br/> | current depth | no change |
| XML declaration | <?xml version="1.0"?> | current depth | no change |
| Comment | <!-- note --> | current depth | no change |
| Short text child | <to>Tove</to> | same line as its tags | no change |
Indent options and the whitespace each one inserts per level
| Option | Characters per level | Best for |
|---|---|---|
| 2 spaces | two space characters | Compact code, most web and JS projects. |
| 4 spaces | four space characters | Deeply nested documents where clarity matters. |
| Tab | one tab character | Teams that indent with tabs, or accessibility. |
Common mistakes to avoid
- Expecting full schema validation. This tool checks that opening and closing tags are balanced and nested correctly. It does not validate against a DTD or XSD schema, so a document can pass the balance check and still break a strict schema rule such as a required attribute or a wrong data type.
- Confusing indentation with data. Beautifying adds whitespace that most parsers ignore, but a few whitespace-sensitive formats (for example XML that carries pre-formatted text) treat that whitespace as content. If exact spacing inside an element matters, minify instead of beautify, or keep the original.
- Assuming an unbalanced-tag warning means the tool failed. The warning is informational. The formatter still indents or minifies your input so you can see the structure and locate the mismatch. Fix the tag it names, then re-run to clear the warning.
- Forgetting that self-closing tags do not nest. A tag like <img src="x"/> is self-closing, so it never increases the indent depth. If your output looks over-indented, check that tags you meant to self-close actually end in /> rather than being left open.
Glossary
- XML
- Extensible Markup Language, a text format that stores data as a tree of nested tagged elements.
- Beautify
- To re-print XML with newlines and indentation so its nested structure is easy to read. Also called pretty printing.
- Minify
- To strip the whitespace between tags so the document becomes the smallest single-line version of itself.
- Well-formed
- XML that follows the basic syntax rules: every opening tag has a matching closing tag and elements are properly nested.
- Self-closing tag
- An element with no children written in one tag ending in a slash, such as <br/>. It does not change indent depth.
- CDATA
- A section wrapped in <![CDATA[ ... ]]> whose contents are treated as raw text, not markup, so it can contain characters like < and &.
Frequently asked questions
What does an XML formatter do?
An XML formatter takes raw XML and re-prints it with consistent indentation so its nested structure is readable (beautify), or strips the whitespace between tags to collapse it to a single line (minify). It reformats the layout without changing the data the tags contain.
How do I beautify XML online for free?
Paste your XML into this tool, choose an indent style of 2 spaces, 4 spaces, or a tab, and click Beautify. The formatter adds a new line for each element and indents children under their parent, then you click Copy to grab the result. It runs entirely in your browser at no cost.
What is the difference between beautifying and minifying XML?
Beautifying adds newlines and indentation to make the document human-readable, which increases its size. Minifying removes the whitespace between tags to make the document as small as possible for storage or transfer. Both keep the underlying data identical; only the layout changes.
Does this XML formatter validate my XML?
It runs a light well-formedness check that verifies your opening and closing tags are balanced and correctly nested, and warns you if they are not. It does not perform full DTD or XSD schema validation, so it will not check attributes, data types, or required elements.
Is my XML uploaded to a server?
No. All formatting happens locally in your browser using JavaScript. Your XML is never sent anywhere, which makes the tool safe to use with private or sensitive documents.
Can I choose tabs instead of spaces for indentation?
Yes. The indent dropdown offers 2 spaces, 4 spaces, or a single tab character per level. Pick the one that matches your project style and the beautified output uses it throughout.
Why does short text stay on the same line as its tags?
When an element contains only a short piece of text with no child elements, such as <to>Tove</to>, splitting it across three lines would make the output harder to scan. The formatter keeps that leaf value on one line while still indenting it under its parent.
Will the tool still format XML that has an error?
Yes. If it detects an unbalanced or mismatched tag it shows a warning that names the tag involved, but it still beautifies or minifies your input. This lets you see the document structure and pinpoint exactly where the problem is before fixing it.