How to Validate XML and Fix Well-Formedness Errors
By Durim Hoti · Last updated
XML parsers are strict by design: at the first syntax error they stop and refuse the whole document. A sitemap, an RSS feed or a config file with one wrong character can therefore be ignored completely by the program that reads it.
Validate XML step by step
- Open the XML validator.
- Paste the XML or click Open file. The result appears right away.
- If it says Invalid XML, read the message, the line and the column. The line is shown with a
^under the problem. - Click Go to error, fix the spot and check the result again; the validator shows one error at a time.
- When it's well-formed, make it readable with the XML formatter if needed.
Example: a sitemap with an ampersand in a URL
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/</loc>
</url>
<url>
<loc>https://example.com/search?q=shoes&page=2</loc>
</url>
</urlset>
The validator reports: line 7, column 44, “&” must be written as & (or start an entity like <).

The fix is to escape the ampersand. The URL itself doesn't change: programs that read the sitemap turn & back into &.
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/</loc>
</url>
<url>
<loc>https://example.com/search?q=shoes&page=2</loc>
</url>
</urlset>
Common XML errors and the messages
We ran each snippet through the validator; this is the first error it reported.
Mismatched tag
<note>
<to>Tove</To>
</note>Line 2, column 11: End tag </To> doesn't match the open element <to> (line 2). Close <to> first.
Unclosed tag
<note>
<to>Tove
</note>Line 3, column 1: End tag </note> doesn't match the open element <to> (line 2). Close <to> first.
Two root elements
<a>1</a>
<b>2</b>Line 2, column 1: Only one root element is allowed. Wrap the elements in a single parent element.
Attribute without quotes
<book id=bk101>
</book>Line 1, column 10: The value of “id” must be in quotes.
HTML entity
<p>Price: 5</p>Line 1, column 10: Undefined entity . XML only defines < > & " ' — use a character reference like   instead.
A < sign in text
<expr>a < b</expr>Line 1, column 10: Invalid element name: names can't start with “ ”.
Something before the declaration
<?xml version="1.0"?>
<a/>Line 2, column 1: The XML declaration <?xml … ?> must be at the very start of the document, before any whitespace.
The message for a < b needs a word of explanation: the parser reads < as the start of a tag, then finds a space where the tag name should be. Write <, or wrap text with many special characters in a CDATA section, which the validator accepts:
<expr><![CDATA[a < b && c]]></expr>Common problems
The error points to the end of the file
A tag opened somewhere earlier was never closed, so the parser only notices at a later closing tag. The message names the open element and its line; start looking there.
The XML is valid here but rejected elsewhere
The other program probably checks a schema: an element name, order or value it doesn't expect. Check its documentation, for example the sitemap protocol for sitemaps.
Strange characters in the file
Save the file as UTF-8 and keep the encoding in the XML declaration in line with how the file is saved.
What to do next
Working with JSON as well? See how to fix JSON errors, and for config files JSON vs YAML. To turn XML into JSON, use the XML to JSON converter.
FAQ
- What is the difference between well-formed and valid XML?
- Well-formed XML follows the syntax rules every XML parser enforces: one root element, closed and correctly nested tags, quoted attributes, escaped special characters. Valid XML is also checked against a schema (XSD, DTD or RELAX NG) that defines which elements are allowed. Our validator checks well-formedness, not schemas.
- Why does an & in a URL break my XML?
- In XML, & starts an entity such as <. A bare & followed by other text is an error, so a link like ?q=shoes&page=2 must be written as ?q=shoes&page=2. Programs reading the XML turn & back into &.
- Can I use or © in XML?
- Not unless a DTD defines them. XML only knows <, >, &, " and '. Use numeric references instead, for example   for a non-breaking space.
- Does the validator check my XML against an XSD schema?
- No. It checks well-formedness, which every parser requires first. For schema validation, use a schema-aware tool or your code editor's XML support.
- Is my XML uploaded?
- No. The XML is parsed in your browser.
Try it and keep reading
- Open the XML validatorCheck XML is well-formed, with exact errors.
- How to Fix JSON Errors Like "Unexpected token"Understand JSON error messages, find the exact spot, and fix the nine most common mistakes.
- JSON vs YAML: Differences and Conversion PitfallsCompare JSON and YAML and avoid the values that silently change during conversion.