XML Formatter
Beautify or minify XML after checking that it is well-formed. Mixed content, CDATA and comments are kept intact.
XML Formatter: the tool
Input
Formatted XML
The result appears here.How to use XML Formatter
- Paste XML into the left panel, or press Example.
- Choose Beautify with 2 spaces, 4 spaces or tabs, or Minify to remove the whitespace between elements.
- Turn on Remove comments to drop <!-- --> comments from the result.
- If the XML is not well-formed, the status bar shows the line and column of the first problem.
Example
A one-line catalog with a comment and an entity, beautified with 2 spaces.
<?xml version="1.0" encoding="UTF-8"?><catalog><book id="b1"><title>Compilers</title><price currency="EUR">39.90</price></book><!-- second title --><book id="b2"><title>Networks & Protocols</title><price currency="EUR">45.00</price></book></catalog>
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="b1">
<title>Compilers</title>
<price currency="EUR">39.90</price>
</book>
<!-- second title -->
<book id="b2">
<title>Networks & Protocols</title>
<price currency="EUR">45.00</price>
</book>
</catalog>About this tool
The formatter first checks the document against the XML 1.0 syntax rules, so a formatted result is also a well-formed one. It then re-indents it so that every element that contains only other elements puts each child on its own line.
Whitespace inside text matters in XML, so the tool is careful. An element that contains text, including mixed content such as <p>Hello <b>big</b> world</p>, is written exactly as it was, because adding line breaks there would change the document. Only whitespace between elements, which has no meaning in data documents, is re-indented or removed. Attribute values, entities, CDATA sections, processing instructions and the DOCTYPE are kept as written, and self-closing elements stay self-closing.
The check is for well-formedness, not for validity. The document is not compared to a DTD or an XML Schema, and namespace prefixes are not resolved. Large documents are processed without recursion, so very deep nesting does not crash the page.
Frequently asked questions
Does formatting change the meaning of my XML?
No. Only whitespace between elements is changed, and elements that contain text are left exactly as written, so mixed content keeps its spaces.
What is the difference between well-formed and valid XML?
Well-formed means the syntax is correct: tags match, attributes are quoted, special characters are escaped. Valid means the document also follows a DTD or a schema. This tool checks the first only.
Why does the formatter refuse my document?
Usually a mismatched tag, an unescaped & or <, an unquoted attribute or a second root element. The message names the problem and its position.
Are comments and CDATA kept?
Yes. Comments are kept unless you turn on Remove comments, and CDATA sections are written exactly as they were.
Can it handle namespaces?
Prefixes and xmlns declarations are kept as written. They are not resolved or checked, which is outside the syntax rules.
More developer tools
- <ok>XML ValidatorCheck that XML is well-formed and get the line and column of the first error, with a summary of elements, attributes and namespaces.
- →xmlJSON to XML ConverterConvert JSON to well-formed XML with a root element you choose. Arrays repeat their element, and invalid names are repaired and reported.
- <//>HTML MinifierReduce the size of HTML by removing comments and collapsing whitespace. Pre-formatted text, scripts and conditional comments are left alone.
- ymlYAML Formatter & ValidatorValidate and re-indent YAML while keeping comments, anchors and tags. Syntax errors point to the exact line and column.