How to check if XML is valid and fix the first error

How to check if XML is valid and fix the first error

One unescaped ampersand or a tag closed in the wrong order and a feed, sitemap, or config file stops loading. Find the line and column of the first error, fix it, then format the file so the next mistake is easy to spot.

One error stops the whole file

XML parsers do not guess. A browser shows a broken HTML page anyway, but an XML parser stops at the first problem and rejects the file. A sitemap with one bare & in a URL, an RSS feed with an unclosed tag, or a config file with an unquoted attribute fails completely.

The error from the program that rejected the file is often vague, and finding one character in a few thousand lines by eye is slow.

Validate first, then format

The XML Validator checks the XML 1.0 well-formedness rules as you type: one root element, matched and correctly nested tags, quoted attributes with no duplicates, escaped < and &, and names that start with a letter, an underscore, or a colon. When something is wrong it shows the line and column, the line itself with a ^ under the problem, and how to fix it. It reports only the first error, because one mistake often causes others further down.

Once the file passes, the XML Formatter indents it so each element sits on its own line, or minifies it to one line. It keeps comments, CDATA, and the XML declaration exactly as they are, and it refuses input that is not well-formed. Both run in your browser, on files up to 5 MB.

Check if XML is valid

  1. Paste the XML into the XML Validator, or upload the .xml file.
  2. If it is valid, read the document statistics and move on.
  3. If not, go to the line and column it reports and read the suggested fix.
  4. Fix the usual causes: write a bare & as &amp;, close tags in the reverse order you opened them, and quote every attribute value.
  5. Validate again until no error is left.
  6. Run the clean file through the XML Formatter to review it with proper indentation.

What a well-formedness check does not cover

  • It does not validate against a DTD, XSD, or RELAX NG schema, so a document with the wrong elements can still pass.
  • Entities declared in an external DTD file can't be resolved and are reported as warnings.
  • HTML with unclosed tags such as <br> is not XML and is rejected.

Frequently asked questions

How do I check if XML is well-formed?

Paste it into the XML Validator. It checks the XML 1.0 rules and points to the first error by line and column.

Why does an ampersand break my XML?

In XML text, & starts an entity. A bare one must be written as &amp;.

Is valid XML the same as well-formed XML?

Not quite. Well-formed means the syntax is right. Valid also means the document matches a schema such as a DTD or XSD.

Can I check a sitemap.xml this way?

Yes, for well-formedness. Open the sitemap, copy its contents, and paste them into the XML Validator.

Tools used in this guide

  • XML ValidatorDeveloper Tools

    Checks XML for well-formedness and explains each syntax error.

  • XML FormatterDeveloper Tools

    Beautifies XML with clean indentation or minifies it to one line.

More from the blog

All guides