XML Formatter
Paste XML to pretty-print, validate or minify it instantly — with CDATA, comments and the XML declaration preserved, and the exact line of any error. Nothing leaves your browser.
More options
Paste or type XML above to validate, format or minify it — with the exact line of any error.
What "well-formed XML" actually means
A formatter can only re-indent text that already obeys XML's grammar, so the first job of this tool is to tell you exactly where that grammar breaks — the line and column the browser's own parser stopped at — and only then to pretty-print or compress the result. This tool checks well-formedness (the syntax every XML document must follow), not validity against a specific schema or DTD, since that would require fetching external files over the network, which a browser-only tool intentionally never does.
Every well-formed XML document has exactly one root element, every opening tag has a matching closing tag in the right order, attribute values are always wrapped in quotes, and the characters &, < and > can only appear literally inside a <![CDATA[ ... ]]> block — anywhere else they must be escaped.
The rules people trip over
| Allowed | Not allowed |
|---|---|
One root element: <root>...</root> | Two siblings at the top level |
Quoted attributes: id="7" | Unquoted: id=7 |
Self-closed empty tags: <br/> | An opening tag with no closing tag |
Escaped ampersand: & | A bare & in text |
Matching case: <Item>...</Item> | <Item>...</item> (XML is case-sensitive) |
Worked example 1 — finding a mismatched tag
This input fails: <order><item>Pen</order></item>
The parser expected </item> to close the tag that was opened right before it, but found </order> instead — the two closing tags are in the wrong order. Fix it by closing tags in last-opened, first-closed order: <order><item>Pen</item></order>.
Worked example 2 — Format versus Minify
Take <book id="7"><title>Dune</title></book> (39 characters). Format with 2-space indent expands it into a readable three-line block for editing or review. Minify strips every optional space and newline to produce the shortest equivalent — handy for shrinking a payload before sending it over a network. Both represent the identical document; only the whitespace between tags differs.
Worked example 3 — CDATA and comments survive a round trip
Enter <script><![CDATA[if (a && b) { go(); }]]><!-- keep --></script> and click Format: the <![CDATA[ ... ]]> block and the <!-- keep --> comment both come back unchanged, because the serializer walks the parsed document tree node by node instead of pattern-matching the raw text. A naive formatter built on regular expressions would often reformat or corrupt exactly this kind of content.
Elements, attributes and namespaces
- Indentation (2 spaces, 4 spaces, or a tab) changes only readability, never the document's meaning.
- Empty elements are written as a compact self-closing tag, so
<note></note>becomes<note/>— the two forms are equivalent in XML. - Mixed content — an element that contains both text and child elements, like
<p>Hello <b>world</b>!</p>— is left exactly as written on one line, since inserting or removing whitespace there could change what a reader (or a program) sees as the actual text. - Namespace declarations (
xmlns,xmlns:prefix) are ordinary attributes as far as the serializer is concerned, so they are preserved with the element that declares them, exactly as typed.
Runs entirely in your browser
Parsing and serialization happen locally using the browser's built-in DOMParser — nothing you paste is uploaded, which means configuration files, SOAP or REST responses, and RSS or Atom feeds that contain access tokens or personal data can be formatted safely. A formatter only confirms your document is well-formed XML; it cannot know whether the element names and structure are the ones a particular application or schema expects, so a document can format cleanly and still fail a stricter, schema-based check elsewhere.
Sources & further reading
Frequently asked questions
How do I pretty-print or format XML online?
Paste your XML into the input box and click Format — the parser reads it into a document tree and a hand-written serializer re-indents every element with the spacing you choose (2 spaces, 4 spaces or a tab). Empty elements collapse to a compact self-closing tag like <note/>, elements that only contain text stay on one line, and everything nested under a parent element is indented one level deeper. For example, <note><to>Ada</to><body>Meet at noon.</body></note> becomes a three-line block with <to> and <body> indented under <note>. The formatting only changes whitespace between tags — the data itself is untouched.
Why does this tool say my XML is invalid?
XML has a strict grammar, and the most common breakages are an unclosed or mismatched tag (<a><b></a>), more than one root element (everything must nest inside a single top-level tag), an unescaped & < or > inside text or an attribute value, or an attribute value that isn't wrapped in quotes. This validator reports the browser parser's own error message together with the line and column where it stopped, so you can jump straight to the problem — for example <user name=Ada> fails because Ada needs quotes around it: <user name="Ada">.
What's the difference between Format and Minify?
Format re-indents the same document to make it readable for editing or code review, while Minify strips every newline and indentation space to produce the smallest possible XML — useful for shrinking a payload before sending it over a network or storing it in a database column. Both represent identical data; only the whitespace between tags differs. The status line shows exactly how many bytes minifying removed, as a percentage of the original size.
Does this tool preserve CDATA sections, comments and the XML declaration?
Yes. Instead of using regular expressions on the raw text, the formatter parses your XML into a real document tree with the browser's DOMParser and then walks that tree with a hand-written serializer, so <![CDATA[ ... ]]> blocks, <!-- comments --> and the original <?xml version="1.0" encoding="UTF-8"?> declaration are re-emitted exactly as they were, never stripped or reformatted. Elements that mix text and child elements (for example <p>Hello <b>world</b>!</p>) are also left untouched on one line, since reformatting the whitespace inside mixed content could change what the document means.
Is my XML data uploaded to a server?
No. Parsing, formatting, minifying and validation all run locally in your browser using the built-in DOMParser — nothing you paste is ever sent to a server, logged or shared, which makes it safe for XML that contains API responses, configuration or personal data. Your most recent input is optionally kept in this browser's local storage so it's still here next time; untick "Save my XML in this browser" to keep it for the current session only, and Clear removes the stored copy immediately.