Skip to main content
ponderloop

XML Formatter & Validator

Paste XML and it is checked on the spot: if it is well-formed it is pretty-printed, and if not you get the message, the line and column and the offending line with a caret under it. The example is 358 bytes of compact XML; pretty-printed with 2 spaces it is 403 bytes, and its mixed-content line, with the CDATA section inside, comes out exactly as typed. Comments, CDATA, processing instructions and the XML declaration are kept, and text is never changed: only whitespace between elements is re-indented or removed. You can also minify, and copy or download the result. Your XML never leaves the browser.

Updated Runs in your browser: nothing you enter is uploadedHow this tool works

Stays in your browser

Replace the example: paste or type XML. It is checked as you type.

Your XML is processed on this device and never uploaded. It is not saved either: reload the page and it is gone.

Options

Result

Well-formed XML: root element <catalog>

Size and structure

Before (UTF-8)
358 bytes
After (UTF-8)
403 bytes
Change
+45 bytes (+12.6%)
Depth
4
Elements
9
Attributes
5
Comments
1

What each layout produces

The same input, <a x="1"><b>text</b><c/></a>, with one option changed at a time (line breaks shown as ⏎).
LayoutResultEffect
2 spaces<a x="1"> ⏎ <b>text</b> ⏎ <c/> ⏎ </a>The default: one nesting level per two spaces.
4 spaces<a x="1"> ⏎ <b>text</b> ⏎ <c/> ⏎ </a>One nesting level per four spaces.
Tab<a x="1"> ⏎ →<b>text</b> ⏎ →<c/> ⏎ </a>One tab per nesting level (shown here as →).
Minify<a x="1"><b>text</b><c/></a>Whitespace between elements removed: the smallest text with the same content.

What is kept and what changes

Each input is formatted with 2 spaces (line breaks shown as ⏎).
CaseYou typeThe tool outputsWhy
Mixed content<p>Hi <b>there</b>, you.</p><p>Hi <b>there</b>, you.</p>Text next to child elements is data, so the whole element is copied as written.
Text-only element<r><n> two words </n></r><r> ⏎ <n> two words </n> ⏎ </r>Leading, trailing and repeated spaces inside text stay.
Whitespace between elements<r> ⏎ ⏎ <a/> ⏎ →<b/> ⏎ </r><r> ⏎ <a/> ⏎ <b/> ⏎ </r>Only this is replaced: it is re-indented, or removed when minifying.
CDATA section<r><![CDATA[ <x> & ]]></r><r><![CDATA[ <x> & ]]></r>Kept as a CDATA section; its content is text.
Comment and instruction<r><!-- note --><?go now?><a/></r><r> ⏎ <!-- note --> ⏎ <?go now?> ⏎ <a/> ⏎ </r>Each goes on its own line, unchanged.
Entity references<r a="&lt;">&amp; &#233; &#xE9;</r><r a="&lt;">&amp; &#233; &#xE9;</r>References are not expanded or re-encoded.
xml:space="preserve"<r><p xml:space="preserve"> ⏎ <b/> ⏎ </p></r><r> ⏎ <p xml:space="preserve"> ⏎ <b/> ⏎ </p> ⏎ </r>Marked elements are never re-indented.

Common mistakes and the message you get

Positions are 1-based; the column counts characters from the start of the line.
InputMessagePositionFix
<a> ⏎ <b>text</c> ⏎ </a>The end tag </c> does not match the open element <b> (opened at line 2, column 3). Close <b> first.line 2, column 10Make the end tag match its start tag.
<a><b>text</a>The end tag </a> does not match the open element <b> (opened at line 1, column 4). Close <b> first.line 1, column 11Close <b> before closing <a>.
<a>AT&T</a>The entity reference “&T” is missing its closing “;”. To write a literal ampersand, use &amp;.line 1, column 6Write &amp; for a literal ampersand.
<a>&nbsp;</a>Undefined entity “&nbsp;”. XML predefines only &lt; &gt; &amp; &apos; and &quot;. Use a character reference such as &#160;, or declare the entity in a DOCTYPE.line 1, column 4Use &#160; (XML only predefines five entities).
<a>1 < 2</a>Expected an element name after “<”, found a space. A literal “<” in text must be written &lt;.line 1, column 7Write &lt; for a literal less-than sign.
<a b=1/>The value of the attribute “b” must be in quotes, found “1”.line 1, column 6Put attribute values in quotes.
<a b="1" b="2"/>Duplicate attribute “b” on <a>.line 1, column 10Remove or rename the repeated attribute.
<a/><b/>Only one root element is allowed; <b> starts a second one (the first is <a>).line 1, column 5Wrap both elements in one root element.
<a><!-- x -- y --></a>“--” is not allowed inside a comment.line 1, column 11Remove the double hyphen inside the comment.
⏎ <?xml version="1.0"?><a/>The XML declaration must be the very first thing in the document: nothing, not even a space or line break, may come before <?xml.line 2, column 1Put the XML declaration at the very first character.

