Machine-generated XML arrives as one unbroken line — a SOAP envelope, an RSS feed, a config file written by a build tool. Formatting it is the first step to reading it, and validating it is usually the reason you opened it in the first place. This tool does both as you paste, and puts the result on your clipboard so the next step is a single keystroke.
How to use the XML Formatter
- Paste your XML into the input box
- It formats immediately, and the result is copied to your clipboard
- Check the status badge — it reports whether the document is valid
- Change the indent to 2 spaces, 4 spaces or a tab if you need to
Formatting on paste, and copying on paste
Two things happen when you paste. The document is formatted, and the formatted result is written to your clipboard. Typing also reformats, continuously, but does not copy.
That split is deliberate rather than arbitrary. Writing to the clipboard requires what browsers call transient user activation — a recent, genuine user gesture. A paste counts; a keystroke does not. So the copy is attached to the one edit that can reliably carry it.
It is also the behaviour you would want anyway. Auto-copying on every keystroke would overwrite your clipboard dozens of times while you were still editing, and would almost certainly capture a half-finished document.
If your browser refuses the copy — an unfocused window and a denied clipboard permission are the two usual reasons — the note under the output says so, and the Copy button always works. The Auto-copy on paste checkbox turns the behaviour off entirely if you would rather your clipboard were left alone.
Why angle brackets are not enough to parse XML
The obvious way to format XML is to split on < and > and indent what falls out. It works on simple documents and corrupts the ones people actually need help with.
Four constructs contain angle brackets that are not tags:
- CDATA sections.
<![CDATA[ … ]]>exists precisely to hold raw markup — embedded HTML, a script, a chunk of another document. Everything inside is character data and must survive untouched. - Comments.
<!-- … -->can contain anything but a double hyphen, including tags that were commented out. - Attribute values.
<item label="a > b">is legal — a bare>needs no escaping inside a quoted value, though<does. - DOCTYPE internal subsets. The square-bracketed section of a doctype declaration carries its own
>characters.
This formatter tokenises the document instead, tracking quotes and bracket depth, so each of those is recognised and passed through whole. It is the difference between a formatter that works on a toy example and one that works on a SOAP response.
What the validator catches
XML is far stricter than HTML, and the strictness is the point — a parser that guesses is a parser that silently gives different consumers different data. Validation runs in two passes.
The first pass balances the tags, and it is the one that produces a useful message. If you have mistyped a closing tag it says expected </item> but found </iten>, which names the problem directly. If something is left open it names the element.
The second pass hands the document to the browser's own XML parser, which catches things tag balancing cannot: undefined entities, illegal characters, a missing or duplicated root element, and malformed declarations.
The most common failures, in rough order:
- More than one root element. An XML document has exactly one. Concatenating two responses produces a file that looks fine and parses nowhere.
- Unescaped
&. A bare ampersand starts an entity reference. In text and in attribute values it must be&— which is why URLs with query strings break XML so often. - Unclosed empty elements.
<br>is valid HTML and invalid XML; it has to be<br/>. - Case mismatch.
<Item>and</item>are different tags. XML is case-sensitive throughout.
Whitespace, and the one attribute that changes the rules
Reformatting moves whitespace around, and in XML whitespace is sometimes data. Being clear about what the formatter does matters more here than in JSON or CSS, where it never is.
Whitespace between elements is removed and replaced with indentation. For the overwhelming majority of documents — configuration, data interchange, feeds — this is exactly right and changes nothing a consumer cares about.
Text content is preserved, trimmed of its surrounding whitespace, and an element whose only child is text is kept on one line: <price>44.95</price> rather than split across three.
xml:space="preserve" is honoured. That attribute is the document telling you its whitespace is significant, and when it appears the entire subtree below it is emitted byte for byte as it arrived. This matters for documents carrying poetry, code samples, fixed-width records or anything where line breaks are content.
Mixed content is detected and left alone. An element holding both text and child elements — <p>Hello <b>world</b>!</p> — cannot be indented without inserting whitespace that was not in the document, and for prose-bearing XML such as XHTML, DocBook or an SVG <text> element that changes what it renders as. Most formatters break these across lines anyway. This one keeps the element on a single line and indents everything around it normally.
The trade-off is that a long paragraph of marked-up prose stays on one long line. That is the correct choice when the alternative is altering the document, and it is why a DocBook chapter formats less prettily here than a configuration file does.
Reading an unfamiliar XML document
Once a document is formatted, a few things orient you quickly.
Check the declaration. <?xml version="1.0" encoding="UTF-8"?> tells you the encoding, and an encoding mismatch is the usual explanation for accented characters arriving as mojibake.
Look at the root element and its namespaces. xmlns attributes identify what the document is. A root of soap:Envelope, rss, svg or xsd:schema tells you immediately which specification governs it and where to look things up.
Note the depth. The statistics under the output report the maximum nesting depth. Deeply nested documents — SOAP responses routinely reach eight or ten levels — are usually easier to navigate by collapsing in an editor than by reading top to bottom.
Distinguish attributes from child elements. Where a document puts its data is a design decision, and inconsistency usually marks a boundary where two systems meet.
Find the repeating unit. Most data documents are a container wrapping many identical records. Identifying that element tells you the shape of everything else.
Frequently asked questions
Is my XML uploaded anywhere?
No. Tokenising, formatting and validation all run in your browser, and the document is never transmitted. This matters for XML in particular, since SOAP envelopes and config files routinely carry credentials, endpoints and customer data.
Why did it not copy automatically?
Browsers only allow a clipboard write just after a genuine user gesture. A paste provides one; typing does not. If a paste still did not copy, the window may have lost focus or clipboard permission may be denied — the Copy button always works.
Can I turn the automatic copying off?
Yes. Uncheck 'Auto-copy on paste' and the tool will format without touching your clipboard. Formatting on paste and while typing continues either way.
Will formatting change what my XML means?
For ordinary data documents, no — whitespace between elements is insignificant and that is all that moves. The exception is mixed content, where an element holds both text and child elements. Any subtree marked xml:space="preserve" is left exactly as it arrived.
Does it handle CDATA sections and comments?
Yes. Both are recognised as single units and passed through unchanged, including any markup inside them. The same applies to processing instructions and DOCTYPE declarations with internal subsets.
Why does my file say it is invalid when it opens fine in a browser?
Browsers parse HTML forgivingly and XML strictly. Unclosed tags such as a bare <br>, an unescaped & that should be &, mismatched capitalisation, or more than one root element are all accepted in HTML and rejected in XML.
How large a document can it handle?
There is no imposed limit. Processing happens on your machine, so very large files — tens of megabytes — will make the tab work hard, and formatting runs slightly behind your typing on documents that size.