How to use the XML Formatter
- Paste XML into the box, open an .xml, .svg, .xsd or .config file, or click Load sample.
- If the document is not well-formed, read the message and click Go to line to jump straight to the problem.
- Choose 2 spaces, 4 spaces or tabs and click Format, or click Minify to remove the whitespace between tags.
- Tick Strip comments if you do not need them, then copy the result or download it as a file.
Features
- Well-formedness check by the browser’s own XML parser
- Errors shown with line, column, a code excerpt and a fix hint
- Text, CDATA sections and xml:space="preserve" content left untouched
- Keeps the XML declaration, DOCTYPE, comments, processing instructions and namespaces
- Indent with 2 spaces, 4 spaces or tabs, or minify
- Optional comment stripping for smaller files
Well-formed versus valid XML
XML has two levels of correctness. A document is well-formed when it follows the syntax rules of the XML 1.0 specification: there is exactly one root element, every start tag has a matching end tag, elements nest properly without overlapping, attribute values are quoted, an attribute appears only once per element, and the special characters < and & are escaped in text. A document is valid when, on top of that, it matches a schema such as a DTD or an XSD that says which elements and attributes are allowed. This tool checks well-formedness, which is what every XML parser requires before it will read a file at all. Schema validation needs the schema file and a dedicated validator.
Characters that must be escaped
| Character | Write it as | When |
|---|---|---|
& | & | Always, in text and attribute values |
< | < | Always, in text and attribute values |
> | > | Required only in the sequence ]]>, but common everywhere |
" | " | Inside double-quoted attribute values |
' | ' | Inside single-quoted attribute values |
Those five are the only named entities XML defines. HTML names such as or © cause an “entity not defined” error; use a numeric reference like   or type the character directly. If you need to convert a block of text, the HTML Entity Encoder can help.
How whitespace is handled
Pretty-printing adds line breaks and indentation between elements, and minifying removes them. That is safe because whitespace between tags is normally just layout. Whitespace inside text is a different matter: in <name> Ada </name> the spaces may be meaningful to the program reading the file. So the formatter only rearranges elements that contain other elements. Any element that holds text, including mixed content such as <p>Hello <em>world</em></p>, is written exactly as it was. Elements marked xml:space="preserve" and CDATA sections are never touched.
Small changes you may notice
- Attribute values are always written in double quotes.
- Numeric references such as
©are written as the character itself (©), which means the same thing to any parser. - An empty element such as
<br></br>is written in its short form,<br/>. - Attribute order, namespace prefixes and comments stay as they were.
When to minify XML
Minified XML is useful when the document travels inside something else: a SOAP request, a database column, an environment variable or an embedded SVG icon. Removing indentation from a deeply nested file often saves a quarter or more of its size. For files served over HTTP the gain is smaller, because gzip and Brotli already compress repeated whitespace well, so readable formatting usually costs little.
Reading common error messages
Different browsers word parser errors differently, so the tool adds a hint for the frequent ones. “Opening and ending tag mismatch” means a closing tag does not match the element that is open, often because of a typo or a tag closed in the wrong order. “Extra content at the end of the document” means there is more than one root element. An error at the very end of the file usually means a closing tag is missing somewhere earlier.
Working with other formats? Try the JSON Formatter or the HTML Formatter.
Frequently asked questions
Does this validate XML against an XSD or DTD?
No. It checks that the document is well-formed, which every XML parser requires. Checking it against a schema needs the schema file and a separate validator.
Why does cause an error?
XML defines only five named entities: &, <, >, " and '. Names like come from HTML, so write   or the non-breaking space character instead.
Will formatting change my data?
Only the whitespace between elements changes. Elements that contain text, CDATA sections and xml:space="preserve" blocks are written exactly as they were, so the values a program reads stay the same.
Can I format SVG, RSS or sitemap files?
Yes. Any XML-based format works, including SVG images, RSS and Atom feeds, sitemaps, Android layouts, Maven pom.xml files and .NET config files.
Is my XML sent to a server?
No. Parsing and formatting happen in your browser, so configuration files and API payloads stay on your device.