6. XML Trees

What you will learn

  • How an XML document forms a tree
  • The family words: parent, child, sibling, ancestor, descendant
  • The kinds of node in the tree
  • Why the tree matters for the tools that read XML

A document is a tree

Because XML has one root and properly nested elements, every document forms a tree: a single starting point that branches downward. Programs do not read XML as flat text. They build this tree in memory, and then walk through it.

XML code: school.xml

<?xml version="1.0" encoding="UTF-8"?>
<school name="Greenfield">
  <class level="1">
    <teacher>Ms Khan</teacher>
    <student>Sam</student>
    <student>Ali</student>
  </class>
  <class level="2">
    <teacher>Mr Reid</teacher>
  </class>
</school>

The tree

(document)
 └── school   [name="Greenfield"]
      ├── class   [level="1"]
      │    ├── teacher  = Ms Khan
      │    ├── student  = Sam
      │    └── student  = Ali
      └── class   [level="2"]
           └── teacher  = Mr Reid

Family words

Word Meaning In the tree above
Root The one top element school
Parent The element directly above The parent of teacher is class
Child An element directly inside teacher and both student elements are children of the first class
Sibling Elements with the same parent The two student elements
Ancestor Any element above, at any distance class and school are ancestors of student
Descendant Any element below, at any distance All of teacher, student, and class are descendants of school
Leaf An element with no child elements Each teacher and student

The kinds of node

Everything in the tree is a node. There are several kinds:

Node type Example
Document The invisible top of everything, above the root element
Element <student>
Attribute level="1"
Text Sam
Comment <!-- note -->
Processing instruction <?xml-stylesheet ... ?>

The text Sam is not part of the student tag. It is a separate text node that sits inside the student element. Attributes are attached to their element, but are not counted as its children.

Document order

Nodes have an order: the order in which they appear in the file, reading top to bottom. The first student comes before the second. This order is kept, and tools rely on it, for example when asking for “the second student”.

Depth

The depth is how many steps an element is below the root. The root is at depth 0, class at depth 1, and student at depth 2. Very deep nesting is harder to read, so keep your structure as flat as the data allows.

A tree describes a whole document

Even a mixed-content document makes a tree, with text nodes between the element nodes.

XML code

<p>XML is <b>strict</b> but <i>dependable</i>.</p>

The tree

p
 ├── "XML is "
 ├── b
 │    └── "strict"
 ├── " but "
 ├── i
 │    └── "dependable"
 └── "."

Why the tree matters

Nearly every tool for XML works on this tree:

  • The DOM gives programs, including JavaScript and PHP, objects for each node to read and change.
  • XPath (a later lesson) is a language for choosing nodes by their place in the tree.
  • XSLT (a later lesson) walks the tree to build new output.
  • Schemas describe which tree shapes are allowed.

If you picture your XML as a tree, those tools become much easier to understand. It is also the same tree idea you met in the JavaScript DOM lessons.

Designing a good tree

  • Choose one root that names the whole document, such as library or orders.
  • Group repeating items under a plural parent (students holding many student elements).
  • Keep related data together, and avoid very long flat lists of unrelated elements.
  • Do not nest more deeply than you need to.

Common mistakes

  • Thinking text is part of the tag instead of a text node.
  • Mixing up parent and ancestor, or child and descendant.
  • Forgetting that whitespace between elements may also appear as text nodes in some tools.
  • Creating several top-level elements, so there is no single root.
  • Nesting far deeper than the data needs.

Exercise

For the school example, answer: (1) what is the parent of the second class? (2) how many students are descendants of school? (3) name two siblings of the first teacher.

Show solution
  1. The parent of the second class is school.
  2. Two students (Sam and Ali) are descendants of school.
  3. The two student elements (Sam and Ali) are siblings of the first teacher, because they share the same parent.

Knowledge check

1. Why does every XML document form a tree?

It has a single root, and elements are properly nested inside each other.

2. What do you call two elements with the same parent?

Siblings.

3. Is the text inside an element a node?

Yes, a text node.

Summary

  • An XML document is a tree with one root, parents, children, and siblings.
  • Elements, attributes, text, and comments are all nodes, kept in document order.
  • The DOM, XPath, and XSLT all work on this tree.
Scroll to Top