Bookmark and Share

XML Family Tree

You may have seen a lot of XML material make references to descendants, children, parents, or even great grandchildren and wondered to yourself, "Am I reading a family tree or am I learning XML?" The terminology that XML uses to describe the various relationships in a document can be confusing at first, which is why we wrote a set of lessons to clearly explain and show the most commonly used relation terms.

Advertise on Tizag.com

XML - The Tree Structure

The great benefit about XML is that the document itself describes the structure of data. If any of you have researched your family history, you have probably come across a family tree. At the top of the tree is some early ancestor and at the bottom of the tree are the latest children.

With a tree structure you can see which children belong to which parents, which grandchildren belong to which grandparents and many other relationships.

The neat thing about XML is that it also fits nicely into this tree structure, often referred to as an XML Tree.

Before you even think about matching up these tree terms with their XML counterparts, it might be helpful to see a real XML document converted into a tree structure. The lemonade.xml document we will be using was created by a particularly enterprising kid to keep inventory of her lemonade stand.

XML Code, lemonade.xml:

<inventory>
	<drink>
		<lemonade>
			<price>$2.50</price>
			<amount>20</amount>
		</lemonade>
		<pop>
			<price>$1.50</price>
			<amount>10</amount>
		</pop>
	</drink>

	<snack>
		<chips>
			<price>$4.50</price>
			<amount>60</amount>
		</chips>
	</snack>
</inventory>

XML Tree:

As you can see from the XML document, inventory is the root element. When we convert the document over to a tree format, we can see that the inventory element is at the top of the tree. In other words, all other elements are a descendent of the inventory element.

Construction of an XML tree isn't all that hard to do, as long as you follow some basic rules.

XML Tree Rules

We've written up a set of rules to use in figuring out if an element is a child or parent of another element. With these basic rules, you can figure out the relationship of just about any two elements in an XML document.

  • Descendants: If element A is contained by element B, then A is a descendant of B. In the lemonade.xml example, every element is a descendant of inventory because inventory is the root element.
  • Ancestors: If an element B contains element A, then B is an ancestor of A. In the lemonade.xml example, drink is the ancestor of lemonade and pop.

XML Family Tree Continued

The next few lessons will go into more detail about descendant, ancestor, child, parent and sibling relationships in an XML document. If you are still attempting to grasp the notion of the XML Tree structure, we recommend that you carefully read through each of these lessons.

Bookmark and Share




Download Tizag.com's XML Book

If you would rather download the PDF of this tutorial, check out our XML eBook from the Tizag.com store.

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials