无法修复错误:类DOMNodeList的对象无法转换为字符串

问题描述 投票:-2回答:1

我试图显示所有内容的XML,但我不断收到此错误:

可捕获的致命错误:无法将类DOMNodeList的对象转换为第43行的C:\ xampp \ htdocs \ bollywood \ rss1.php中的字符串

请帮我理解我做错了什么。

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>

这是我的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);
  }


  ?> 

这是LINE 43,我不断收到错误:

$item_ContentItem=$xpath->$nodeLists->item(0)->childNodes->item(0)->nodeValue;

请帮忙,我真的很感激。

php xml xpath domxpath
1个回答
0
投票

你有这个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=""]

在第一个根级别(<ContentItem>的相同级别,如果根节点是<title>)搜索具有空<channel>属性的Href节点:您的所需节点不是第一个根级别,并且没有空的Href属性,因此您的查询失败。

要使用<ContentItem>属性在任何树位置搜索Href,您必须使用以下模式:

//ContentItem[@Href]

//的意思是:“在任何位置搜索”)

然后,<ContentItem>没有节点值,其子节点是两个<MediaType>节点和一个<Property>节点(两者都没有节点值)。您的

$nodeLists->item(0)->childNodes->item(0)->nodeValue;

选择<MediaType FormalName="Picture" />并尝试提取其节点值,一个空字符串。

要检索Href属性,请使用以下语法:

$nodeLists = $xpath->query( '//ContentItem[@Href]' );
$item_ContentItem = $nodeLists->item(0)->getAttribute( 'Href' );

或者 - 如果要直接使用XPath选择属性:

$item_ContentItem = $xpath->query( '//ContentItem[@Href]/@Href' )->item(0)->nodeValue;
© www.soinside.com 2019 - 2024. All rights reserved.