Multiple Root Elements in XML
A well-formed XML document must have exactly one root element that contains all other elements. Having two or more top-level elements (after the XML declaration) violates this fundamental rule. The parser reads the first root element successfully, then encounters a second top-level element and reports an error.
Why It Matters
The XML parser stops after the first root element closes and rejects everything that follows. This is a structural problem that requires wrapping all content in a single parent element. No partial parsing is possible.
Common Causes
- Outputting a list of records as repeated top-level elements without a surrounding container element.
- Concatenating two complete XML documents or fragments into one file, leaving two root elements side by side.
- Appending new entries to the end of a file, after the original root element has already closed.
Code Examples
<?xml version="1.0" encoding="UTF-8"?> <user> <name>Alice</name> </user> <user> <name>Bob</name> </user>
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<name>Alice</name>
</user>
<user>
<name>Bob</name>
</user>
</users>How to Fix
- 1Wrap all top-level elements inside a single root element: <users>...</users>.
- 2Choose a meaningful name for the root element that describes the collection.
- 3If merging multiple XML fragments, always create a wrapper element first.
- 4Remember that the XML declaration (<?xml ...?>) is not an element and does not count as a root.
Frequently Asked Questions
Why can an XML document have only one root?
Do comments or the declaration count as extra roots?
How do I combine several records into valid XML?
Check Your XML Now
Our XML validator detects this error automatically and shows the exact line and column.
Open XML ValidatorRelated XML Errors
Unclosed Tag in XML
Fix the XML unclosed tag error fast. Learn why every opening tag needs a matching closing tag or a self-closing form, with clear examples you can copy.
Mismatched Tags in XML
XML tag names are case-sensitive. Learn how to fix mismatched opening and closing tags, spot nesting mistakes, and make your XML well-formed once more.
Invalid or Missing XML Declaration
Learn how to write a correct XML declaration and fix common mistakes like wrong attribute order, missing encoding, or stray whitespace before the prolog.