Although we don't have to do this, it is generally easier than accessing the document
directly using $parser->document.
Next we use the attributes() method. This method returns the value of an
attribute from the current node. When we use this method we supply the name of
the attribute we wish to retrieve, in this case name. If a requested attribute does not
exist, null is returned.
APIs and Web Services
[ 280 ]
If we want to retrieve all of the attributes associated with a node, we simply
omit to pass the name of an attribute. This returns an associative array of the node's
attributes.
What if, for some reason, there was a possibility that the root node wasn't of the
expected type? We can use the name() method to get the name of the node type; in
our case we are checking for a catalogue node:
if ($document->name() != 'catalogue')
{
// handle invalid root node
}
Nodes can have child nodes; in the case of our example, the root node has one child
node, album. The root node could well contain more album nodes.
Pages:
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383