XML — Tree
XML Tree Structure
<bookstore>
<book>
<title>Harry Potter</title>
<author>J.K. Rowling</author>
</book>
<book>
<title>Learning XML</title>
<author>Erik T. Ray</author>
</book>
</bookstore>
Tree Concepts
| Term | Description |
|---|---|
| Root | Top-level element |
| Parent | Element containing children |
| Child | Element inside a parent |
| Sibling | Elements at same level |
| Leaf | Element with no children |
Tree Traversal
bookstore (root)
├── book (child)
│ ├── title (leaf)
│ └── author (leaf)
└── book (child)
├── title (leaf)
└── author (leaf)
Navigating the Tree
- Parent: Go up one level
- Children: Go down one level
- Siblings: Go left or right
- Ancestors: Go up multiple levels
- Descendants: Go down multiple levels
XPath Navigation
/bookstore/book[1] # First book
/bookstore/book/title # All titles
//book[@genre='fiction'] # Fiction books
/bookstore/book[last()] # Last book
Mini Practice
- Draw a tree for an XML document
- Identify parent, child, sibling relationships
- Write XPath expressions
- Navigate from root to leaf
Up Next
Continue with DOM — Document Object Model for XML.
Related Topics
Frequently Asked Questions about Tree
What is Tree in XML?
Tree is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Tree?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Tree.
Why is Tree important in XML?
Tree is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.