Cet article est actuellement disponible uniquement en anglais.

Extra content at the end of the document: XML feed fix

FeedAwake 4 min de lecture

An XML document has exactly one root element: in a Google Shopping feed that's <rss>, in an Atom feed <feed>. When the root closes, the document is over. Anything other than whitespace after it makes the file invalid, and the feed stops being readable for Google Merchant Center, Meta and other channels.

So this error is never about a product. It's about what comes after the last closing tag.

What the error looks like

Chrome, Edge:  error on line 7 at column 1: Extra content at the end of the document
PHP, lxml:     Extra content at the end of the document
Firefox:       XML Parsing Error: junk after document element
Python:        junk after document element: line 7, column 0

Chrome, PHP and lxml use libxml2; Firefox and Python's built-in xml.etree use expat, which calls it "junk after document element". Both point at the first character after the root closed: that's where to look.

The four usual causes

1. Something printed after the feed

The most common case when the feed is generated on request by a script (feed.php, a plugin endpoint, a theme template). The XML is complete, and then the script or the system around it adds output:

</channel>
</rss>
<b>Warning</b>: Undefined variable $x in feed.php on line 88

PHP notices and warnings, a debug echo, a theme footer, a plugin that appends an HTML comment or a tracking script to every response. Open the feed URL and look at the very end of the response, or run:

curl -s "https://shop.example/feed.xml" | tail -c 500

Fix: stop the extra output at its source: turn off display_errors on production (log errors instead), remove the debug output, end the script right after the XML is written, and exclude the feed URL from plugins that inject code into pages.

2. Products without a wrapping root

A hand-made or custom export writes the products one after another with no <rss> and <channel> around them:

<?xml version="1.0" encoding="UTF-8"?>
<item><g:id>A1</g:id></item>
<item><g:id>B1</g:id></item>

The first <item> becomes the root, and the error points at the second one, usually on line 3. Google feeds have to be RSS 2.0 or Atom anyway: wrap the items in <rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"><channel>…</channel></rss>.

3. Two feeds in one file

The export appended to the existing file instead of replacing it, or several batches were joined with cat. The file contains a complete feed, then a second <?xml …?> declaration and a second feed. libxml then reports the declaration rather than "extra content":

error on line 7 at column 6: XML declaration allowed only at the start of the document

Python and Firefox still say "junk after document element". Check the file size: if it doubles from one export to the next, this is it. Fix: write each export into a new temporary file and rename it over the old one when it's complete, so there's never anything left to append to.

4. A shorter file written over a longer one

If a script overwrites the feed in place without truncating it first, a new export that's shorter than yesterday's leaves the tail of yesterday's file behind the new </rss>: half a product, then the old closing tags. It appears after the catalogue shrinks, which makes it look random. The temporary file plus rename from the previous point fixes this too.

"It opens fine for me"

Two reasons the feed can look fine in your browser while Google gets the error:

  • Different responses. Some setups only print warnings or inject scripts for certain visitors, or serve a cached copy to you and a fresh one to crawlers. Check with curl as above rather than a logged-in browser tab.
  • It comes and goes. A warning that only fires for some products, or an export that sometimes overlaps a cron run, breaks the feed some nights and not others.

The free feed checker downloads the feed like a channel does and shows the first errors with line and column. For the general way to find a broken line in a large feed, see this guide.

Catch it when it happens

Because the cause is usually outside the product data, this error tends to appear after a server change: a PHP upgrade that turns old code into new warnings, a new plugin, a theme update. FeedAwake checks the feed on a schedule and alerts you by email, Telegram, Slack or Discord when it stops parsing, so you find out that night and not when products drop out of Shopping. To check it once, use the free feed checker.

Vérifiez gratuitement votre flux produits

Repérez les erreurs de flux avant de perdre votre budget publicitaire. Sans compte.

Commencer votre essai gratuit de 14 jours Audit Merchant Center gratuit

Autres articles