XML Family Tree

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.

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:



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 inventoryelement 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 inventorybecause 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.