问题描述
我正在尝试从 RSS 提要获取一些数据,但在获取 img 的 src 属性时遇到问题.该图像位于 //item/description/div[@class="separator"]//img
并且我所做的一项尝试是加入具有
I am trying to get some data from a RSS feed but I am having problem on getting the src attribute of img. The image is located at //item/description/div[@class="separator"]//img
and one attempt I made is to join the posts xPath having
推荐答案
那里:
$imagespath = $IMAGES->item(0)->nodeValue;
您正在将某些 标记的内容放入
$imagespath
变量中.
因此,该变量不包含 DOMElement
或任何类型的对象,而是一些字符串内容.
You are getting the content of some <description>
tag into the $imagespath
variable.
That variable, so, doesn't contain a DOMElement
nor any kind of object, but some string content.
然后,您尝试对该变量调用 getAttribute()
方法:
$image = $imagespath->getAttribute('src');
但是由于 $imagespath
不是对象(它是一个字符串),你会得到一个致命错误.
But as $imagespath
is not an object (it's a string), you get a Fatal Error.
如果你想获得某个标签的 src
属性,我想你应该用这样的东西来获得它:
If you want to get the src
attribute of some tag, I suppose you should get it with something like this :
$image = $IMAGES->item(0)->getAttribute('src');
即读取 XPath 查询返回的节点的 src
属性——当然,在调用 getAttribute()
之前,您应该检查查询是否确实返回了一个项目.
i.e. reading the src
attribute of a node returned by your XPath query -- of course, before calling getAttribute()
, you should check that there is actually an item returned by the query.
另外,如果你想加载图片,也许你应该得到 标签:
$IMAGES = $post->getElementsByTagName( "img" );
而不是 的?
这篇关于获取 src 属性时出错.(关于如何正确获取路径的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!