XML Tree
The XML Tree Structure
Section titled “The XML Tree Structure”XML documents are inherently hierarchical, forming a tree structure. Understanding this structure is fundamental to working with XML.
An Example XML Document
Section titled “An Example XML Document”Let’s revisit a simple XML document:
<?xml version="1.0" encoding="UTF-8"?><note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>Breakdown:
- XML Declaration:
<?xml version="1.0" encoding="UTF-8"?>(Optional but recommended) Specifies the XML version and character encoding. - Root Element:
<note>This is the top-level element that encloses the entire document content. Every well-formed XML document must have exactly one root element. - Child Elements:
<to>,<from>,<heading>, and<body>are direct children of the<note>element. - Text Content: “Tove”, “Jani”, “Reminder”, and “Don’t forget me this weekend!” are the text content found within their respective parent elements.
XML’s syntax, using descriptive tags, makes the document relatively self-explanatory.
XML Documents Form a Tree
Section titled “XML Documents Form a Tree”Think of an XML document like an upside-down family tree or a file system directory structure:
- Root: The single top-level element (e.g.,
<note>or<bookstore>) is the root of the tree. - Nodes: Every part of the document—elements, attributes, text—is considered a node in the tree.
- Parent/Child Relationship: An element directly containing another element is its parent (e.g.,
<note>is the parent of<to>). The contained element is the child. - Siblings: Elements that share the same parent are siblings (e.g.,
<to>,<from>,<heading>, and<body>are siblings). - Ancestors/Descendants: A parent, grandparent, etc., is an ancestor. A child, grandchild, etc., is a descendant.
- Leaves: Nodes that have no children (like text nodes or empty elements) are often referred to as leaves of the tree.
This tree model is crucial because XML processing tools (like parsers, XSLT processors, DOM APIs) operate based on navigating this structure.
Consider this slightly more complex example:
<?xml version="1.0" encoding="UTF-8"?><bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price currency="USD">30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price currency="USD">29.99</price> </book></bookstore>Visualizing the Tree Structure:
(Root) bookstore | +-- book (category="COOKING") | | | +-- title (lang="en") | | +-- "Everyday Italian" | | | +-- author | | +-- "Giada De Laurentiis" | | | +-- year | | +-- "2005" | | | +-- price (currency="USD") | +-- "30.00" | +-- book (category="CHILDREN") | +-- title (lang="en") | +-- "Harry Potter" | +-- author | +-- "J K. Rowling" | +-- year | +-- "2005" | +-- price (currency="USD") +-- "29.99"In this tree:
<bookstore>is the root element.- The two
<book>elements are children of<bookstore>and siblings of each other. <title>,<author>,<year>, and<price>are children of their respective<book>parent.category,lang, andcurrencyare attribute nodes, associated with their respective elements (book,title,price).- The text like “Everyday Italian” or “2005” are text nodes, children of their respective elements (
title,year).
Understanding these relationships (parent, child, sibling, attribute, text content) is key to querying and manipulating XML data effectively.