How to use the XML formatter tool
- Paste a complete XML document into the XML box, or select Example to load a small sample.
- Select Format XML to parse the document and indent it.
- Read the indented document in the Result box. If the status line says Enter a well-formed XML document, fix the syntax and try again.
- Select Copy to copy the result, or Download to save it as formatted.xml.
How the XML formatter indents a document
The tool hands your text to the browser's built-in XML parser. If parsing succeeds, it walks the resulting tree and writes each node back out with the browser's serializer, adding a line break and two spaces of indentation for every level of depth.
The key rule concerns text. An element whose children are only other elements is opened on one line, its children are listed below it, and it is closed on a line of its own. An element that directly contains non-blank text or a CDATA section is written on a single line exactly as serialized, including any elements nested inside it. This keeps mixed content, such as a sentence with inline markup, from gaining spaces or line breaks that were not in the source.
Comments, processing instructions and a DOCTYPE each get their own line. An XML declaration at the very start of the input is copied to the first line of the output.
Worked example with mixed content
The first line below is the input and the rest is the result. The order and items elements contain only elements, so they are expanded. The note element mixes text with a b element, so it stays on one line exactly as written.
<?xml version="1.0" encoding="UTF-8"?><order id="42"><customer>Ana Lima</customer><note>Ship <b>today</b> please</note><items><item sku="A1" qty="2"/><item sku="B7" qty="1"/></items></order>
<?xml version="1.0" encoding="UTF-8"?>
<order id="42">
<customer>Ana Lima</customer>
<note>Ship <b>today</b> please</note>
<items>
<item sku="A1" qty="2"/>
<item sku="B7" qty="1"/>
</items>
</order>Changes the serializer makes
The output is produced from the parsed tree, not by editing your text, so a few details are normalized. The document means the same thing to an XML parser, but a byte-for-byte diff will show these differences.
- Existing line breaks and indentation between elements are discarded and replaced with two-space indentation.
- Attribute values are written in double quotes, with any inner double quote as ".
- An element with a separate closing tag and nothing inside becomes a self-closing tag.
- Numeric character references and ' in text are written as the literal characters, while & and < stay escaped.
- Namespace declarations are repeated on nested elements that get their own line, for example xmlns on every child of an SVG root.
- An element that contains only whitespace is split across two lines, so that whitespace is lost even when xml:space is set to preserve.
Why an XML document is rejected
The parser enforces the well-formedness rules of the XML 1.0 specification, and the tool shows the same message for every failure: Enter a well-formed XML document. These are the usual causes.
- A tag that is opened but never closed, or tags closed in the wrong order.
- More than one root element, or text outside the root.
- A bare ampersand or less-than sign in text, which must be written as & or <.
- Spaces or other characters before the XML declaration, which has to be the very first thing in the document.
- A namespace prefix that is never declared, or an entity that XML does not define, such as .
Frequently asked questions
Does the formatter change my data?
Text inside elements is not re-wrapped or indented, so values keep their characters. The syntax around them is normalized: quotes, self-closing tags and some character references are rewritten, and whitespace-only content between tags is replaced by indentation.
Is my XML sent to a server for formatting?
No. Parsing uses the DOMParser built into your browser, and both the formatted text and the formatted.xml download are produced on the page. Config files and API payloads stay on your machine.
Can I choose tabs or four spaces for indentation?
No. The indent is fixed at two spaces per level and the form has no option for it. If your project needs another width, convert the leading spaces in your editor after downloading the file.
Can the tool validate XML against an XSD or DTD?
No. It checks well-formedness only: matching tags, a single root, legal characters and declared namespaces. A document can pass here and still break the rules of a schema. Schema validation needs a validating parser.