Can't fix the error: Object of class DOMNodeList could not be converted to string
Can't fix the error: Object of class DOMNodeList could not be converted to string
我正在尝试显示 XML 的所有内容,但我一直收到此错误:
Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\xampp\htdocs\bollywood\rss1.php on line 43
请帮助我理解我做错了什么。
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;
请帮忙,我将不胜感激。
你有这个XML:
<ContentItem Href="http://yourwebiste.com/i.php?k=d88d4e2b336966b538983783230051c7">
<MediaType FormalName="Picture" />
<MimeType FormalName="image/jpg" />
<Property FormalName="caption" value="Nick Carter" />
</ContentItem>
此 XPath 模式:
ContentItem[@Href=""]
在第一个根级别(如果根节点是 <channel>
,则与 <title>
同一级别)搜索具有空 Href
属性的 <ContentItem>
节点:您的desired 节点不在第一个根级别并且没有空的 Href
属性,因此您的查询失败。
要在具有 Href
属性的任何树位置搜索 <ContentItem>
,您必须使用此模式:
//ContentItem[@Href]
(//
表示:“在任意位置搜索”)
那么,<ContentItem>
没有节点值,它的子节点是两个<MediaType>
节点和一个<Property>
节点(都没有节点值)。你的
$nodeLists->item(0)->childNodes->item(0)->nodeValue;
select <MediaType FormalName="Picture" />
并尝试提取其节点值,一个空字符串。
要检索 Href
属性,请使用以下语法:
$nodeLists = $xpath->query( '//ContentItem[@Href]' );
$item_ContentItem = $nodeLists->item(0)->getAttribute( 'Href' );
或者 — 如果你想直接 select 带有 XPath 的属性:
$item_ContentItem = $xpath->query( '//ContentItem[@Href]/@Href' )->item(0)->nodeValue;
我正在尝试显示 XML 的所有内容,但我一直收到此错误:
Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\xampp\htdocs\bollywood\rss1.php on line 43
请帮助我理解我做错了什么。
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;
请帮忙,我将不胜感激。
你有这个XML:
<ContentItem Href="http://yourwebiste.com/i.php?k=d88d4e2b336966b538983783230051c7">
<MediaType FormalName="Picture" />
<MimeType FormalName="image/jpg" />
<Property FormalName="caption" value="Nick Carter" />
</ContentItem>
此 XPath 模式:
ContentItem[@Href=""]
在第一个根级别(如果根节点是 <channel>
,则与 <title>
同一级别)搜索具有空 Href
属性的 <ContentItem>
节点:您的desired 节点不在第一个根级别并且没有空的 Href
属性,因此您的查询失败。
要在具有 Href
属性的任何树位置搜索 <ContentItem>
,您必须使用此模式:
//ContentItem[@Href]
(//
表示:“在任意位置搜索”)
那么,<ContentItem>
没有节点值,它的子节点是两个<MediaType>
节点和一个<Property>
节点(都没有节点值)。你的
$nodeLists->item(0)->childNodes->item(0)->nodeValue;
select <MediaType FormalName="Picture" />
并尝试提取其节点值,一个空字符串。
要检索 Href
属性,请使用以下语法:
$nodeLists = $xpath->query( '//ContentItem[@Href]' );
$item_ContentItem = $nodeLists->item(0)->getAttribute( 'Href' );
或者 — 如果你想直接 select 带有 XPath 的属性:
$item_ContentItem = $xpath->query( '//ContentItem[@Href]/@Href' )->item(0)->nodeValue;