How-to guide

How to Fix XML Parsing Errors

XML parsers are strict by design, so most errors come from four causes: something before the XML declaration, usually a byte-order mark or a blank line; a bare ampersand that was never escaped; tags that do not close in the order they opened; and an HTML entity such as nbsp that XML does not define. Each has a precise fix.

XML Validator

Check that an XML document is well-formed.

Open the tool

XML has no error recovery. An HTML parser will make a reasonable guess about a missing closing tag; an XML parser stops. That strictness is deliberate — it is what makes XML safe for document interchange — but it means a single stray byte invalidates the whole file.

The error messages are unusually literal once you know the vocabulary. "Prolog" means everything before the root element, so an error there is about the very start of the file, not about your data.

Step by step

  1. Check what comes before the declaration

    Content is not allowed in prolog means there are bytes before <?xml. Usually this is an invisible UTF-8 byte-order mark, a blank line, or a PHP warning printed above the output. Nothing at all may precede the declaration.

  2. Escape bare ampersands

    A raw & is the start of an entity reference, so a URL such as ?a=1&b=2 inside an element breaks parsing. Write &amp; instead. This is the single most common cause in feeds and sitemaps.

  3. Match every tag, in order

    XML requires strict nesting: <a><b></b></a>, never <a><b></a></b>. Empty elements must self-close as <br/> rather than <br>. A formatter that re-indents the document makes an unclosed tag obvious immediately.

  4. Replace HTML entities XML does not know

    XML defines only five entities: &amp;, &lt;, &gt;, &quot; and &apos;. Anything else — &nbsp;, &copy;, &mdash; — is undeclared and rejected. Use the numeric form such as &#160;, or the literal character in a UTF-8 document.

  5. Validate the whole file, not a fragment

    Paste the complete document into a validator. A fragment will report misleading errors because a well-formed XML document must have exactly one root element, and a snippet usually does not.

Example

Two errors in three lines: a bare ampersand and an undeclared HTML entity.

Rejected

<item>
  <link>https://example.com/?a=1&b=2</link>
  <title>Tea &nbsp; Coffee</title>
</item>

Well-formed

<item>
  <link>https://example.com/?a=1&amp;b=2</link>
  <title>Tea &#160; Coffee</title>
</item>

Frequently asked questions

What does "Content is not allowed in prolog" mean?
Something appears before the opening <?xml declaration. The usual culprit is a UTF-8 byte-order mark, which is invisible in most editors, followed by stray whitespace or output accidentally printed by the script that generated the file.
Why is a plain ampersand invalid in XML?
Because & always begins an entity reference, so the parser expects a name and a semicolon after it. A URL containing &b=2 looks like a malformed entity. Writing &amp; escapes it, which is why query strings in feeds are such a frequent source of errors.
Can I use &nbsp; in XML?
Not without declaring it. XML predefines only five entities, and nbsp is not among them — it comes from HTML. Use the numeric character reference &#160; instead, which needs no declaration.
Is XML case-sensitive?
Yes, entirely. <Item> and <item> are different elements, and a document that opens one and closes the other is not well-formed. This catches people who have moved across from HTML, where tag names are case-insensitive.
What is the difference between well-formed and valid?
Well-formed means the syntax is correct — tags match, entities are escaped. Valid means it also conforms to a schema or DTD that defines which elements may appear where. A document can be well-formed but not valid; it cannot be valid without being well-formed.

Tools used in this guide

All tools →
All guides →

Last reviewed .