0

http://feeds.bbci.co.uk/news/rss.xml
^^ I'm extracting data from the above website. I can extract most actual elements using the following code:

$xml="http://feeds.bbci.co.uk/news/rss.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;

...but I can't read the attributes from <media:thumbnail height="........." width="......." url=".........">. In this particular case I need to read "url" and I think although it will be possible using string manipulation after getting the whole whole code as String, but I can't seem to be able to find any source where to do this in a simple manner like I do above.

Edited by asif49: n/a

3
Contributors
9
Replies
16
Views
5 Years
Discussion Span
Last Post by asif49
0
<?php
$dom = new DOMDocument('1.0', 'utf-8');

$element = $dom->createElementNS('http://feeds.bbci.co.uk/news/england/london/rss.xml', 'media:thumbnail', 'url');

// We insert the new element as root (child of the document)
$dom->appendChild($element);

echo $dom->saveXML();
?>

I can't seem to get it right where I'm supposed to put the attribute name or element name ??

Edited by asif49: n/a

0

I used the following code like you said

<?php
$xmlDoc = new DOMDocument();
$i = $xmlDoc->getElementsByTagNameNS('http://feeds.bbci.co.uk/news/england/london/rss.xml','thumbnail');
echo $i->item(0)->getAttribute('url');
?>

and i get this error...

Fatal error: Call to a member function getAttribute() on a non-object in C:\xampp\htdocs\Web\DSA\test.php on line 4

Am I still doing something wrong?

0

The namespace is 'media', not 'thumbnail'. You should use the URL from the xmlns:media definition, instead of the URL of the RSS (the URL cereal showed).

Edited by pritaeas: n/a

0

^^ I tried media, thumbnail, media:thumbnail and every other possibility already lol. didn't work and that's why I asked again

2

^^ I tried media, thumbnail, media:thumbnail and every other possibility already lol. didn't work and that's why I asked again

use my code posted above, without changing anything, is tested and works, in this case you don't have to set a namespace because we are using getElementsByTagNameNS() which needs just the namespace URI for media, which, in your case, is http://search.yahoo.com/mrss/

So, for completeness:

<?php
$xml="http://feeds.bbci.co.uk/news/rss.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$i = $xmlDoc->getElementsByTagNameNS('http://search.yahoo.com/mrss/','thumbnail');
echo $i->item(0)->getAttribute('url');
?>

Edited by cereal: n/a

0

You're right^^ it does work!

Thank you, I mustn't have made a mistake when I was last checking it. +1'd

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.