XML Formatter
Formats XML with 2 spaces, 4 spaces or tabs, or minifies it to one line. It checks that the XML is well-formed and points to the exact line and column of any error.
- Runs in your browser
- Nothing uploaded
- Free, no sign-up
How to Use XML Formatter
- Paste or open your XML. Paste it into Your XML, press Open file, or drop a file on the box.
- Pick Format or Minify. Format adds line breaks and indentation; Minify puts everything on one line.
- Set the options. Choose 2 spaces, 4 spaces or a tab, and whether to remove comments, collapse empty elements or put long attribute lists one per line.
- Read the status bar. Green means well-formed. Red gives the line and column of the first error, with a caret under it.
- Copy or download. Copy result puts it on your clipboard; Download .xml saves a file.
Example: formatting a one-line catalog
The Sample button loads a 341-byte catalog written on one line, with a comment, a CDATA section and a character reference. Format turns it into this:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!-- spring list -->
<book id="bk101" lang="en">
<title>XML Basics</title>
<price currency="USD">29.99</price>
<note><![CDATA[Use <tags> & entities freely here]]></note>
</book>
<book id="bk102" lang="fr">
<title>Café & Code</title>
<price currency="EUR">24.50</price>
<stock/>
</book>
</catalog>
The status bar reads Well-formed XML: 9 elements, 6 attributes, 3 levels deep. The comment, the CDATA section and é are copied exactly as written. Nothing is decoded or reordered.
Switch to Minify and the same file becomes one line of 321 B, 5.9% smaller than the input, with the comment removed. Untick Remove comments to keep it.
Example: finding the error in broken XML
The Broken sample has two common mistakes:
<catalog>
<book id="bk101">
<title>Fish & Chips</title>
<price currency=USD>9.99</price>
</book>
</catalog>
The tool stops at the first one: Not well-formed: line 3, column 17, “A raw & must be written as &. XML treats & as the start of an entity such as & or é.” Change it to Fish & Chips and it moves to the next: Not well-formed: line 4, column 21, “The value of attribute currency must be in quotes, for example currency="USD".”
XML parser errors and what they mean
Browsers and languages do not write their own XML parsers. Chrome, Edge, PHP and the xmllint command use libxml2; Firefox and Python use expat. So the same mistake gives different wording depending on where you see it. We ran each example through libxml2 2.9.13 and expat 2.4.7 and copied the first message each one printed.
| Problem | Example | Chrome, Edge, PHP, xmllint (libxml2) | Firefox, Python (expat) | What this tool says |
|---|---|---|---|---|
| A raw ampersand in text | <menu>Fish & Chips</menu> |
xmlParseEntityRef: no name |
not well-formed (invalid token) |
A raw & must be written as &. XML treats & as the start of an entity such as & or é. |
| An attribute value without quotes | <price currency=USD>9.99</price> |
AttValue: " or ' expected |
not well-formed (invalid token) |
The value of attribute currency must be in quotes, for example currency="USD". |
| Closing tags in the wrong order | <p><b>bold</p></b> |
Opening and ending tag mismatch: b line 1 and p |
mismatched tag |
Closing tag </p> does not match the open <b> from line 1. <b> was probably never closed. |
| Upper and lower case do not match | <Item>x</item> |
Opening and ending tag mismatch: Item line 1 and item |
mismatched tag |
Closing tag </item> does not match the open <Item> from line 1. Check the spelling and letter case; XML tag names are case-sensitive. |
| An element that is never closed | <list><item>a</item> |
Premature end of data in tag list line 1 |
no element found |
The document ends but <list> from line 1 is never closed. |
| Two root elements | <a/><b/> |
Extra content at the end of the document |
junk after document element |
Only one root element is allowed. This second top-level element must go inside the first one, or wrap both in a new root element. |
| An HTML entity such as | <p>A B</p> |
Entity 'nbsp' not defined |
undefined entity |
The entity is not defined. XML only knows < > & " and '. Use a number instead, for example   for a non-breaking space. |
| The same attribute twice | <img src="a.png" src="b.png"/> |
Attribute src redefined |
duplicate attribute |
Attribute src appears twice in <img>. Each attribute can appear only once per element. |
| Something before the XML declaration | <?xml version="1.0"?><a/> |
XML declaration allowed only at the start of the document |
XML or text declaration not at start of entity |
The XML declaration <?xml … ?> is only allowed at the very start of the document, with nothing before it, not even a space or a blank line. |
| A < inside an attribute value | <rule test="a<b"/> |
Unescaped '<' not allowed in attributes values |
not well-formed (invalid token) |
A < inside an attribute value must be written as <. |
| A tag name that starts with a digit | <1st>x</1st> |
StartTag: invalid element name |
not well-formed (invalid token) |
An element name cannot start with "1". Names must start with a letter, a colon or the _ character. |
The five characters XML escapes
XML has only five named entities. Anything else, such as or ©, comes from HTML and fails unless a DOCTYPE defines it. Use a numeric reference instead:   for a non-breaking space, © for the copyright sign.
| Character | Write it as | Must be escaped |
|---|---|---|
& |
& |
Always, in text and in attribute values |
< |
< |
Always, in text and in attribute values |
> |
> |
Only in the sequence ]]> in text; safe elsewhere |
" |
" |
Inside an attribute value that uses double quotes |
' |
' |
Inside an attribute value that uses single quotes |
How it works
- The tool reads your XML with its own parser and checks the rules for well-formed XML from the W3C XML 1.0 specification: one root element, every tag closed in the right order, quoted attribute values, no repeated attributes, escaped
&and<, and the declaration only at the very start. - It keeps the text you wrote. Attribute values, quote style, entities, comments, CDATA sections, processing instructions and the DOCTYPE come out character for character. Only the whitespace between tags changes.
- Elements that hold only text stay on one line, so
<title>XML Basics</title>does not get split into three lines. - Mixed content, where text and tags sit side by side as in
<p>Hello <b>world</b></p>, is left exactly as it is, because adding line breaks there would change the text. Elements markedxml:space="preserve"are also left untouched. - Minify removes the whitespace between tags and, by default, the comments. It also tidies the inside of each tag, so
<a x = "1" >becomes<a x="1">. Text inside elements is not changed.
Limits
- It checks that XML is well-formed. It does not validate against a DTD, an XSD schema or RelaxNG, so a misspelled element name that is closed correctly still passes.
- Entities declared inside a DOCTYPE are accepted but not expanded. If a DOCTYPE is present, unknown entity names are not flagged.
- Only the first error is reported. Fix it and run again to see the next one.
- Whitespace-only text between tags is dropped when formatting. If that whitespace matters, mark the element with
xml:space="preserve". - Files open up to 30 MB. Above about 1 million characters, live updates pause and you press the button instead.
Frequently asked questions
How do I check if my XML is valid?
Paste it here. A green bar means it is well-formed, which is what most parsers need. A red bar gives the line and column of the first problem with the line shown underneath. Checking against a schema (XSD) is a separate step this tool does not do.
Why does my XML say the entity nbsp is not defined?
Because is an HTML entity, not an XML one. XML only predefines <, >, &, " and '. Replace with  .
Does minifying XML change the data?
Not for data files. It removes whitespace between tags and, if you choose, comments. Text inside elements, attribute values and CDATA are kept. If a program reads the whitespace between tags as data, keep using the formatted version or mark that element with xml:space=”preserve”.
Can I format SVG, RSS, sitemap or .csproj files?
Yes. They are all XML, so Open file accepts .svg, .rss, .kml, .gpx, .xsd, .xsl and similar files. For a sitemap you may also want the XML Sitemap Validator, which checks the sitemap rules on top of the XML syntax.
Related Tools
Convert CSV to XML →
Convert CSV to XML with custom tag names, element/attribute modes, CDATA wrap, headerless mode.…
JSON to XML Converter →
Converts JSON to well-formed XML. Keys starting with @ become attributes, arrays become repeated…
XML to JSON Converter →
Converts XML to JSON with attributes, repeated elements, CDATA and namespaces handled. You choose…
Convert XML to PDF Invoice →
Convert XML invoice data to professional PDF invoices instantly. Support for UBL and custom…
XML to YAML Converter →
Convert XML into readable YAML that mirrors the document tree, with indentation for nesting.
XML Sitemap Formatter →
Pretty print or minify a sitemap XML with clean indentation, a well-formed check, and…
Generate Random XML Data - Test Files →
Generate random XML data with customizable elements, nesting, and attributes. Free, instant, and secure…
JSON Formatter and Validator →
Paste JSON to format or minify it. Errors are shown with the line, column…
SQL Formatter →
Formats SQL queries and scripts into readable, indented lines, or minifies them to one…
XML Sitemap Validator →
Check your sitemap XML against the protocol, with a well-formed check, URL and priority…
XML to CSV Converter →
Convert repeated XML records into CSV, with the first record setting the columns.
YAML to XML Converter →
Convert YAML with nested mappings and lists into clean, nested XML.