The XML Tree Structure
Every XML document forms a hierarchical tree, starting from a single root and branching into parent-child relationships — this structure is what parsers and query languages navigate.
Parents, Children, and Siblings
Just like a family tree or a folder structure on a computer, XML elements relate to each other as parents, children, and siblings, and understanding this vocabulary is essential for navigating XML with tools like XPath (covered later in this course).
<library>
<book>
<title>Learning XML</title>
<author>J. Naidoo</author>
</book>
<book>
<title>Data Formats Explained</title>
<author>S. Khumalo</author>
</book>
</library>
Reading the Tree
In the example above, <library> is the root, each <book> is a child of the root and a sibling of the other <book>, and <title>/<author> are children of their respective <book>.
Common Mistakes
- Confusing sibling elements (same parent, same level) with child elements (nested one level deeper).
- Losing track of which closing tag belongs to which element in a deeply nested tree without consistent indentation.
- Assuming element order within the same parent never matters — some schemas and applications do rely on a specific sequence.
- Not indenting nested elements consistently, making the tree's structure hard to read visually.
Professional Tip
Consistent indentation is not required by the XML specification, but it's the single easiest thing you can do to keep a hand-written XML tree readable and to spot structural mistakes quickly.
Your Turn
Draw (on paper or in a text file) the tree structure of an XML document representing a family with a parent element and multiple child elements, each with their own name and age children.
Mini Quiz
In an XML tree, what describes two elements that share the same parent?