simplexml and xpath - Need some help

Posted on 16th Feb 2014 by admin

Hello,

I am trying to parse an xml which comes back from Amazon and I am trying to know how many Browsenodes are shown within the XML.

I am using the classs Tarzan to get the xml from Amazon and the returned data is automatically placed into simplexml.

So currently I have this script

Code: $nodeinfo = $pas->browse_node_lookup($node);

$xmlNode = $nodeinfo ->body->BrowseNodes->BrowseNode->xpath('/Ancestors');
$nodeCount = count ( $xmlNode );
if($nodeCount){
echo "NodeCount".$nodeCount."n";
}
The $nodeinfo holds the following xml structure and I am only showing the part for the Nodes
Code: [BrowseNodes] => SimpleXMLElement Object
(
[Request] => SimpleXMLElement Object
(
[IsValid] => True
[BrowseNodeLookupRequest] => SimpleXMLElement Object
(
[BrowseNodeId] => 16197
)

)

[BrowseNode] => SimpleXMLElement Object
(
[BrowseNodeId] => 16197
[Name] => Epic
[Ancestors] => SimpleXMLElement Object
(
[BrowseNode] => SimpleXMLElement Object
(
[BrowseNodeId] => 16190
[Name] => Fantasy
[Ancestors] => SimpleXMLElement Object
(
[BrowseNode] => SimpleXMLElement Object
(
[BrowseNodeId] => 25
[Name] => Science Fiction & Fantasy
[Ancestors] => SimpleXMLElement Object
(
[BrowseNode] => SimpleXMLElement Object
(
[BrowseNodeId] => 1000
[Name] => Subjects
[IsCategoryRoot] => 1
[Ancestors] => SimpleXMLElement Object
(
[BrowseNode] => SimpleXMLElement Object
(
[BrowseNodeId] => 283155
[Name] => Books

with the Xpath I am trying to get the number how often "Ancestors" is shown. With this count I am able to goto my next programming script.

With my script this is not working, it always gives me a 1.

Is there a better way then how I have tried to solve it to count the Ancesters node?

Is there also a way how to read the Nodes backway to front? Amazon gives the Structure of an item from the lowest group to the highest.

I am kind of new with Simplexml and would really need some help.

Hope some one can help me out.

Best regards

Other forums