How to format and validate XML: a broken example, fixed

By Hammad Rasool · November 16, 2026

Quick answer: to validate XML, run it through an XML parser and fix the first error it reports, then run it again. Parsers stop at the first problem, so a file with four mistakes shows you four errors one after another. Once the parser accepts the file, formatting it is safe. The XML formatter does both in your browser: it reports the error with a line and column, and beautifies or minifies the XML once it is valid.

How this was tested: on 16 November 2026, every snippet on this page was parsed with DOMParser in Chrome 157. The error messages below are copied from that browser. Firefox and Safari report the same mistakes with different wording.

What "valid" XML means here

There are two levels of correctness. Well-formed XML follows the syntax rules: every element is closed, tags nest properly, attribute values are quoted, special characters are escaped, and there is exactly one root element. Schema-valid XML is also well formed and additionally matches a DTD or XML Schema (XSD) that says which elements and attributes are allowed.

A browser-based formatter checks the first level. That catches the mistakes people actually make when editing XML by hand. Checking against an XSD needs a schema-aware tool such as xmllint --schema.

A broken example

This order document has four separate mistakes:

<order id=1001>
  <customer>Ali Raza</Customer>
  <note>Pens & paper</note>
  <items>
    <item sku="PEN-01" qty="2">Blue pen</item>
  </items>
</order>
<order id="1002"/>

Here is what the parser says as each one is fixed in turn.

1. Attribute value without quotes

error on line 1 at column 11: AttValue: " or ' expected

HTML lets you write id=1001; XML does not. Every attribute value needs double or single quotes: id="1001".

2. Closing tag with different case

error on line 2 at column 32: Opening and ending tag mismatch: customer line 2 and Customer

XML names are case-sensitive, so <customer> cannot be closed by </Customer>. The same message appears when a closing tag is missing altogether: the parser reaches the parent's closing tag while the child is still open.

3. An unescaped ampersand

error on line 3 at column 15: xmlParseEntityRef: no name

A bare & starts an entity reference such as &amp;, so the parser expects a name after it. Write &amp; for an ampersand and &lt; for a less-than sign in text. For a large block of text full of these characters, wrap it in <![CDATA[ ... ]]> instead.

4. More than one root element

error on line 8 at column 1: Extra content at the end of the document

An XML document has exactly one root. The first <order> closed the document, so the second one is "extra content". Wrap both orders in a parent element such as <orders>.

The fixed version

With all four problems corrected and the result beautified with two-space indentation:

<orders>
  <order id="1001">
    <customer>Ali Raza</customer>
    <note>Pens &amp; paper</note>
    <items>
      <item sku="PEN-01" qty="2">Blue pen</item>
    </items>
  </order>
  <order id="1002"/>
</orders>

The parser accepts this, and running Beautify again returns exactly the same text, which is a quick way to confirm a file is both valid and consistently indented.

Beautify or minify?

Beautified XML is for people: reviewing a change, reading a SOAP response, or keeping a config file under version control, where one element per line makes diffs readable. Minified XML is for machines: it removes the whitespace between tags, and the same document becomes one line:

<orders><order id="1001"><customer>Ali Raza</customer><note>Pens &amp; paper</note><items><item sku="PEN-01" qty="2">Blue pen</item></items></order><order id="1002"/></orders>

Text inside elements is not touched by either mode. If you need to compare two versions of a file, format both the same way first and then use the XML diff checker, so indentation differences don't hide the real changes.

Check your own XML

Paste your file into the ToolStash XML formatter. It parses the XML in your browser, shows the error with a line and column if there is one, and never uploads the document or loads external DTDs or entities.

Sources