About the XML Formatter
XML often arrives as one long line: an API response, a sitemap, an RSS feed, an SVG icon or a config file exported by another program. The XML Formatter puts every element on its own line and indents it to show how the elements nest, with tag names, attribute values, comments and CDATA in different colors.
Every input is checked with your browser's own XML parser. If the XML is not well-formed, for example because a tag is never closed or an ampersand is not escaped, the status line tells you the line and column where the problem was found.
Minify does the opposite and removes the line breaks and indentation between tags, which makes files smaller for sending and storing. You can also remove comments in either mode. Everything runs in your browser, so your XML never leaves your device.
How to use it
- Add your XMLPaste it into the Input box, drop a file onto it, or use Open file. XML, SVG, RSS, Atom, XSD, XSL, plist and .config files all work. Sample loads a small book catalog.
- Choose Format or MinifyFormat spreads the XML over indented lines. Minify puts it back on one line without the whitespace between tags.
- Pick an indentChoose 2 spaces, 4 spaces or tabs for formatted output.
- Remove comments if you likeTurn on Remove comments to drop every <!-- ... --> comment from the output.
- Copy or downloadCopy the result, or download it as formatted.xml or minified.xml.
Key features
Validation built in
Each change is checked by the XML parser in your browser. "Valid XML" plus the number of elements means the document is well-formed.
Short elements stay on one line
An element that holds only text, like <title>The Long Way Home</title>, stays on one line instead of being split into three.
Everything kept
The XML declaration, DOCTYPE, comments, CDATA sections, entities like & and attribute quotes are copied exactly.
Indent and minify
2 spaces, 4 spaces or tabs for reading, or one compact line for sending.
Syntax colors
Tag names, attribute values, comments, declarations and CDATA each have their own color, so the structure is easy to scan.
Private by design
Your XML is formatted in your browser. Nothing is uploaded or stored.
Examples
Formatting a catalog
The Sample button loads a 422-byte catalog on a single line. Formatted with 2 spaces, it becomes 22 lines, and the status line reads Valid XML with 14 elements · 22 lines · 515 B:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!-- spring list -->
<book id="b1" lang="en">
<title>The Long Way Home</title>
<author>R. Patel</author>
<price currency="USD">12.99</price>
<tags>
<tag>fiction</tag>
<tag>travel</tag>
</tags>
</book>
<book id="b2">
<title>Cooking & Lentils</title>
...
Notice that & stays escaped and the CDATA section in the second book is kept as it is.
Indent size and file size
| Option | Lines | Size |
|---|---|---|
| Minify | 1 | 422 B |
| Tabs | 22 | 479 B |
| 2 spaces | 22 | 515 B |
| 4 spaces | 22 | 587 B |
| 2 spaces, Remove comments | 21 | 492 B |
Tidying an SVG icon
This SVG, pasted on one line:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill="none" stroke="currentColor"/><path d="M8 12l3 3 5-6"/></svg>
comes out as 4 lines and 3 elements:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="10" fill="none" stroke="currentColor"/>
<path d="M8 12l3 3 5-6"/>
</svg>
Errors the parser catches
| You paste | The problem |
|---|---|
<a><b>text</a> | <b> is never closed |
<Note></note> | Tag names are case-sensitive |
<a>Tom & Jerry</a> | & must be written & |
<a id=1/> | Attribute values need quotes |
<a/><b/> | Only one root element is allowed |
<br> | Empty tags must end with /> |
The exact wording of the message comes from your browser, so Chrome, Firefox and Safari phrase it differently. Each one gives the line and column.
Tips
- Keyboard users can format with Ctrl Enter and minify with Ctrl Shift M; on a Mac, use Cmd instead of Ctrl.
- Format a sitemap or RSS feed before you read it. Problems such as a missing <loc>, a stray tag or an unescaped & are much easier to spot on separate lines.
- Minify SVG icons before you inline them in HTML or CSS, and turn on Remove comments to drop the notes that design programs often add.
- When the formatter reports an error you cannot see, look just before the reported position. A tag that was never closed is often reported where the parent element ends.
- If your XML has an encoding other than UTF-8 in its declaration, open it as a file in a text editor first and save it as UTF-8, then paste or open it here.
Common mistakes and limits
- Formatting documents with mixed textIn XHTML, DocBook or similar files, text and tags are mixed, as in <p>Hello <b>world</b> again</p>. Formatting puts each piece on its own line and trims the spaces around the text, which changes the spacing when the text is displayed.
- Expecting Minify to keep spaces between tagsMinify removes whitespace that sits between two tags. In <b>world</b> <i>again</i> the space between the elements is dropped. For data XML that is what you want; for mixed text it is not.
- Treating HTML as XMLHTML allows <br>, unquoted attributes and unclosed <p> tags. XML does not, so most HTML pages fail here. Use the HTML Beautifier for HTML.
- Confusing well-formed with validThis tool checks that the XML is well-formed: tags match, attributes are quoted and so on. It does not check the document against a schema (XSD) or a DTD.
- Putting anything before the XML declarationThe <?xml ...?> line must come first. A comment or an element before it makes the XML invalid. This tool ignores blank lines at the start, but many other programs reject even those.