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.
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.
Check Your XML Now
Our XML validator detects this error automatically and shows the exact line and column.
Open XML ValidatorHostinger — Fast & Affordable Web Hosting
Serve well-formed XML feeds and APIs on reliable hosting.
Related XML Errors
Unclosed Tag in XML
Learn why XML throws an unclosed tag error and how to fix it. Every opening tag must have a matching closing tag or be self-closing.
Mismatched Tags in XML
XML tag names are case-sensitive. Learn how to fix mismatched opening and closing tags in XML documents.
Invalid or Missing XML Declaration
Learn how to write a correct XML declaration and fix common mistakes like wrong attribute order or missing encoding.