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.
<loc>https://example.com/</loc>
</url>
</urlset><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/</loc>
</url>
</urlset>Where this bites most
Other XML errors
- Opening and ending tag mismatch
- Premature end of data in tag
- Unescaped ampersand (EntityRef: expecting ';')
- Entity is not defined
- Start tag expected
- XML declaration allowed only at the start of the document
- Unquoted attribute value (AttValue expected)
- Attribute redefined
- Attributes construct error (unterminated quote)
- Invalid element name
- Document is empty
- Unsupported encoding
- Sequence ']]>' not allowed in content
- Comment not terminated
- Detected an entity reference loop
- PCDATA invalid Char value (control character)
- Unescaped '<' in an attribute value
- Invalid numeric character reference
- Blank needed here
- Malformed declaration expecting version
- Unsupported XML version
- Processing instruction not terminated
- CData section not finished
- Error parsing attribute name
- Double hyphen within comment
- CharRef: invalid decimal value
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.