Skip to the tool
DevToolBench

XML Formatter

Indent or minify XML, with the broken tag named.

0 chars
Elements
0
Max depth
0
Characters
0
Well formed

Everything runs in your browser. Nothing you type is sent to a server.

Found a bug in this tool? Report it.

Share this tool

What the formatter does

This XML formatter reindents a document with 2 spaces, 4 spaces or tabs, or flattens it to a single line, without leaving your browser. It is a hand-written tokeniser rather than the browser's parser, so the same code runs under test: it walks declarations, DOCTYPE lines, comments, processing instructions, CDATA sections, tags and text, then rebuilds the file from what it found.

Attributes are put back one space apart with proper quoting, so a tag spread over five lines becomes one readable line. When something does not close, you get the line, the column and the name of the tag that went wrong.

XML did not go anywhere

It is easy to believe the format died in 2012. It did not, and the places it survives are places nobody gets to opt out of.

Every sitemap Google reads is XML, and so is every RSS and Atom feed. Android lays out its screens and declares its permissions in it. Maven, Ant and NuGet describe builds in it. SOAP still moves money between banks and insurers, and the government and healthcare integrations built around it are measured in decades. Office documents — .docx, .xlsx — are zipped folders of it. SVG is it.

None of that is nostalgia. It is why a working formatter is still a tool people reach for, usually at the moment a feed will not validate or a build file has been merged badly.

Whitespace is not decoration

The rule that trips people is that XML has no way to know which spaces you meant. Between two tags, whitespace is almost always layout. Inside a run of text, it is content. A formatter that treats both the same will quietly edit your document.

So this one draws the line the same way a careful hand would. Whitespace-only text between elements is layout and gets replaced by the new indentation. An element holding one run of text has that text trimmed at the ends and pulled onto the tag's line. But an element with text beside a child tag is left alone completely, on a single line, because there is no safe way to add newlines to a sentence. And xml:space="preserve" disables all of it for that subtree, which is the attribute's whole purpose.

CDATA, and when to use it

A CDATA section is an escape hatch: everything between the delimiters is text, so < and & lose their meaning and need no escaping. That makes it the right home for an embedded script, a chunk of HTML inside a feed, or a SQL fragment in a config file.

It is not a magic quote. The one sequence it cannot contain is its own terminator, and a section holding untrusted content is a real injection route in feed readers. Content inside one is copied through here untouched, and its presence stops the surrounding element from being reindented.

XML against JSON, without the sermon

JSON won for APIs because it maps onto the data structures of every language with no ceremony, and because it has one obvious way to write most things. For a payload between two services, that is the whole argument.

XML earns its weight where documents, not records, are the subject. Attributes distinguish metadata from content. Namespaces let two vocabularies share a file without colliding. Schemas, XPath and XSLT are mature in a way the JSON equivalents still are not, and mixed content — text with markup inside it — has no natural JSON representation at all.

Choose by shape of the problem, not by year. If you are moving between the two, the JSON formatter handles the other side, and the JSON to TypeScript converter will type a payload once you have it.

Frequently asked questions

Why did my indentation not change inside one element?

Because that element contains mixed content — text sitting next to a child tag — or a CDATA section, and in both cases the whitespace belongs to the document. Reindenting a paragraph that reads "See <em>this</em> page" would insert newlines into the sentence. Elements like that are put on one line exactly as they arrived, and everything around them is still indented normally.

What does xml:space="preserve" do here?

It switches the formatter off for that element and everything under it, which is precisely what the attribute means in the specification. If you have a block whose spacing matters — a code sample, a fixed-width record, an ASCII diagram — mark it that way and no formatter, including this one, is allowed to touch it.

Does minifying change what the document means?

Not in the cases it acts on. It removes whitespace-only text between tags, which no XML consumer treats as content, and trims the padding around a single run of text. Anything else is copied through: comments stay, CDATA stays, and text sitting beside a tag stays byte for byte. If you need comments gone, delete them deliberately rather than letting a minifier decide.

Why does it complain about an ampersand in my text?

Because a bare & is not valid XML. The parser reads it as the start of an entity reference and then finds no semicolon, so the document is rejected — often much later, by something that gives no line number. Write &amp; for a literal ampersand, or move the run of text into a CDATA section where & has no special meaning.

Can it fix a document rather than just reporting the error?

No, and that is on purpose. When a closing tag does not match, only you know whether the opening tag was wrong or the closing one was — guessing produces a well-formed file that says something you never wrote. You get the line, the column and both tag names instead.

Related tools

Updated