Skip to main content

XML error · structure

Extra content at the end of the document

Something follows the root element — a second root, a stray closing tag, or a fragment that was never a whole document.

Extra content at the end of the document (line 2)

What it means

An XML document has exactly one root element, and nothing but comments, whitespace and processing instructions may follow it. This message means the parser finished the root and then found more content.

Two very different mistakes land here. One is a genuine second root: <a/><b/> is not a document, it is two. The other is a fragment — the opening tags live in another file or another chunk of a stream — which is what you get from splitting a large XML file on line or byte boundaries.

This is one of the cases permissive parsers wave through. fast-xml-parser accepts <a/><b/> as valid, so a document can pass some tools and still be unreadable by any standard parser.

What usually causes it

  • Two root elements, often from concatenating two documents.
  • The input is a fragment rather than a whole document.
  • A file split by size or line count instead of at element boundaries.
  • A find-and-replace that removed an opening tag but left its close behind.
  • Trailing junk appended by a logger or a template after the root closed.

How to fix it

  • Wrap multiple top-level elements in a single root, or process them as separate documents.
  • Wrap a fragment in one synthetic root element before parsing.
  • Split large XML on element boundaries, never on lines or bytes.
  • Merge documents by combining their trees, not by concatenating their text.

The same error elsewhere

Different parsers, same defect. If you arrived with one of these messages, you are in the right place.

  • Xerces (Java)

    Content is not allowed in prolog.
  • expat (Python)

    junk after document element: line 1, column 4
  • .NET System.Xml

    Data at the root level is invalid. Line 1, position 5.
  • Browsers / MSXML

    Only one top level element is allowed in an XML document.
  • fast-xml-parser and similar

    (accepted — no error reported at all)

Before and after

Both snippets are re-checked by the test suite against the real parser: the first is confirmed to produce the exact error above, the second to parse cleanly.

Fails
    <loc>https://example.com/</loc>
  </url>
</urlset>
Parses
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
  </url>
</urlset>

Where this bites most

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.