<?php
$q=$_GET["q"];
$xml = $q;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
echo("<b> $channel_title </b>" );
echo("<br>");
echo($channel_desc . "</p>");
$x=$xmlDoc->getElementsByTagName('item');
$i=0;
while($i<=9)
{
$i++;
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
echo ("<b> $item_title </b>" );
echo ("<br>");
echo ($item_desc . "</p>");
}
?>
我收到此错误:
致命错误:在a上调用成员函数getElementsByTagName() C:\ Program Files中的非对象 (x86)\ EasyPHP-DevServer-14.1VC11 \ data \ localweb \ v9 \ naloga2.php在线 19。
我不知道如何解决这个问题,我真的非常需要它。那么任何想法如何解决这个问题? 当RSS中的项目少于10个时,我收到此错误。
答案 0 :(得分:0)
你有while($i<=9)
然后你要求每个元素,0-9。因此,当您没有10个元素时,$x->item($i)
不返回任何内容,因此调用->getElementsByTagName('title')
会产生错误,因为您在没有任何内容的情况下调用该方法。
您想要重组为:
<?php
$q=$_GET["q"];
$xml = $q;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
echo("<b> $channel_title </b>" );
echo("<br>");
echo($channel_desc . "</p>");
$x=$xmlDoc->getElementsByTagName('item');
$counter = 0 ;
foreach($x as $item)
{
if(++$counter > 9) break;
$item_title=$item->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$item->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
echo ("<b> $item_title </b>" );
echo ("<br>");
echo ($item_desc . "</p>");
}
?>
答案 1 :(得分:0)
假设有8个项目。你的代码
$i=0;
while($i<=9) {
$i++;
...
}
迭代到i === 10.因此这段代码
$item_title = $x->item($i)
不返回元素 - 因为最后一个元素的索引为7 [有效0 .. 7]。
试试这个:
for( $i=0; $i < $x->length; $i++ ) {
...
}
答案 2 :(得分:-1)
<?php
$q=$_GET["q"];
$xml = $q;
$xmlDoc = new DOMDocument();
$xmlDoc->load("rss.xml");
//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
$channel_guid = $channel->getElementsByTagName('guid')
->item(0)->childNodes->item(0)->nodeValue;
//output elements from "<channel>"
echo("<p><a href='" . $channel_link
. "' style='color:blue;'>" . $channel_title . "</a>");
echo("<br>");
echo("<a style='color:lightgreen;'>" . $channel_desc . "</p>");
echo("<a style='color:lightgreen;'>" . $item_guid . "</p>");
//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
$counter = 0 ;
foreach($x as $item)
{
if(++$counter > 9) break;
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
$item_guid=$x->item($i)->getElementsByTagName('guid')
->item(0)->childNodes->item(0)->nodeValue;
echo ("<p><a href='" . $item_link
. "' style='color:blue;'>" . $item_title . "</a>");
echo ("<br>");
echo ("<a style='color:lightgreen;'>" . $item_desc . "</p>");
echo ("<a style='color:lightgreen;'>" . $item_guid . "</p>");
}
?>
标题+链接+说明+ guid +轻松添加/否致命错误/