How to Format XML and Find Errors (Well-Formed vs Valid)

Learn how to pretty-print or minify XML, what makes a document well-formed, how that differs from schema validation, and how to fix the most common XML errors.

Updated September 29, 2026 5 min read

Use the free XML FormatterBeautify, validate and minify XML documents

XML is still everywhere behind the scenes: product feeds for shopping platforms, sitemaps, RSS and Atom feeds, SOAP web services, office document internals, Android layouts, invoice and banking standards. It often arrives as a single unbroken line, and when something rejects it, the error message rarely says more than "parsing failed".

This guide explains how to turn compressed XML into readable, indented markup and back again, what "well-formed" really means, why a well-formed file can still be rejected, and how to fix the handful of mistakes that cause most XML errors.

Anatomy of an XML document

A short example shows the parts a formatter has to handle:

<?xml version="1.0" encoding="UTF-8"?>
<!-- product feed -->
<catalog>
  <book id="bk101" lang="en">
    <title>XML Basics</title>
    <price currency="EUR">24.00</price>
    <description><![CDATA[Covers <elements>, attributes & namespaces.]]></description>
    <instock/>
  </book>
</catalog>
  • The declaration on the first line states the version and encoding.
  • Elements such as <book> have a start tag and an end tag, or are self-closing like <instock/>.
  • Attributes like id="bk101" sit inside start tags, always quoted.
  • Comments use <!-- -->.
  • A CDATA section holds text that may contain < and & without escaping.

Well-formed vs valid

These two terms are often confused, and the difference explains many "but it looks fine" moments.

Well-formed means the document follows XML's basic syntax rules:

  • every start tag has a matching end tag, or is self-closing;
  • elements are properly nested (<a><b></b></a>, never <a><b></a></b>);
  • tag names match exactly, including case (<Note> must close with </Note>);
  • attribute values are quoted, and no attribute appears twice on one element;
  • the characters < and & in text are escaped as &lt; and &amp;, or placed in CDATA;
  • there is exactly one root element.

Valid goes further: the document also matches a schema (an XSD, DTD or RelaxNG file) that defines which elements are allowed, in what order, with what types. A sitemap with a misspelt <lastmode> element is perfectly well-formed but invalid against the sitemap schema.

A formatter checks well-formedness because it must parse the document to reformat it. Schema validation needs the schema file and a validating parser, which is a separate step.

Format or minify XML with our tool

The XML Formatter checks and reformats XML as you type, in your browser.

  1. Open the XML Formatter. Paste into XML input, or use Open file for .xml, .svg, .rss, .xsd or .xsl files up to 20 MB. Load sample shows a one-line catalogue with a comment and a CDATA section.
  2. Choose the Mode: Format / beautify or Minify.
  3. When formatting, choose Indentation: 2 spaces, 4 spaces or Tab. When minifying, decide whether to Keep comments.
  4. Check the output pane (Formatted XML or Minified XML), then Copy it or Download it. Downloads are named with a -formatted or -minified suffix.

If the document is not well-formed, the output pane shows Error on line X, column Y and a message such as "Expected closing tag 'b' (opened in line 1, col 4) instead of closing tag 'a'." That pinpoints mismatched tags, unescaped ampersands and case differences. Elements that contain only text stay on one line (<title>XML Basics</title>), so the result stays compact. The tool checks syntax only; it does not load or apply schemas.

Fixing the most common XML errors

Unescaped ampersand

<company>Smith & Sons</company> is the classic error. The parser sees & as the start of an entity reference. Write Smith &amp; Sons, or wrap the text in CDATA. The same applies to < in text, which must be &lt;. Feeds built by joining strings in code often break this way; build XML with a proper library, or escape values with the HTML Entity Encoder, which uses the same entities.

Mismatched or crossed tags

<b><i>text</b></i> is invalid because the tags overlap. Close them in reverse order of opening. With formatting, the indentation makes these mistakes easy to see.

Case mismatch

XML is case sensitive. <Price> and </price> are different elements. Pick one spelling, ideally the one the schema uses.

Content before the declaration

If an XML declaration is present, nothing may come before it, not even a blank line or a byte order mark in some parsers. Delete any leading whitespace.

Multiple root elements

A document must have exactly one top-level element. Concatenating two XML files, or returning a list of records without a wrapper, breaks this rule. Wrap them in a parent such as <items>…</items>. Not every checker reports this one clearly, especially when the extra top-level elements are self-closing like <item/>. Our formatter checks for it separately and reports an error such as "Multiple root elements: appears after the root element has closed" with the line and column of the second top-level element.

Whitespace: when formatting changes meaning

For data-oriented XML, formatting is harmless because whitespace between elements is ignored. Two situations need care:

  • Mixed content, where text and elements are interleaved (<p>Price: <b>10</b> EUR</p>), is common in document formats. Re-indenting can add or remove spaces around the inline element.
  • xml:space="preserve" marks content whose whitespace matters exactly, such as some text in office formats.

For these documents, format a copy for reading and keep the original for processing. Minifying is also lossy for comments unless you keep them.

Other ways to format XML

xmllint

On macOS and most Linux systems, xmllint (part of libxml2) formats and validates:

xmllint --format feed.xml > feed-pretty.xml
xmllint --noout --schema sitemap.xsd sitemap.xml   # schema validation

It is the tool to use for real schema validation and for large files.

Code editors

VS Code with an XML extension, IntelliJ-based IDEs and Notepad++ with its XML Tools plugin can all format documents and highlight errors. An editor is the better choice when you will keep editing the file.

Browsers

Opening an XML file directly in a desktop browser shows a collapsible tree for well-formed files and an error message for broken ones. The XML Viewer gives a similar tree view for pasted text.

If you need the data in a format that is easier to work with in code, the XML to JSON converter turns elements and attributes into JSON properties.

Frequently asked questions

What is the difference between well-formed and valid XML?

Well-formed XML follows the basic syntax rules: matched tags, quoted attributes, escaped special characters and one root. Valid XML is well-formed and also conforms to a schema that defines allowed elements and structure.

Does formatting XML change the data?

For data-oriented XML, no; only whitespace between elements changes. For documents with mixed text and inline elements, reformatting can alter spaces, so keep the original.

Why does my XML fail on an ampersand?

In XML, & starts an entity reference. A literal ampersand in text or an attribute must be written as &amp;.

Can the formatter validate against an XSD?

No. It checks that the document is well-formed. Use a validating tool such as xmllint --schema or your IDE for schema validation.

Can I format SVG files with it?

Yes. SVG is XML, so you can open .svg files and indent or minify them. For SVG-specific size savings, the SVG Optimizer does more.

Try it now: XML Formatter

Loading tool…