Common causes
1. Several records without a common parent
Exports and logs that write one element per record produce a list of roots. Wrap them in a container element.
<user id="1"/>
<user id="2"/><users>
<user id="1"/>
<user id="2"/>
</users>2. Documents concatenated with their declarations
Appending one XML file to another repeats the <?xml ?> declaration and the root. Merge the content under one root and keep a single declaration at the top. Python reports junk at the second declaration; libxml2 says “XML declaration allowed only at the start of the document”.
<?xml version="1.0"?>
<feed><entry>A</entry></feed>
<?xml version="1.0"?>
<feed><entry>B</entry></feed><?xml version="1.0"?>
<feed>
<entry>A</entry>
<entry>B</entry>
</feed>3. Text or a stray closing tag after the root
A debug line, a status word or an extra </root> printed after the document counts as junk. Make sure the writer emits nothing after the final end tag.
<status code="200"/>
OK<status code="200">OK</status>4. An HTML fragment parsed as XML
A snippet with sibling paragraphs is fine in an HTML page but has several roots as XML. This often happens with content taken from a CMS field or an RSS description. Wrap it in one element, or use an HTML parser.
<p>First paragraph.</p>
<p>Second paragraph.</p><div>
<p>First paragraph.</p>
<p>Second paragraph.</p>
</div>Three kinds of junk, reported three ways
The XML grammar is short here: document ::= prolog element Misc*, where Misc is a comment, a processing instruction or whitespace. What the parser found after the root decides the message:
- Python ElementTree (expat):
junk after document elementfor a second element or for text, butnot well-formed (invalid token)for an extra closing tag. - lxml and xmllint (libxml2):
Extra content at the end of the documentfor all three. - Java (JDK):
The markup in the document following the root element must be well-formed.for a second element or a closing tag, andContent is not allowed in trailing section.for text. - .NET:
There are multiple root elements.for an element,Data at the root level is invalid.for text, andUnexpected end tag.for a closing tag.
The reported position is where the junk starts, so the root ended just before it. In a file with thousands of records the first error usually sits at the second record, near the top. PasteKit names both elements and the line where the root closed.
Reading multi-root input without losing data
If you cannot change the producer, you have three reasonable options, and one that looks reasonable but loses data.
Wrap the input in a synthetic root, after removing any declarations, since those are only legal at the very start:
import re
import xml.etree.ElementTree as ET
body = re.sub(r"<\?xml[^>]*\?>", "", text)
root = ET.fromstring(f"<batch>{body}</batch>")
Or split on the declarations and parse each document separately, which keeps errors isolated to one document:
documents = [ET.fromstring(part) for part in re.split(r"(?=<\?xml)", text) if part.strip()]
In .NET, read it as a fragment with XmlReaderSettings and ConformanceLevel = ConformanceLevel.Fragment, which accepts several top-level elements.
The trap is lxml’s etree.XMLParser(recover=True). It does not raise an error here; it keeps the first root and silently discards every record after it. Only use it when losing data is acceptable.
Writing one root every time
For exports, write the opening root tag first, then the records, then the closing root tag in a finally block so an error part-way through does not leave the file unusable. A streaming writer such as XMLStreamWriter or XmlWriter makes this natural.
Avoid appending to an existing XML file. If each run adds a complete document, the second run creates this error. Use one file per document, or write JSON Lines if the data is really a log.
For an API response, look for anything that can write after the document: a debug toolbar, a framework injecting a script, or a warning printed by the application. Compare the raw body with what the application thinks it sent.
Frequently asked questions
Why does Python say "column 0"?
Expat counts columns from 0, so column 0 is the first character of the line. Other tools, including PasteKit, count from 1.
Are comments allowed after the root element?
Yes. Comments, processing instructions and whitespace may appear after the root. Elements and non-whitespace text may not.
How do I process a file that contains many XML documents?
Split it at the declarations or root elements and parse each piece separately, or wrap the whole stream in a synthetic root element before parsing.
Can an XML file have two XML declarations?
No. The declaration must be the first thing in the document, and it can only appear once. A second one usually means two files were joined together.
Why does Firefox show this error for my RSS feed?
Firefox displays parse errors for XML it renders directly, and it uses the same expat wording as Python. Look at the raw feed for a second root or text after the closing rss tag.