XML Formatter
Format, beautify, and minify XML instantly. Validates XML structure and shows errors. Free, browser-based, no server required.
- Runs in your browser
- Your data never leaves your browser
- Free · No Sign-Up
Scan with WeChat to share this tool
Examples, details and FAQ Worked examples, how it compares with other tools, and answers to common questions.
Worked Examples
Before formatting
<root><user id="1"><name>Alice</name><email>alice@example.com</email></user></root>
After formatting
<root>
<user id="1">
<name>Alice</name>
<email>alice@example.com</email>
</user>
</root>
Declaration, comment and CDATA
The output below is what the tool returned in the browser for a one-line order document:
<?xml version="1.0" encoding="UTF-8"?><!-- order 1042 --><order id="1042" status="paid"><item sku="A-1" qty="2"/><note><![CDATA[Leave at <door> & ring]]></note><total currency="EUR">59.90</total></order>
Format with 2 spaces:
<?xml version="1.0" encoding="UTF-8"?>
<!-- order 1042 -->
<order id="1042" status="paid">
<item sku="A-1" qty="2"/>
<note>
<![CDATA[Leave at <door> & ring]]>
</note>
<total currency="EUR">59.90</total>
</order>
The XML declaration is copied as written, because the browser’s DOM has no node for it. The comment and the CDATA section keep their content, so <door> and & inside CDATA are not escaped. Minify on the formatted text gives back the original single line. Attribute order and quote style follow the parsed document: every attribute is written with double quotes, and " < > & ' inside a value are escaped.
Mixed content changes
In documents where text and elements mix, such as XHTML or DocBook, the whitespace around inline elements carries meaning. The formatter does not keep it. <p>Hello <b>world</b>!</p> formats to:
<p>
Hello
<b>world</b>
!
</p>
and minifies to <p>Hello<b>world</b>!</p>, which drops the space before “world”. Use the tool on data-style XML (configuration, API payloads, feeds); for markup documents, check the result before you replace the source.
Error messages
Errors come from the browser’s XML parser. The status line shows “Error: ” and the first error with its line and column. In Chrome 152 (measured on 2026-10-08), <company>AT&T</company> gives error on line 1 at column 14: EntityRef: expecting ';', and <a><b>x</a> gives error on line 1 at column 12: Opening and ending tag mismatch: b line 1 and a. Firefox words the same errors differently, and the message stays in English on every page language.
Whitespace-only elements
Both outputs below start from the same input. With two-space indentation, the first value loses its surrounding spaces. The blank element becomes a self-closing tag in formatted output. Minify also removes the blank text, but keeps that element’s opening and closing tags because the parsed element still has a text node. The already empty element stays self-closing.
Input
<root><value> x </value><blank> </blank><empty/></root>
Formatted with 2 spaces
<root>
<value>x</value>
<blank/>
<empty/>
</root>
Minified
<root><value>x</value><blank></blank><empty/></root>
Attribute quotes and escaped text
The input uses single quotes for its attribute. Both operations write double quotes instead. The ampersand in the attribute and the less-than sign in the text are escaped again when the parsed values are serialized. Formatting this single text child adds no extra line breaks.
Input
<item name='A & B'>1 < 2</item>
Formatted or minified output
<item name="A & B">1 < 2</item>
Common Errors
The formatter parses input with the browser’s XML parser, which stops at the first well-formedness error and shows the browser’s message. These are the usual causes:
| Error | Example | Fix |
|---|---|---|
| Unclosed tag | <person><name>Alice</person> | Close <name> with </name> |
| Mismatched tags | <b>bold</i> | Use the same name in the start and end tag; names are case-sensitive |
Raw & or < in text | <company>AT&T</company> | Write & and < |
| Two root elements | <record/><record/> | Wrap them in one parent, such as <records> |
| Unquoted attribute | <item id=42/> | Quote the value: id="42" |
Limits
- Declared entities are expanded. The XML declaration and the DOCTYPE, including its internal subset, are copied to the output unchanged, but a reference to an entity declared there, such as
&company;, is written as its replacement text. - Text is trimmed. Leading and trailing whitespace inside elements is removed, and
xml:space="preserve"is not honored:<v xml:space="preserve"> x </v>becomes<v xml:space="preserve">x</v>, although XML 1.0 §2.10 asks applications to keep that whitespace. - During formatting, empty elements are self-closed.
<note></note>and<note> </note>both become<note/>. - Well-formedness only. The tool does not validate against a DTD or XML Schema.
FAQ
Does it validate XML?
It checks well-formedness only. The browser's XML parser (DOMParser) stops at the first error, and the status line shows the browser's English message with the line and column. In Chrome 152 (tested), <company>AT&T</company> gives "error on line 1 at column 14: EntityRef: expecting ';'"; Safari messages in the same libxml2 form are read the same way. For Firefox, the tool reads its error format ("XML Parsing Error: …" with "Line Number N, Column M") and shows the first line followed by the line and column; this has not been tested in Firefox. The tool does not validate against a DTD or XML Schema.
Are CDATA sections preserved?
Yes. CDATA sections, comments, processing instructions, the XML declaration, and the DOCTYPE (with its internal subset) are kept during formatting. A CDATA section that is the only child of an element is written on its own line, between the start and end tags.
Are namespaces and prefixes kept?
Yes. Prefixed names such as x:v and the xmlns declarations are written as they were parsed, with the attributes in their original order. Every attribute value is written in double quotes.
Can I customize indentation?
Yes. Choose 2 spaces, 4 spaces, or a tab from the Indent menu. Typing formats with the selected indentation; after you change the menu, click Format to apply it to the current output.
Why did the spaces in my text change?
The tool trims leading and trailing whitespace in every text node and drops whitespace-only text, and it does not honor xml:space="preserve". When text and elements are mixed, Format puts each piece on its own line and Minify joins the trimmed pieces, so <p>Hello <b>world</b>!</p> minifies to <p>Hello<b>world</b>!</p>.
Is my data sent anywhere?
No. The XML is parsed and formatted in this browser tab and is not sent to a server or saved in browser storage. When you click Format or Minify, the page statistics record the tool name and that action, not the XML.