What is an XML formatter?
XML is a text format that describes data with nested tags such as <order id="7"><item>Pen</item></order>. Computers do not care about line breaks, so many systems send XML on a single line: SOAP responses, RSS and Atom feeds, sitemaps, Android resources, Maven pom.xml files, SVG images, invoices and tax documents such as the Brazilian NF-e. An XML formatter (also called an XML beautifier or pretty printer) puts every element on its own line and indents the children, so you can see the structure at a glance.
This XML formatter does two jobs. First it checks that your text is well-formed XML: tags open and close in the right order, attribute values are quoted, special characters are escaped, and there is a single root element. If something is wrong it tells you the line and column and what to change, instead of returning an empty result. Then it formats the text with the indent size you choose, or minifies it by removing the whitespace between tags, which is handy for a request body or a config value that must fit on one line.
It works in your browser and never uploads what you paste. Keep in mind that it checks well-formedness (the rules of the XML syntax), not validity: it does not compare the document against an XSD or a DTD, so it will not tell you that a required element is missing.
How to format XML online
- Paste your XML in the input box. The page opens with a small example, so you can see the result before you paste anything; the example button brings it back, and the clear button empties the box.
- Read the formatted XML below the box. It updates while you type, a moment after you stop.
- Change the Indent size (0 to 10 spaces, 2 by default). Use 4 if your team's style guide asks for it, or 0 to put each element on its own line without indentation.
- Decide what to do with Collapse content. When it is on, an element that holds only text stays on one line (
<title>Dom Casmurro</title>) and text mixed with tags stays together. When it is off, every piece of text goes on its own line. - Turn on Minify to get the opposite result: all the whitespace between tags is removed and the document becomes a single line. The indent option is hidden in this mode.
- Press the copy button to take the result. If the box shows an error instead, read the line and column it points to, fix the text and the output comes back by itself.
Examples
Format a one-line document
Input:
<?xml version="1.0" encoding="UTF-8"?><catalog><book id="1"><title>Dom Casmurro</title><author>Machado de Assis</author></book><book id="2"><title>Grande Sertao</title><author>Guimaraes Rosa</author></book></catalog>Output with indent 2:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="1">
<title>Dom Casmurro</title>
<author>Machado de Assis</author>
</book>
<book id="2">
<title>Grande Sertao</title>
<author>Guimaraes Rosa</author>
</book>
</catalog>Minify a formatted document
Input:
<catalog>
<book id="1">
<title>Dom Casmurro</title>
</book>
</catalog>Output with Minify on:
<catalog><book id="1"><title>Dom Casmurro</title></book></catalog>What Collapse content changes
With <p>Hello <b>big</b> world</p>, the option on keeps the paragraph as it is. With it off, the same text is split so that every text node and element has its own line:
<p>
Hello
<b>
big
</b>
world
</p>Use the second form only when whitespace does not matter in your document, because it adds line breaks inside text.
An error with its position
Input:
<a>
<b>x</c>
</a>The tool answers that the closing tag </c> does not match <b>, opened on line 2, and points at line 2, column 7.
Common errors and how to fix them
- A closing tag does not match the opening tag: you wrote
<b>…</c>or closed the parent before the child. The message names both tags and the line where the tag was opened. - The value of an attribute must be in quotes:
<a x=1/>is valid HTML-style text but not XML. Writex="1", with single or double quotes. - A "&" must be written as &: a bare ampersand, as in
Tom & Jerry, is not allowed. Write&, or wrap the text in a CDATA section. The HTML entities page lists the others. - Unknown entity : XML only predefines
&,<,>,"and'. Use a numeric reference such as or declare the entity in a DOCTYPE. - Only one root element is allowed: two top-level elements, such as
<a/><b/>, are not a document. Wrap them in one parent. - Text before the root element: something such as a stray word or a log prefix came before the first tag. Remove it.
- The XML declaration must be at the very start:
<?xml ...?>cannot follow another element. Leading spaces and blank lines are trimmed for you.
Frequently asked questions
How do I format XML in the browser without installing anything?
Paste it in the box above. The result appears below, already indented, and you can copy it. Nothing is installed and nothing is sent to a server.
How do I minify XML?
Turn on the Minify switch. The whitespace between tags is removed and you get a single line. To do the same for JSON use JSON minify.
Does the formatter validate my XML?
It checks that the XML is well-formed and reports the first problem with its line and column. It does not validate against an XSD or DTD, so a document can pass here and still be rejected by a system that requires a specific schema.
Does formatting change my data?
Formatting only changes whitespace between elements. If you need to be sure, compare the before and after with the text diff, or turn on Minify, which brings the document back to one line.
How do I convert XML to JSON?
Format and check the XML here first, then use XML to JSON. To go the other way use JSON to XML.