</>
Skip to content
XML lessons (10/29)

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

TermDescription
RootTop-level element
ParentElement containing children
ChildElement inside a parent
SiblingElements at same level
LeafElement 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

  1. Draw a tree for an XML document
  2. Identify parent, child, sibling relationships
  3. Write XPath expressions
  4. 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.