skyzyx
1/9/2010 - 8:22 PM

Doing an XPath search over SimpleXML nodes for the Amazon Product Advertising Service.

Doing an XPath search over SimpleXML nodes for the Amazon Product Advertising Service.

<?php
include '../cloudfusion.class.php';

$pas = new AmazonPAS(); // Using CloudFusion 2.5 development build. In this case, AmazonPAS == AmazonAAWS.
$response = $pas->item_search('brad paisley', array(
	'ResponseGroup' => 'Large'
));

// Grab the first browseNodeId for this content, typecasting the value as a string
$node = (string) $response->body->Items->Item[0]->BrowseNodes->BrowseNode->BrowseNodeId;

// Lookup the browseNode
$nodeinfo = $pas->browse_node_lookup($node);

// Grab a reference to the element that you're going to do an XPath search from
$browseNode = $nodeinfo->body->BrowseNodes->BrowseNode;

// Get a list of namespaces for the XML document
$ns = $browseNode->getNameSpaces(true);

// Register the non-named, default namespace as 'aws' for use with your XPath query
$browseNode->registerXPathNamespace('aws', $ns['']);

// Do the search for all 'Ancestors' nodes, using the registered namespace
$xmlNode = $browseNode->xpath('//aws:Ancestors');

// Count the array that xpath() sends back
$nodeCount = count($xmlNode);

header('Content-type: text/plain; charset=utf-8');
print_r($nodeCount);