I am trying to display XML with all the content but I keep getting this error:
Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\xampp\htdocs\bollywood\rss1.php on line 43
Please help me understand what I am doing wrong.
XML Code:
<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>
And this is my PHP code:
<?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);
}
?>
This is LINE 43, where I keep getting error:
$item_ContentItem=$xpath->$nodeLists->item(0)->childNodes->item(0)->nodeValue;
Please help, I would really appreciate it.
You have this XML:
<ContentItem Href="http://yourwebiste.com/i.php?k=d88d4e2b336966b538983783230051c7">
<MediaType FormalName="Picture" />
<MimeType FormalName="image/jpg" />
<Property FormalName="caption" value="Nick Carter" />
</ContentItem>
This XPath pattern:
ContentItem[@Href=""]
search for a <ContentItem>
node at first root level (same level of <title>
, if the root node is <channel>
) with an empty Href
attribute: your desired node is not at first root level and doesn't have an empty Href
attribute, so your query fails.
To search for <ContentItem>
in any tree position with Href
attribute you have to use this pattern:
//ContentItem[@Href]
(//
means: “search in any position”)
Then, <ContentItem>
has not a node value, and its childnodes are two <MediaType>
nodes and one <Property>
node (both without node values). Your
$nodeLists->item(0)->childNodes->item(0)->nodeValue;
select <MediaType FormalName="Picture" />
and try to extract its node value, an empty string.
To retrieve the Href
attributes, use this syntax:
$nodeLists = $xpath->query( '//ContentItem[@Href]' );
$item_ContentItem = $nodeLists->item(0)->getAttribute( 'Href' );
Or — if you want directly select attribute with XPath:
$item_ContentItem = $xpath->query( '//ContentItem[@Href]/@Href' )->item(0)->nodeValue;