XML Formatter and Validator
Paste an XML document of up to about 1,000,000 characters (or load a .xml file) to pretty-print it, minify it or validate it. You also get element and attribute counts, a collapsible tree view and an XML to JSON conversion — all processed locally in your browser.
Format, Validate or Minify Your XML — All in Your Browser
XML Formatter Formula
Where:
- \(s\) — whitespace saved by minification, in percent
- \(L_{\min}\) — length of the minified document in characters
- \(L_{\text{orig}}\) — length of the original document in characters
- \(i\) — one indentation step (2 spaces, 4 spaces, or a tab character)
- \(d_i\) — nesting depth at line \(i\); \(c_i\) — that line's content
Formatting is a structural operation, so a single equation can only describe its measurable effects. The first expression is the "minified: … chars, saved …%" badge the tool shows in Minify mode; the second states the pretty-printing rule — every output line is indented by one step per nesting level before its content is written.
XML Formatter – Calculation Example
Example: a short note stored pretty-printed with two-space indentation and three line breaks — 67 characters in total — is run through Minify.
\[ L_{\text{orig}} = 67 \text{ characters}, \qquad L_{\min} = 60 \text{ characters} \]
\[ s = \left(1 - \frac{60}{67}\right) \times 100\% \approx 10\% \]
Result: <note to="You"><from>WPCalc</from><body>Hello!</body></note> — one compact, 60-character line, about 10% smaller than the original.
How to Use the Calculator
- Paste your document into the Your XML box, or press the 📂 Load .xml file button in the results to open a file from your device — .xml, .xsd, .xsl, .svg, .rss, .kml, .gpx, .plist and .config files are all accepted.
- Choose the Output: Pretty-print (indent, one element per line) for readable markup, Minify (compact single string) for the smallest possible file, or Validate only (no output) if you just want the health check.
- In pretty-print mode, pick the Indentation: 2 spaces, 4 spaces, or tab characters — one step is applied per nesting level.
- Tick Sort attributes (A→Z) if you want every element to reprint its attributes in alphabetical order. Leave it off to keep the original order.
- Read the live results: status badges with element, attribute and nesting counts, the syntax-highlighted output, the validation report, the collapsible tree view and the JSON conversion. Use 📋 Copy or 💾 Download to take the result with you.
What This Tool Does
The formatter parses your text with the XML parser built into your browser, so the verdict matches what real XML consumers would say about the same file. It then re-serializes the parsed document tree in one of three ways: pretty-printed with your chosen indentation and one element per line, minified with every skippable whitespace run stripped, or left untouched when you only asked for validation. Comments, CDATA sections, processing instructions and the XML declaration survive all three modes.
When the document is valid, the status area shows a green ✔ Well-formed badge plus counts of elements, attributes, nesting levels, comments, CDATA sections and processing instructions — a quick way to measure a file before you commit it to a repository or pipeline.
When something is wrong, the tool stops at nothing being guessed: it shows the exact parser message together with the line number, the column and the offending source line with a caret under the problem position, plus a plain-English hint about the most probable cause (mismatched tags, a stray unescaped character, duplicate attributes, more than one root element, and so on). Because the document is re-checked on every edit, you can fix the error right in the input box and watch the report turn green.
Validation, Tree View and JSON Output
The check performed is XML 1.0 well-formedness: exactly one root element, every start tag closed by a case-sensitive matching end tag, quoted and non-repeated attribute names, correctly escaped text where only the five predefined entities &, <, >, ", ' (or numeric references such as é) resolve, and a legal prolog.
The tree view draws the root element as collapsible nodes, showing child counts, attributes and trimmed text snippets for each branch. It is drawn for documents of up to 2,000 elements.
The XML → JSON panel applies the classic mapping: attributes become @name keys, an element with only text collapses to a plain string, mixed content keeps its text under #text, and repeated siblings — such as a catalog's <book> entries — come out as a JSON array. Comments and processing instructions are dropped, since JSON has no equivalent. If you need to tidy up JSON files the same way, the JSON Formatter is the matching tool for the other direction.
Tips and Limitations
- Keep documents under about 1,000,000 characters — past that the page refuses politely rather than freezing your tab.
- Well-formedness is not schema validation. A document can pass here and still violate its DTD or XSD; external DTD files are never fetched.
- Outputs longer than 300,000 characters are displayed without syntax coloring to keep the page fast, but Copy and Download always carry the complete text.
- Attribute values longer than 60 characters are trimmed with an ellipsis in the tree view only; the formatted output is always complete.
- The JSON conversion is skipped above 6,000 elements, again purely for speed.
- The most common fatal error is a raw
&inside text or an attribute value — it must be written as&. Our HTML entity encoder escapes&,<and>for you. - To compare two versions of a similar document, sort attributes A→Z in both, then paste them into the Diff Checker.
- Everything — parsing, formatting, tree building and conversion — runs locally inside this tab. Your XML, including any loaded file, never leaves your device.
Frequently Asked Questions
What is the difference between well-formed and valid XML?
Well-formed means the syntax is legal: one root element, matching case-sensitive tags, quoted and unique attributes, escaped text. Valid means the document additionally obeys a schema (DTD, XSD, RelaxNG) — required elements, correct data types, allowed attribute values. This tool checks well-formedness, which every XML consumer requires, but does not fetch or evaluate schemas. To verify schema compliance, paste your document into a dedicated XSD validator along with the schema file.
Why does a raw ampersand (&) break my XML, and how do I fix it?
The & character starts an entity reference in XML, so a bare & inside text or an attribute value is a fatal parse error. Replace every literal ampersand with & — for example, "Tom & Jerry" must be written as "Tom & Jerry" in the source. The same rule applies to <, > in text, and quotes inside attribute values. This stray-ampersand mistake is by far the most common validation failure.
Is minifying XML safe? Can removing whitespace change its meaning?
Usually yes, but not always. Whitespace between elements is ignorable in most documents, so stripping it changes nothing semantically. However, if your document uses mixed content (text interleaved with child elements) or declares xml:space="preserve", that whitespace can be significant data. The same caution applies to pretty-printing: if you add indentation inside an element whose text matters, you change the content. Test minified output against whatever consumes the file before committing it.
How can I check whether an XML document is well-formed without any tool?
Scan for these rules: exactly one top-level element after the optional declaration and DOCTYPE; every start tag has a matching end tag with identical capitalization (<Book> is not closed by </book>); attribute values are in quotes and no attribute name repeats in one tag; text contains only &, <, >, ", ' or numeric references like é — never raw &, < or unmatched quotes. Then run the file through this validator: it pinpoints the exact line and column of any violation.
Which characters must be escaped in XML, and are numeric references safe instead?
Only five entities are predefined: & (&), < (), " and '. In element text you must escape & and <; escaping > is optional there, but inside attribute values quotes matter too. HTML entity names like or © are not defined in plain XML and cause parse errors — use numeric references such as   or © instead.
How does the XML-to-JSON conversion handle attributes, repeating elements and namespaces?
The classic mapping is used: each attribute becomes an @name key, so id="bk101" turns into "@id": "bk101". Repeating sibling elements with the same name become a JSON array; a single element with only text collapses to a plain string; an element with both attributes and text keeps the text under #text. Namespace declarations such as xmlns are treated as ordinary attributes and appear as @xmlns keys, exactly as written in the source.
My XML came from Excel, Word or an API and contains weird characters. Why won't it parse?
Two usual culprits. First, a character not allowed in XML 1.0 — most control characters below \(0x20\), except tab, newline and carriage return, are illegal even when escaped. Second, an encoding mismatch: if the declaration says encoding="UTF-8" but the file was saved as, say, Windows-1252, multi-byte characters desynchronize and the parser reports an error near the damaged byte. Re-save the file as UTF-8 and remove control characters pasted from rich-text sources.