...

View Full Version : $xmlDoc = new DOMDocument()- loading first 5 items help



LJackson
05-04-2009, 08:09 PM
Hi all

im having trouble with some code of mine

<?php

$xml=("http://www.amazon.co.uk/rss/new-releases/dvd/ref=pd_nr_rss_link");


$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

for ($i = 0; $i < 5; $i++) {
//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('item')->item($i);
$channel_title = $channel->getElementsByTagName('title')->item($i)->childNodes->item(0)->nodeValue;

echo $channel_title;
echo("<br />");
}
?>

i get the first item title display but after that i get

Notice: Trying to get property of non-object in C:\wamp\www\Website\testfeedamazon.php on line 12

Fatal error: Call to a member function item() on a non-object in C:\wamp\www\Website\testfeedamazon.php on line 12

not sure why thought...
cheers

venegal
05-04-2009, 08:48 PM
$channel_title = $channel->getElementsByTagName('title')->item($i)->childNodes->item(0)->nodeValue;

Well, that doesn't make much sense. You are already inside the "item" node; there's only one "title" node there, so calling item($i) produces an error for all but the first node.

Use

$channel_title = $channel->getElementsByTagName('title')->item(0)->nodeValue;
instead and it will work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum