XML Root Element

Estimated study time: 7 minutes.

The root element is the single top-level element that wraps every other element in an XML document. Every well-formed XML document must have exactly one.

Example

<?xml version="1.0" encoding="UTF-8"?>
<course>
  <title>Data Analytics</title>
  <duration>4 Months</duration>
</course>

Here, course is the root element. Everything else in the document — title, duration — lives inside it.

Rules for the Root Element

  • There can be only one root element per document.
  • It must fully enclose every other element — nothing can exist outside it.
  • It comes immediately after the XML declaration and prolog.

What Happens Without One?

<!-- Invalid: two top-level elements, no single root -->
<title>Data Analytics</title>
<duration>4 Months</duration>

A parser will reject this document as not well-formed, because there's no single element containing both pieces of data.

💡 Tip: If you're combining data from multiple sources into one XML file, wrap everything in a single meaningful root element like <catalog> or <data> rather than leaving multiple top-level elements.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →