What the XML formatter does
XML stores structured data as nested tags. It is used in RSS feeds, sitemaps, SOAP APIs, Android layouts, Maven pom.xml files, Office documents and many data exports. Machines often write it as one long line. This tool lays it out with one element per line and consistent indentation, so the nesting is easy to follow.
There are three actions:
- Format indents the XML.
- Minify removes the whitespace between tags to make the file smaller.
- Validate checks that the document is well formed and reports the first problem.
How to use it
- Paste XML into the left box, press Open file, or press Sample. Pasted XML is formatted right away.
- Choose an Indent of 2 spaces, 4 spaces or Tabs.
- Press Format, Minify or Validate. Ctrl+Enter repeats the last action.
- Press Copy or Download to save the result as an
.xmlfile.
Before and after
A product catalog on one line:
<?xml version="1.0" encoding="UTF-8"?><catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><price>44.95</price></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><price>5.95</price></book></catalog>
After Format with 2 spaces:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<price>44.95</price>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<price>5.95</price>
</book>
</catalog>
Text content stays on the same line as its element. That matters, because putting a line break inside <price>44.95</price> would change the value a program reads.
Minify turns the formatted version back into the single line shown above. Validate on the same input reports: Well-formed XML. Root element <catalog>, 9 elements in total.
CDATA sections are left exactly as written, so <script><![CDATA[if (a < b) run();]]></script> comes out unchanged.
Errors the checker catches
Many XML formatters quietly “repair” broken markup, for example by closing a tag in a place you did not intend. This one checks the document first and stops at the first problem. These are real messages:
| Input | Message |
|---|---|
<to>Tove</from> |
Line 2, column 11: Closing tag </from> does not match <to> opened on line 2 |
Smith & Sons |
Line 1, column 16: A bare "&" must be written as & |
An <item> that is never closed |
Line 4, column 1: Closing tag </list> does not match <item> opened on line 3 |
<book id=101/> |
Line 1, column 10: The value of "id" must be in quotes |
| Two top-level elements | Line 2, column 1: Only one root element is allowed, found a second one: <b> |
Press Show in input to move the cursor to the problem.
What “well formed” means
An XML document is well formed when it follows the basic syntax rules:
- Every opening tag has a matching closing tag, or is self-closing like
<br/>. - Tags close in the reverse order they opened.
- There is exactly one root element.
- Attribute values are in single or double quotes, and no attribute appears twice on one element.
- A literal
&or<in text is written as&or<. - The
<?xml ... ?>declaration, if present, is the very first thing in the file.
Named entities such as are only allowed when a DOCTYPE declares them, because XML itself knows just five: <, >, &, ' and ".
Tips and limits
- Validation does not use a schema. A document can be well formed and still have the wrong elements for your application.
- Whitespace inside text is kept. If you need text reflowed, edit it by hand.
- Minify keeps comments. Delete them first if you need the smallest file.
- Everything runs locally, so internal configs and exports are safe to paste.
Other ways to format XML
xmllint. On Linux and macOS, xmllint --format file.xml prints an indented copy, and xmllint --noout file.xml only reports errors. It can also check against a schema with --schema.
VS Code. Install an XML extension (the Red Hat XML extension is a common choice), then run Format Document.
Notepad++. The XML Tools plugin adds a Pretty Print command.
Related tools
If your data is JSON, use the JSON Formatter. For configuration files, the YAML Formatter does the same job for YAML. For web pages, use the HTML Formatter, which understands HTML rules that XML does not allow.