PDA

View Full Version : using and displaying large xml files?


LJackson
04-18-2010, 10:21 PM
Hi All,

here is a snippet of an xml file i have
<merchantProductFeed>

<merchant id="1418">

<prod id="9887290">
<pId>91625</pId>

<text>
<name>A River Runs Through It</name>
</text>

<uri>

<awTrack>
http://www.awin1.com/pclick.php?p=9887290&a=79524&m=1418
</awTrack>

<awImage>
http://images.productserve.com/preview/1418/9887290.jpg
</awImage>
</uri>

<price>
<store>0.00</store>
<rrp>9.99</rrp>
<delivery>0.00</delivery>
</price>
<cat/>
<brand/>
</prod>

and i am just trying to get the product id and name and display it, for now but i cant seem to do it :(

here is my code
// xml file in the same directory as this file -
$feed_url = "http://www.kernow-connect.com/feedtest/dvdfeed.xml";

// Load XML.
$xml=simplexml_load_file($feed_url);

// How many items to display from the RSS Feed ...
$count = 5;

foreach ($xml->merchantProductFeed->merchant as $item)
{
if($count > 0)
{
echo"
$item->prod->id<br />
$item->prod->text->name<br />
<br />";
}

$count--;
}


any ideas where ive gone wrong?

thanks

Dormilich
04-18-2010, 11:34 PM
any error messages?

LJackson
04-18-2010, 11:41 PM
sorry mate, that would help...

im getting this error msg
Warning: Invalid argument supplied for foreach() in C:\wamp\www\feeds\feedtest.php on line 20

p.s the php code above has changed slightly

thanks mate

Dormilich
04-19-2010, 07:43 AM
it simply means, $xml->merchantProductFeed->merchant (or what it is now) is not iterable or an array.

LJackson
04-19-2010, 10:39 AM
oh ok, any idea based on th snippet how i can pull out the prod name and id please

thanks
Luke

Dormilich
04-19-2010, 11:00 AM
you could use an XPath to get all the required nodes, examples can be found in the manual (http://php.net/simplexml).