Error getting src attribute. (question about how to get the path correctly)

I am trying to get some data from an RSS feed, but I had a problem getting the src attribute for img. The image is in //item/description/div[@class="separator"]//img , and one attempt I made was to join xPath messages with

+1
source share
2 answers

There:

 $imagespath = $IMAGES->item(0)->nodeValue; 

You get the contents of the <description> in the $imagespath variable.
This variable, therefore, does not contain a DOMElement and no object type, but some string content.


Then you try to call the getAttribute() method for this variable:

 $image = $imagespath->getAttribute('src'); 

But since $imagespath not an object (this is a string), you get Fatal Error.


If you want to get the src attribute of a tag, I suppose you should get it something like this:

 $image = $IMAGES->item(0)->getAttribute('src'); 

i.e. reading the src node attribute returned by your XPath request - of course, before calling getAttribute() you should verify that there is actually an element returned by the request.


Also, if you want to upload images, perhaps you should get the <img> tags:

 $IMAGES = $post->getElementsByTagName( "img" ); 

And not a <description> unit?

+1
source

If someExpr XPath expression selects one someElem element, then the following XPath expression:

 someExpr/@src 

selects the src attribute of someElem (if it has the src attribute).

In your case :

 //item/description/div[@class="separator"]//img/@src 
0
source

All Articles