How to format and validate XML online

  1. Paste your XML. Replace the example with your own text. It is checked as you type; for very large input, press Format instead.
  2. Read the verdict. A well-formed document shows its root element, size and structure counts. An invalid one shows the message, the line and column, and the offending line with a caret.
  3. Choose the layout. Pick 2 spaces, 4 spaces, a tab or Minify. The result updates at once.
  4. Fix problems. Use “Show in input” to jump to the error, fix it, and the next error, if any, appears.
  5. Copy or download. Copy the result to the clipboard or download it as a .xml file. Nothing is uploaded at any step.

Frequently asked questions

How do I check whether XML is well-formed?
Paste it into the box. The tool reads it against the XML 1.0 rules and either shows the formatted result or the first error with its line, column and a caret under the offending character. Only the first error is reported, because one mistake often causes several confusing follow-ups; fix it and the next one, if any, appears.
What is the difference between well-formed and valid XML?
Well-formed means the text follows the XML syntax rules: one root element, matching tags, quoted attributes, legal characters and escaped special characters. Valid means it also conforms to a schema or DTD that lists which elements and attributes are allowed. This tool checks well-formedness only; it does not check a document against an XSD, Relax NG or DTD.
Will formatting change my text?
No. Text, CDATA sections, comments, processing instructions, entity references and attribute values are copied exactly as written. The only whitespace that changes is the blanks inside tags and the whitespace-only text between the child elements of an element that has no other text. An element that mixes text and child elements, such as <p>Hi <b>there</b></p>, is left exactly as typed, because the spaces around the child are part of the data.
Why does the tool keep some elements on one line?
An element whose content includes text or a CDATA section is printed on one line with its content untouched, since adding line breaks or indentation inside it would add characters to the text. Elements that only contain other elements, comments and instructions are broken onto separate lines. Elements marked xml:space="preserve" are never re-indented.
Why is &nbsp; an error in XML?
XML predefines only five entities: &lt;, &gt;, &amp;, &apos; and &quot;. Names such as &nbsp; or &copy; come from HTML, so an XML parser rejects them unless a DOCTYPE declares them. Use a character reference instead, such as &#160; for a non-breaking space or &#169; for the copyright sign, or just type the character.
Does it support DOCTYPE and entities declared in it?
Yes. A DOCTYPE, including an internal subset, is kept as written, and entities declared in it are accepted in text and attribute values (they are not expanded). If the DOCTYPE points to an external DTD, the tool cannot see what that file declares, so it accepts any well-formed entity reference. External files are never loaded.
What happens to the XML declaration and encoding?
A declaration such as <?xml version="1.0" encoding="UTF-8"?> is checked for the right shape and kept. It is only accepted as the very first thing in the document: a space or line break before it is an error, which is a common reason parsers reject files. The encoding value is not used to re-encode anything, because the text you paste is already characters. A leading byte order mark is removed, and the page says so.
Are namespaces checked?
Prefixed names such as ns:item are accepted as ordinary names. The tool does not check that a prefix is bound by an xmlns declaration, because that is a rule of the Namespaces in XML specification and not of XML itself.
How big a file can I paste?
About 5 MB works. Up to 500,000 characters the result updates a moment after you stop typing; above that the page waits for you to press Format so typing stays smooth. Elements nested deeper than 500 levels are refused.
Is my XML uploaded or saved?
No. The XML is checked and re-indented inside your browser tab and is never sent, saved or kept in local storage, so it disappears when you reload. With analytics allowed, the site logs only which button was pressed, never the document itself.

How it works

The text is read by a small tokenizer and parser written for this tool, following the well-formedness rules of XML 1.0 (fifth edition): exactly one root element, matching start and end tags, unique attribute names, quoted attribute values without a raw “<”, only legal characters, “&” only as part of a reference, and no “]]>” in text. Comments may not contain “--”, the XML declaration must come first, and a DOCTYPE must precede the root. It does not use the browser’s DOMParser, so error positions and messages are the same in every browser and the page can show its example without scripts. Line and column are counted in Unicode characters, with LF, CRLF and a lone CR each ending a line. Formatting prints the parsed tree again. Each tag is rewritten with single spaces between attributes, with attribute values and their quotes unchanged. An element whose content has non-blank text or a CDATA section, that is marked xml:space="preserve", or that has no markup inside is copied verbatim; any other element puts each child on its own line, with the whitespace-only text between children replaced by the chosen indentation (or by nothing when minifying). CRLF and CR in the output become LF, as an XML parser would read them. Depth is the nesting of elements (the root alone is 1); sizes are UTF-8 byte counts of the text before and after. Namespace bindings and schemas are not checked.

Sources

More: Formatters and data convertersAll developer toolsAll toolsHome & DIY calculators