</>
ValidateHTML

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

Invalid XML
<?xml version="1.0" encoding="UTF-8"?>
<user>
  <name>Alice</name>
</user>
<user>
  <name>Bob</name>
</user>
Valid XML
<?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 Validator
Recommended

Hostinger Fast & Affordable Web Hosting

Serve well-formed XML feeds and APIs on reliable hosting.

Get 80% Off Hosting →

Related XML Errors

View all XML errors