PHP Classes

Looping nodes

Recommend this page to a friend!

      SimpleXML for PHP4  >  All threads  >  Looping nodes  >  (Un) Subscribe thread alerts  
Subject:Looping nodes
Summary:How to do a foreach
Messages:1
Author:mundaka
Date:2009-10-16 08:41:00
 

  1. Looping nodes   Reply   Report abuse  
Picture of mundaka mundaka - 2009-10-16 08:41:01
Hi there

First of all, thanks you for the nice class.

I've this xml:

<products>
<product>
<ref>001</ref>
<name>First</name>
</product>
<product>
<ref>002</ref>
<name>Second</name>
</product>
</products>


And I am doing this:

//#################################
$sxml = new simplexml;
$sxml->ignore_level = 1;

$data = $sxml->xml_load_file($file);

echo $data->product->ref;
//#################################

And it works perfectly, but I need to loop all the product nodes, so I am trying this with no luck:

foreach($data->product as $item){
echo $item->ref;
}

How can access each node?

Thanks!