【问题标题】:Can't fix the error: Object of class DOMNodeList could not be converted to string无法修复错误:DOMNodeList 类的对象无法转换为字符串
【发布时间】: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-&gt;$nodeLists-&gt; ... 更改为$item_ContentItem=$nodeLists-&gt; ...
  • 谢谢!现在,我收到此错误:注意:尝试在第 43 行获取 C:\xampp\htdocs\bollywood\rss1.php 中非对象的属性致命错误:在 C 中调用 null 时的成员函数 item(): \xampp\htdocs\bollywood\rss1.php 第 43 行
  • $item_ContentItem 中你想要什么?
  • 我认为,这是一个布尔值,这就是它不起作用的原因。但我不知道如何将其设置为布尔值。但我正在尝试从 ContentItem 获取 href 链接。

标签: php xml xpath domxpath


【解决方案1】:

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

在第一根级别搜索&lt;ContentItem&gt; 节点(与&lt;title&gt; 相同,如果根节点为&lt;channel&gt;),Href 属性为空:您想要的节点不在第一根级别,并且不'没有空的Href 属性,因此您的查询失败。

要在具有Href 属性的任何树位置搜索&lt;ContentItem&gt;,您必须使用此模式:

//ContentItem[@Href]

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

那么&lt;ContentItem&gt;没有节点值,它的子节点是两个&lt;MediaType&gt;节点和一个&lt;Property&gt;节点(都没有节点值)。你的

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

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

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

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

或者——如果你想用 XPath 直接选择属性:

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

【讨论】:

  • 非常感谢!你是救生员!它已经完成并且完美地工作了。谢谢!!
猜你喜欢
  • 2015-05-15
  • 2013-07-03
  • 2014-11-21
  • 2015-02-05
  • 2012-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多