FormatterHQ

XML Formatter

Paste one-line or badly indented XML and get clean, nested markup. The XML formatter and the XML beautifier are the same tool. The document is checked for well-formedness first, so broken tags are reported with their line and column.

Input XML
Output XML
Ready.

How to use the XML Formatter

  1. Paste XML into the input editor, drop an .xml file on it, use Upload file or Load URL, or click Sample.
  2. Choose an Indent (2, 3 or 4 spaces, or Tab) and tick Remove comments if the output should not contain <!-- --> blocks.
  3. Click Format; with Auto update on, the output refreshes as you type. Minify and Validate are one click away.
  4. If the status bar shows an error, go to the reported line and column, fix the markup and the output updates.
  5. Use Copy or Download to take the formatted XML with you.

An XML formatter that checks the markup first

This XML formatter takes the single-line XML you get from APIs, exports and log files and rebuilds it with one element per line and consistent indentation. Before anything is re-indented, the input is parsed as XML. If a tag is left open or an attribute is malformed, you get an error with a line and column instead of a half-formatted document.

Formatting changes the whitespace between tags, trims the whitespace around text that spans several lines and writes attribute values with double quotes. Element names, attribute values, CDATA sections and the XML declaration come through untouched.

Example

An order exported by a web shop, all on one line:

<?xml version="1.0"?><order id="42"><!-- rush --><customer>Ava</customer><items><item sku="A1" qty="2">Mouse</item><item sku="B7" qty="1"/></items></order>

After Format with the default 2-space indent:

<?xml version="1.0"?>
<order id="42">
  <!-- rush -->
  <customer>Ava</customer>
  <items>
    <item sku="A1" qty="2">Mouse</item>
    <item sku="B7" qty="1"/>
  </items>
</order>

Notice that <customer>Ava</customer> stays on one line. Elements that contain only text are kept compact, which is what most people expect from a beautifier and keeps the output short.

Options and buttons

  • Indent – 2, 3 or 4 spaces, or a tab character per nesting level.
  • Remove comments – drops every <!-- ... --> block from the output. It is off by default, so comments are kept in place.
  • Minify – the reverse operation: removes indentation and line breaks and reports the size before and after.
  • Validate – confirms the XML is well-formed and names the root element and the number of elements.

What the formatter preserves

XML is less forgiving about whitespace than JSON, because text is data. The formatter is careful in a few places:

  • Mixed content. In <p>Hello <b>big</b> world</p>, the spaces around <b> are part of the sentence, so such elements are left as they are.
  • Text inside leaf elements. <t> spaced </t> keeps its leading and trailing spaces. Text that spans several lines is trimmed: the whitespace before its first word and after its last word is removed. An element that contains only whitespace, such as <a> </a>, becomes empty, and whitespace between sibling elements is replaced by indentation unless xml:space="preserve" is set.
  • xml:space="preserve" and CDATA sections are output verbatim.
  • Empty elements keep the form you wrote: <b/> stays self-closing and <c></c> stays expanded.

One cosmetic change to expect: attribute values are always written with double quotes. <a b='x'> becomes <a b="x">, and a double quote inside such a value is escaped as &quot;, so the meaning does not change.

Errors you will see and what they mean

Status bar messageTypical cause
Expected closing tag 'b' … instead of closing tag 'a'An element was not closed, or tags overlap like <a><b></a></b>.
Unclosed tag 'a'The document ends before the root element is closed, often a truncated file.
char '&' is not expectedA raw ampersand in text. Write &amp; or wrap the text in CDATA.
Multiple possible root nodes foundTwo top-level elements. XML allows exactly one root.
Attribute 'id' is without valueHTML-style <b id=1> or <input checked>. Quote every value.
Attribute 'x' is repeatedThe same attribute appears twice on one element.

Tag names are case-sensitive, so <Item></item> is also reported as a mismatched closing tag.

Formatter, validator or viewer?

Use the formatter when you want text to paste back into a file, a ticket or a code review. The XML validator focuses on the error report, and the XML viewer shows the document as a collapsible tree for exploring large files. If you are comparing two versions of a document, format both and run them through XML diff so indentation differences do not hide the real changes.

Privacy

Formatting runs entirely in your browser, and the XML you paste or upload is not sent to our server. Load URL fetches a remote file through our server, and Save & Share stores the input so the link can be opened later; both only happen when you use them.

Frequently asked questions

Does formatting change my XML data?
No element or attribute value is changed, and text written on one line stays as it is. Whitespace between tags is added, text that spans several lines loses the whitespace before and after it, and single-quoted attribute values are rewritten with double quotes, which XML treats as identical.
Why is some text not split onto separate lines?
Elements that hold only text, and elements that mix text with child tags, are deliberately kept together. Adding line breaks inside them would change the text content that applications read.
Is XML Beautifier the same as XML Formatter?
Yes. Beautifier, pretty printer and formatter describe the same job: re-indenting markup so the nesting is visible. This page handles all of them and also offers minify and validate.
Can I format very large XML files?
Files of several megabytes format fine because everything runs locally in your browser. Very large documents take longer and use more memory; turn off Auto update while editing them so the output is only rebuilt when you click Format.
Does the formatter check my XML against an XSD schema?
No. It checks well-formedness: matching tags, quoted attributes, one root element and correct escaping. Schema validation against an XSD or DTD needs a schema-aware tool such as xmllint or your IDE.

Last updated October 3, 2026

Guides

Related tools