How to format XML with this tool
- Paste your XML into XML input. The page opens with a small book catalog so you can see a result at once.
- Choose Mode. Beautify puts each element on its own line. Minify removes the whitespace between elements.
- Choose Indent (2 spaces, 4 spaces or Tab). It applies to Beautify only.
- Tick Remove comments if you want comments left out of the result.
- Read the line under Formatted XML. Green means well formed. Red gives the line, column and reason for the first error, and Show error moves the cursor there.
- Click Copy result to copy the output.
How the checking and formatting work
Your browser's built-in XML parser (DOMParser) decides whether the document is well formed under the W3C XML 1.0 specification: one root element, tags that close in the right order, quoted attribute values and legal entities. It reports the first error only; fix it and the message updates.
The formatting is separate code that reads your text itself instead of re-writing it from the parsed tree. Start tags, attribute quotes, entities such as &, comments and CDATA are copied exactly as typed. An element that holds only child elements puts each child on its own line, one level deeper. An element that holds only text stays on one line, with its text untouched, so <title> Sample </title> keeps its spaces. Text mixed with tags, as in <p>Hello <b>World</b></p>, and any element marked xml:space="preserve" are copied unchanged, because the spaces there are part of the content.
Worked example
With the defaults (Beautify, 2 spaces, comments kept), this input from the page:
<?xml version="1.0" encoding="UTF-8"?><catalog><!-- Two sample books --><book id="b1"><author>Maria Lopez</author><title>Pocket Guide to XML</title><price>19.95</price></book><book id="b2"><author>Sam Okafor</author><title>Reading Feeds</title><price>12.50</price></book></catalog>
becomes:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!-- Two sample books -->
<book id="b1">
<author>Maria Lopez</author>
<title>Pocket Guide to XML</title>
<price>19.95</price>
</book>
<book id="b2">
<author>Sam Okafor</author>
<title>Reading Feeds</title>
<price>12.50</price>
</book>
</catalog>
The status line then reads: Well-formed XML. Root element <catalog>, 9 elements.
Limits and edge cases
- This is a well-formedness check, not schema validation. It does not compare your XML to an XSD or DTD. For that, run
xmllint --schemafrom libxml2. - XML only predefines five entities:
<,>,&,"and'. HTML entities such as are errors unless a DTD declares them. - The
<?xml ...?>declaration must be the first thing in the text. A blank line or space before it is an error. - Your XML stays in your browser. A 1 MB file formats in about two seconds; much larger files work but make the text boxes slow.
Frequently asked questions
How do I find the error in my XML?
Paste the XML and read the red message under the result. It gives the line and column of the first error and the reason, such as a closing tag that does not match its opening tag. Click Show error to jump to that spot, fix it, and the message updates as you type.
Can it validate against an XSD schema?
No. It checks that the XML is well formed, meaning the tags nest correctly and the syntax is legal. It does not compare your document to an XSD or DTD. For schema validation use a command-line tool such as xmllint with the --schema option.
Does formatting change my data?
No. Text inside elements, attribute values, entities, comments and CDATA are copied exactly as you typed them. Beautify only changes the whitespace between elements. Text that sits next to child tags, as in a paragraph with bold words, is left untouched because its spaces are part of the content.
Can I format a sitemap.xml or RSS feed?
Yes. Any well-formed XML works, including sitemap.xml, RSS and Atom feeds, SVG, SOAP messages, Android layouts and pom.xml files. Paste the file contents, pick Beautify or Minify, and copy the result.
Working with other formats? Try the XML to JSON converter, the JSON formatter and validator, the YAML formatter or the HTML beautifier.