简体   繁体   English

致命错误:在非对象上调用成员函数 getElementsByTagName()

[英]Fatal error: Call to a member function getElementsByTagName() on a non-object

<?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>"); 
  }
?>

I get this error :我收到此错误:

Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\\Program Files (x86)\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\v9\\naloga2.php on line 19.致命错误:调用第 19 行 C:\\Program Files (x86)\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\v9\\naloga2.php 中非对象的成员函数 getElementsByTagName()。

I get this error when there are less than 10 items in RSS.当 RSS 中的项目少于 10 个时,我会收到此错误。

You have while($i<=9) then you ask for each element, 0-9.你有while($i<=9)然后你要求每个元素,0-9。 So when you don't have 10 elements, $x->item($i) returns nothing, and thus calling ->getElementsByTagName('title') gives an error since you're calling the method on nothing.因此,当您没有 10 个元素时, $x->item($i)返回任何内容,因此调用->getElementsByTagName('title')会产生错误,因为您没有调用该方法。

You want to restructure to this:你想重组为:

<?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>"); 
  }
?>

Suppose there are 8 items.假设有 8 个项目。 Your code你的代码

$i=0;
while($i<=9) {
  $i++;
  ...
}

iterates up to i === 10. Therefore this code迭代到 i === 10。因此这段代码

$item_title = $x->item($i)

doesn't return an element - since the last element has an index of 7 [valid 0 .. 7].不返回元素 - 因为最后一个元素的索引为 7 [valid 0 .. 7]。

Try this:尝试这个:

  for( $i=0; $i < $x->length; $i++ ) {
    ...
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HTML表解析PHP domDocument(PHP致命错误:在非对象上调用成员函数getElementsByTagName()) - PHP domDocument parsing with HTML Table ( PHP Fatal error: Call to a member function getElementsByTagName() on a non-object) 在非对象上调用成员函数getElementsByTagName() - Call to a member function getElementsByTagName() on a non-object 错误:在非对象中调用成员函数getElementsByTagName() - Error: Call to a member function getElementsByTagName() on a non-object in 错误致命错误:在非对象上调用成员函数insert() - Error Fatal error: Call to a member function insert() on a non-object 致命错误:当它是一个对象时,调用非对象上的成员函数 - Fatal Error: Call to member function on non-object when it is an object 致命错误:在非对象错误上调用成员函数prepare() - Fatal error: Call to a member function prepare() on a non-object ERROR 致命错误:在非对象错误上调用成员函数 query() - Fatal error: Call to a member function query() on a non-object Error Magento:致命错误:在非对象上调用成员函数 getMethodInstance() - Magento : Fatal error: Call to a member function getMethodInstance() on a non-object CakePHP致命错误调用非对象上的成员函数schema() - CakePHP Fatal Error Call to a member function schema() on a non-object 致命错误:在非对象上调用成员函数toHtml() - Fatal error: Call to a member function toHtml() on a non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM