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.

Recommended Answers

All 9 Replies

You need to create the media namespace. See the manual.

<?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 ??

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?

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).

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

^^ 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');
?>

You're right^^ it does work!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.