chapter 4: XML Tree
What is the “XML Tree”?
When we talk about the XML tree, we mean:
XML data is organized like a family tree or an organization chart — not like a flat table or a list.
Every piece of XML is part of a hierarchical structure — exactly like a tree in nature:
- There is one root (the starting point)
- The root has children
- Those children can have their own children
- …and so on
This tree-like structure is the most fundamental idea behind XML.
Analogy most people understand quickly:
Imagine a folder on your computer:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
My Documents ├── College │ ├── 1st Year │ │ ├── Maths.pdf │ │ └── Physics.pdf │ └── 2nd Year │ └── Notes │ └── Chemistry.docx └── Personal ├── Photos └── Resume.pdf |
This is exactly how XML is structured — folders = elements, files = text content or leaf nodes.
Visualizing the XML Tree – Very First Simple Example
XML code:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="UTF-8"?> <student> <name>Rahul Verma</name> <roll>42</roll> <class>XII</class> <marks> <math>92</math> <physics>87</physics> <chemistry>94</chemistry> </marks> </student> |
Now let’s draw the tree view of exactly the same document:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
student ←─────────────────────────────── root element │ ├── name → "Rahul Verma" ← text content (leaf) ├── roll → "42" ← text content (leaf) ├── class → "XII" ← text content (leaf) │ └── marks ← parent element │ ├── math → "92" ← leaf ├── physics → "87" ← leaf └── chemistry → "94" ← leaf |
Important names we use when talking about the tree:
| Term | Meaning in the tree | Example from above |
|---|---|---|
| Root | The very top element (only one allowed) | <student> |
| Parent | Element that contains other elements | <student>, <marks> |
| Child | Element directly inside another element | <name>, <roll>, <marks> |
| Sibling | Elements that share the same parent | <math>, <physics>, <chemistry> |
| Leaf / Text node | Element that contains only text (no children) | <name>, <math>, <roll> |
| Branch / Internal node | Element that has children | <student>, <marks> |
A Slightly Bigger & More Realistic Example
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="UTF-8"?> <library> <book id="B101" language="English"> <title>Atomic Habits</title> <author> <firstName>James</firstName> <lastName>Clear</lastName> </author> <year>2018</year> <genres> <genre>Self-help</genre> <genre>Psychology</genre> </genres> <price currency="INR">499.00</price> <available>true</available> </book> <book id="B102" language="Hindi"> <title>Rich Dad Poor Dad</title> <author>Robert Kiyosaki</author> <year>1997</year> <price currency="INR">350.00</price> <available>false</available> </book> </library> |
Tree drawing (very simplified):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
library (root) │ └── book (1st book) │ ├── @id → "B101" ← attribute ├── @language → "English" ← attribute │ ├── title → "Atomic Habits" ├── author │ ├── firstName → "James" │ └── lastName → "Clear" ├── year → "2018" ├── genres │ ├── genre → "Self-help" │ └── genre → "Psychology" ├── price → "499.00" │ └── @currency → "INR" └── available → "true" └── book (2nd book) ├── @id → "B102" ├── @language → "Hindi" ├── title → "Rich Dad Poor Dad" ├── author → "Robert Kiyosaki" ← text directly (no child elements) ├── year → "1997" ├── price → "350.00" │ └── @currency → "INR" └── available → "false" |
Notice:
- Some elements have attributes (start with @ in tree views)
- Some elements have text content
- Some elements have child elements
- Some elements have both text and children (mixed content — more advanced)
Common Tree-related Terms You Should Know
| Term you will hear | What it really means in XML tree | Example |
|---|---|---|
| Root element | The single top-most element | <library> |
| Depth / Level | How many steps down from root | <genre> is at depth 4 |
| Ancestor | Any element above in the path | <library> is ancestor of <genre> |
| Descendant | Any element below in the path | <title> is descendant of <library> |
| Parent / Child | Direct connection (one level) | <book> is parent of <title> |
| Sibling | Same parent, same level | two <book> elements are siblings |
| Leaf node | Element with no children (usually has text) | <year>, <title> |
| Attribute node | Not an element — belongs to an element | id=”B101″, currency=”INR” |
| Text node | The actual characters between tags | “Atomic Habits”, “true” |
Very Helpful Way to Think: “Path to the Data”
In XML trees, we often talk about paths (very important for XPath later).
Examples from the library document:
| What we want | Tree path (like folder path) |
|---|---|
| Title of first book | /library/book[1]/title |
| Last name of author of first book | /library/book[1]/author/lastName |
| Currency of price of second book | /library/book[2]/price/@currency |
| All genre names | /library/book/genres/genre |
| All book titles | /library/book/title |
This path thinking is exactly how people navigate XML trees in code, queries, and transformations.
Common Beginner Mistakes with XML Trees
-
Thinking XML is just “tags and text” (no — it’s a tree)
-
Forgetting there is only one root
-
Creating crossed tags (breaks the tree)
Wrong:
XML0123456<b><i>text</b></i> <!-- crossed → not a proper tree --> -
Putting multiple top-level elements
Wrong:
XML01234567<book>...</book><book>...</book> <!-- two roots → invalid tree --> -
Not understanding that attributes belong to their element (they are not children)
Quick Summary – XML Tree in One Page
- XML is always a single-rooted tree
- Root → has children
- Children → can be elements, text, attributes
- Elements can contain:
- text
- other elements
- attributes
- mixtures of text + elements (mixed content)
- We use terms like: root, parent, child, sibling, ancestor, descendant, leaf, depth, path
Think of it like a family tree, company org chart, or folder structure — just written with tags.
Would you like to continue with one of these next?
- Drawing more complex XML trees (with namespaces, mixed content, comments)
- How programmers “walk” the tree (very simple code examples)
- What is XPath and how it uses the tree
- Difference between element tree vs full node tree (text nodes, attribute nodes…)
- How attributes fit into the tree concept
- Common real-world XML trees (SOAP, Android manifest, Office XML, e-invoice…)
Tell me which direction feels most helpful right now! 😊
