带有for循环内部的foreach-loop不会显示错误

时间:2016-05-09 12:58:44

标签: php for-loop foreach domdocument

我正在尝试从多个XMLDocuments中获取某些值。 你能告诉我这里我做错了什么吗?我是PHP语法的新手,它不会输出任何错误。我“从频道获取元素”和“从频道输出元素”的第一部分工作正常,但输出项目的for循环不起作用。

error_reporting(E_ALL);

$xmlDoc0 = new DOMDocument();
$xmlDoc0->load("http://www.heute.at/storage/rss/rss/heute.xml");
$xmlDoc1 = new DOMDocument();
$xmlDoc1->load("http://kurier.at/newsfeed/oesterreich_rss.xml");

$xmlDocArray = array(
            0 => $xmlDoc0,
            1 => $xmlDoc1
            );

foreach ($xmlDocArray as $key => $value){

//get elements from "<channel>"
$channel=$value->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;

//output elements from "<channel>"
echo("<p><a href='" . $channel_link
  . "'>" . $channel_title . "</a>");
echo("<br>");
echo($channel_desc . "</p>"); 

//get "<item>" elements
$x=$value->getElementsByTagName('item');
for ($i=0; $i<=2; $i++) {
  $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_img=$x->item($i)->getElementsByTagName('enclosure')
  ->item(0)->getAttribute('url');

  //output "<item>" elements
  echo ("<a href='" . $item_link
  . "'>" . $item_title . "</a>");

}
}

0 个答案:

没有答案