XML Tree - Child
When a man loves a woman very, very much, sometimes a kid shows up. This kid is the child of the man and woman.
In XML, an element can have many or no children. Element A is the child of element B when:
- Element A is contained within element B and is exactly one level below element B.
In other words, B is the parent of A. In this example XML document there are three child relationships. Can you find them?
XML Code:
<a>
<b>
<c>
<d>
</d>
</c>
</b>
</a>
- Element b is the child of element a.
- Element c is the child of element b.
- Element d is the child of element c.
Do You Know Where Your Children Are?
To find the children of element A, look between A's opening and closing tags. If there are elements which are only contained by element A, then they are A's children elements.
Can you find the children elements of chips?
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>
price and amount are the children of chips.
If it helps, you can always think of an XML document in a tree structure. Here, we have highlighted the children of chips in our lemonade.xml document.
XML Tree:
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! |