We can use the getElementByPath() method to retrieve a node, provided that its
path is unique. An album will only ever have one of each of these sub-nodes.
This example iterates over the $albums array and outputs title, artist, and year
(we will deal with tracks shortly):
for ($i = 0, $c = count($albums); $i < $c; $i ++ )
{
// get the album
$album =& $albums[$i];
echo '
';
Chapter 10
[ 281 ]
if ($name =& $album->getElementByPath('title'))
{
// display title
echo ''.$name->data().'
';
}
if ($artist =& $album->getElementByPath('artist'))
{
// display the artist
echo ''.$artist->data().'';
}
if ($year =& $album->getElementByPath('year'))
{
// display the year of release
echo ' ('.$year->data().')';
}
echo '
';
}
Our use of the getElementByPath() method is clear. We simply pass the name of
the child node. In more complex data structures we might want to use a deeper path.
To do this we use forward slashes to separate the node names.
The other method that we use in the example is data().
Pages:
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385