XML Tree Structure
Estimated study time: 11 minutes.
Every XML document is structured as a tree — a single root element branching out into child elements, which can themselves have further children. Understanding this tree structure is essential to reading, writing, and navigating XML correctly.
Visualizing the Tree
<course>
<title>Data Analytics</title>
<modules>
<module>Excel Basics</module>
<module>SQL for Analysis</module>
</modules>
</course>
Here, course is the root. It has two children: title and modules. The modules element itself has two children: module and module.
Why the Tree Model Matters
- It mirrors how most parsers (like the DOM) represent the document in memory.
- It makes navigation predictable — you always move up to a parent, down to children, or sideways to siblings.
- It's the foundation for query languages like XPath, which describe paths through the tree.
Tree Terminology
- Root — the single top-level element containing everything else.
- Branch — any element that contains other elements.
- Leaf — an element with no children, usually holding text content.
Relationships Inside the Tree
Every element except the root has exactly one parent. Elements that share a parent are siblings, and everything below an element counts as its descendants. Using a slightly larger document makes these relationships easier to see.
<library>
<book id="b1" lang="en">
<title>Learning SQL</title>
<author>R. Mehta</author>
<price>450</price>
</book>
<book id="b2" lang="hi">
<title>Python Basics</title>
<author>S. Rao</author>
<price>380</price>
</book>
</library>
libraryis the root and the ancestor of every other element.- The two
bookelements are siblings. title,authorandpriceare children of a book and descendants of the library.idandlangare attributes. They belong to their element but are not children in the element tree.
Nodes That Are Not Elements
Parsers that follow the DOM model see more than elements. The text "Learning SQL" is a separate text node inside title. Comments and processing instructions are nodes too. Even the line breaks and spaces between tags can appear as whitespace-only text nodes, which surprises many beginners when they count children and get a bigger number than expected.
For the first book above, the DOM view looks like this when whitespace is ignored:
Element: book (attributes: id="b1", lang="en")
Element: title
Text: "Learning SQL"
Element: author
Text: "R. Mehta"
Element: price
Text: "450"
Notice that the value 450 is text. Code that reads it must convert it to a number before doing any maths, as the next example does with float().
Walking the Tree in Code
Python's built-in ElementTree module loads a document into a tree you can loop through or search.
import xml.etree.ElementTree as ET
root = ET.parse("library.xml").getroot()
print(root.tag) # library
for book in root.findall("book"): # direct children named book
title = book.find("title").text
price = float(book.find("price").text)
print(book.get("id"), title, price)
# search anywhere below the root
authors = [a.text for a in root.iter("author")]
print(authors)
The same paths can be written in XPath, which tools such as XSLT, Selenium and many databases understand:
/library/book/title all titles
/library/book[@lang='hi']/title titles of Hindi books
//book[price > 400]/author authors of books costing more than 400
Structural Errors the Tree Model Exposes
- Two roots: two top-level elements with no wrapper make the document not well-formed.
- Overlapping tags:
<b><i>text</b></i>cannot form a tree, so parsers reject it. - Data hidden in text: writing "450 INR" inside one element makes later queries harder than separate
amountandcurrencyparts. - Very deep nesting: many levels make paths long and fragile; flatten where the hierarchy adds no meaning.
- Mixed naming styles: switching between
bookTitle,book_titleandBookTitlemakes queries error-prone, and XML names are case-sensitive.
Habit worth building: before writing code against an XML file, open it, identify the root, note which elements repeat, and decide which values are attributes and which are child elements.