O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Accessing Descendants

We've now had plenty of practice accessing the child nodes of an XML element. Next let's consider how to access not just an element's child nodes, but also its so-called descendant nodes. An element's descendants are all the nodes it contains, at any level of the XML hierarchy (i.e., grandchild nodes, great-grandchild nodes and so on).

For example, consider the XML fragment in Example 18-10, representing a book and movie loan transaction from a library.

Example 18-10. A library loan record

var loan:XML = <LOAN>
  <BOOK ISBN="0141182806" DUE="1136091600000">
    <TITLE>Ulysses</TITLE>
    <AUTHOR>Joyce, James</AUTHOR>
    <PUBLISHER>Penguin Books Ltd</PUBLISHER>
  </BOOK>

  <DVD ISBN="0790743086" DUE="1136610000000">
    <TITLE>2001 A Space Odyssey</TITLE>
    <DIRECTOR>Stanley Kubrick</DIRECTOR>
    <PUBLISHER>Warner Home Video</PUBLISHER>
  </DVD>


  <DVD ISBN="078884461X" DUE="1137214800000">
    <TITLE>Spirited Away</TITLE>
    <DIRECTOR>Hayao Miyazaki</DIRECTOR>
    <PUBLISHER>Walt Disney Video</PUBLISHER>
  </DVD>
</LOAN>

In the preceding example, the <LOAN> element's descendants include:

  • The direct children: <BOOK> and the two <DVD> elements

  • The grandchildren: every <TITLE>, <AUTHOR>, <PUBLISHER>, and <DIRECTOR> element

  • The great-grandchildren: every text node contained by the <TITLE>, <AUTHOR>, <PUBLISHER>, and <DIRECTOR> elements

To access an element's descendants we use the E4X descendant operator (..), which is used as follows:

theElement..identifier

A descendant-access expression returns an XMLList representing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required