An XML document has a self descriptive structure. It forms a tree structure which is referred as an XML tree. The tree structure makes easy to describe an XML document.
A tree structure contains root element (as parent), child element and so on. It is very easy to traverse all succeeding branches and sub-branches and leaf nodes starting from the root.
<?xml version="1.0"?> <college> <student> <firstname>Tamanna</firstname> <lastname>Bhatia</lastname> <contact>09990449935</contact> <email>email@example.com</email> <address> <city>Ghaziabad</city> <state>Uttar Pradesh</state> <pin>201007</pin> </address> </student> </college>
Let's see the tree-structure representation of the above example.
In the above example, first line is the XML declaration. It defines the XML version 1.0. Next line shows the root element (college) of the document. Inside that there is one more element (student). Student element contains five branches named <firstname>, <lastname>, <contact>, <Email> and <address>.
<address> branch contains 3 sub-branches named <city>, <state> and <pin>.
These rules are used to figure out the relationship of the elements. It shows if an element is a child or a parent of the other element.
Descendants: If element A is contained by element B, then A is known as descendant of B. In the above example "College" is the root element and all the other elements are the descendants of "College".
Ancestors: The containing element which contains other elements is called "Ancestor" of other element. In the above example Root element (College) is ancestor of all other elements.