Multiple Root Elements in XML – Fix Extra Top-Level Elements

Local only

A well-formed XML document has exactly one document element, so two sibling top-level elements cannot both be roots.

33 chars · 1 line

Waiting for input

Loading XML parser…

XML document-root troubleshooting

How to fix multiple root elements in XML

The sample contains name and port as separate top-level elements. XML documents need one document element that contains the rest of the element tree, so independent fragments must be wrapped or handled as separate documents. This tool is free to use. No account or payment is required.

Last updated

What you can do here

  • XML well-formedness validation
  • Broken XML example preloaded
  • Corrected XML comparison
  • .xml file import
  • Browser parser feedback
  • Browser-local processing

Steps

  1. Locate the first complete top-level element and any markup that follows it.
  2. Decide whether the following elements belong inside the same payload or represent separate XML documents.
  3. Wrap related siblings in one root element or split separate documents, then validate again.

XML fragments are not the same as XML documents

Two well-formed element fragments can still be invalid when concatenated as one XML document. A document parser expects one document element around all ordinary element content.

Do not invent a wrapper if the protocol defines separate messages

A wrapper changes the document shape. For APIs, feeds, or message streams, confirm the expected envelope before adding a new root element solely to satisfy syntax validation.

Multiple XML roots example

Wrap related fields in one service root element.

Invalid XML

<name>api</name><port>8080</port>

Corrected XML

<service><name>api</name><port>8080</port></service>

Common questions

Frequently asked questions

How many root elements can an XML document have?

A well-formed XML document has exactly one document element.

Can comments appear outside the XML root element?

Comments and some processing instructions can appear outside the document element, but a second ordinary top-level element cannot become another root.

Should I always wrap multiple XML fragments in a new root?

Only when those fragments are intended to form one document. Separate protocol messages should usually remain separate.

Debugging a specific XML issue? Browse XML troubleshooting.