How to Format XML: Indentation, Errors and Tips

Last updated: · 3 min read

XML is used in configuration files, SOAP messages, RSS feeds and countless data exchanges. When a document arrives squeezed onto one line, it is very hard to see which element sits inside which other element. The XML Formatter parses the text and indents each child element two spaces deeper than its parent. Comments, CDATA sections, processing instructions and attributes are kept exactly as they are. If the document is not well-formed, the tool reports the line number of the problem. Everything runs in your browser, so your text is never sent to a server.

Formula

1. Parse the text according to the XML 1.0 syntax: exactly one root element, and every tag must be closed.
2. Indent each element by two spaces for every level of nesting.
3. Keep text content, attribute values and self-closing tags unchanged.
4. Write comments, CDATA sections and processing instructions on their own lines.
5. On a syntax error, stop parsing and report the line, the column and the offending line.

This is not a mathematical formula but a sequence of parsing and writing steps. Only spaces and line breaks are added or removed; element names, attributes and text stay the same. The well-formedness rules are defined in the W3C Recommendation Extensible Markup Language (XML) 1.0 (Fifth Edition).

What the inputs mean

XML
The XML text to format. Copy it from a file or paste an API response. It can be on one line or irregularly indented, as long as the document is well-formed.

Worked example: Indenting an order document

  1. Input: <order id="7"><item sku="A1">Pen</item><item sku="B2"/></order>
  2. The root element order has id="7" and contains two item elements.
  3. The first item holds the text Pen; the second is self-closing (<item sku="B2"/>).
  4. Both item lines move one level deeper and start with two spaces.
  5. The output has four lines; the attributes and the self-closing tag are unchanged.

Result: The content is identical and only readability has improved: four clear lines instead of one long string.

📄 XML Formatter

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Formatting changes only spaces and line breaks. In mixed content, where text and elements are interleaved as in documentation, whitespace can carry meaning, so format production data only for viewing and keep the original file. This is general information; conformance to a schema (XSD) has to be checked separately.

Common mistakes

  • Closing an attribute value with a different quote type than the one that opened it; in XML both quotes must match.
  • Mixing up case: <Item> and <item> are two different elements.
  • Putting more than one root element in a file; an XML document can have only one.
  • Leaving an unescaped & in the text; it must be written as &amp;, otherwise the parser reports an error.
  • Using two hyphens (--) inside a comment; that sequence is not allowed in XML comments.

Frequently asked questions

Does formatting change the data?

No. Names, attributes and text stay the same. Only spaces and line breaks between tags are added or removed.

Are comments and CDATA sections preserved?

Yes. Comments, CDATA sections and processing instructions are carried over to the output.

Why does the tool show a line number for errors?

The parser reads the document from top to bottom and stops at the first rule it breaks. The line shown is usually right at or just after the faulty tag.

Can I format confidential XML without sending it anywhere?

Yes. Processing happens in your browser and the text is not sent to a server.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice