【发布时间】:2016-04-12 13:07:18
【问题描述】:
我正在尝试显示包含所有内容的 XML,但我不断收到此错误:
可捕获的致命错误: DOMNodeList 类的对象无法在第 43 行的 C:\xampp\htdocs\bollywood\rss1.php 中转换为字符串
请帮助我理解我做错了什么。
XML 代码:
<channel><title>Entertainment News</title>
<link>http://www.yournewssite.com</link>
<description>All the latest news and gossip</description>
<item>
<title>title!!</title>
<link>http://www.yournewssite.com</link>
<description><![CDATA[<img src=http://yourwebsite.com/i.php?k=d88d4e2b336966b538983783230051c width=100 height=100>
<BR>Backstreet Boys, *NSYNC, 98 Degrees and O-Town boy band members have collaborated on a song to promote their new movie. <BR>]]>
</description>
<ContentItem Href="http://yourwebiste.com/i.php?k=d88d4e2b336966b538983783230051c7">
<MediaType FormalName="Picture" />
<MimeType FormalName="image/jpg" />
<Property FormalName="caption" value="Nick Carter" />
</ContentItem>
这是我的 PHP 代码:
<?php
$q=$_GET["q"];
if($q=="Google") {
$xml=("google.xml");
} elseif($q=="b") {
$xml=("sample.xml");
}
$xmlDoc = new DOMDocument();
$xmlDoc->load($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;
//output elements from "<channel>"
echo("<p><a href='" . $channel_link
. "'>" . $channel_title . "</a>");
echo("<br>");
echo($channel_desc . "</p>");
$xpath = new DOMXpath($xmlDoc);
$nodeLists = $xpath->query('ContentItem[@Href=""]');
//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i< $x->length; $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=html_entity_decode($x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue);
$item_ContentItem=$xpath->$nodeLists->item(0)->childNodes->item(0)->nodeValue;
echo ("<p><a href=''" . $item_link
. "'>" . $item_title . "</a>");
echo ("<br>");
echo ($item_desc);
echo ("<br>");
echo var_dump($item_ContentItem);
}
?>
这是第 43 行,我不断收到错误:
$item_ContentItem=$xpath->$nodeLists->item(0)->childNodes->item(0)->nodeValue;
请帮忙,我将不胜感激。
【问题讨论】:
-
将
$item_ContentItem=$xpath->$nodeLists-> ...更改为$item_ContentItem=$nodeLists-> ... -
谢谢!现在,我收到此错误:注意:尝试在第 43 行获取 C:\xampp\htdocs\bollywood\rss1.php 中非对象的属性致命错误:在 C 中调用 null 时的成员函数 item(): \xampp\htdocs\bollywood\rss1.php 第 43 行
-
$item_ContentItem中你想要什么? -
我认为,这是一个布尔值,这就是它不起作用的原因。但我不知道如何将其设置为布尔值。但我正在尝试从 ContentItem 获取 href 链接。