About Me

Friday, 13 April 2012

XML Tree - Child

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:

0 comments:

Post a Comment