What an XML formatter does
XML (Extensible Markup Language) is a text format that structures data with nested tags. It powers RSS feeds, website sitemaps, electronic invoices, the configuration files of countless programs, and a long list of older APIs. The trouble starts when that XML arrives on a single line, with no breaks or indentation: it is perfectly valid for a machine, yet nearly unreadable for a human.
This formatter rewrites XML with one line break per node and an indent of two or four spaces per nesting level. You paste the messy document and get a clean, readable version you can copy with one click. It also ships a minify button that does the opposite: it strips the whitespace between tags so the file weighs as little as possible.
How to use it
- Paste your XML into the input box (or hit Load example to see a sample).
- Pick the indentation: 2 spaces (the common choice) or 4.
- Press Format to indent it, or Minify to compact it.
- Use Copy to send the result to your clipboard and Clear to start over.
Everything happens inside your browser with JavaScript: the XML is never uploaded to a server, so you can work with documents that hold private data without worry.
How the indentation works
The tool scans the text and splits it into two kinds of pieces: tags (anything between < and >) and the text that sits between them. With those pieces it applies a simple depth rule:
- When it meets an opening tag such as
<note>, it increases the indent level. - When it meets a closing tag such as
</note>, it decreases the level. - A self-closing tag such as
<break/>leaves the level unchanged: it gets its own line at the current depth. - An element with a single text child, like
<to>Maya</to>, stays on one line so it is not broken apart.
Comments (<!-- ... -->), declarations (<?xml ... ?>), and CDATA sections are treated as whole blocks that take their own line without touching the indentation.
| Element | Effect on indentation |
|---|---|
<root> (opening) | Adds one level |
</root> (closing) | Removes one level |
<item/> (self-closing) | No change |
<t>text</t> (with text) | No change, one line |
<!-- note --> (comment) | No change |
Worked example
Say you copy this XML note exactly as an API returns it, all crammed onto one line:
<?xml version="1.0" encoding="UTF-8"?><note><to>Maya</to><from>Alex</from><body>Hi</body></note>
When you press Format with 2 spaces, the tool recognizes the leading declaration, opens <note> by raising one level, and places each child element with its text on its own line. The result is:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Maya</to>
<from>Alex</from>
<body>Hi</body>
</note>
If you now press Minify on that same document, you get back exactly the original line: the round trip is stable.
Frequently asked questions
Does the formatter validate my XML?
Its main job is to indent, not to validate strictly. Even so, if it spots opening or closing tags without a match it shows a warning so you can review the document. For formal validation against a schema (XSD or DTD) you need a dedicated validation tool.
What is the difference between format and minify?
Formatting adds line breaks and spaces so a person can read it comfortably; minifying removes them so the file is as light as possible when you send or store it. The data itself is identical either way: only the whitespace between tags changes.
Can I use it for HTML?
HTML looks like XML, but it has tags that never close (such as <br> or <img> without the trailing slash) and looser rules. This formatter targets well-formed XML; it may handle simple HTML, though that is not its purpose.
Is my file uploaded anywhere?
No. All processing happens on your own device with JavaScript. It is safe to paste XML with sensitive information, such as invoices or internal configurations, because it never leaves your screen.