954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to get attribute (image) from xml

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

asif49
Posting Whiz in Training
245 posts since Dec 2010
Reputation Points: 5
Solved Threads: 0
 

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

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
<?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 ??

asif49
Posting Whiz in Training
245 posts since Dec 2010
Reputation Points: 5
Solved Threads: 0
 

Almost there, you don't need create function but getElementsByTagNameNS(): http://www.php.net/manual/en/domdocument.getelementsbytagnamens.php

$i = $xmlDoc->getElementsByTagNameNS('http://search.yahoo.com/mrss/"','thumbnail');
echo $i->item(0)->getAttribute('url');

bye :)

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

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?

asif49
Posting Whiz in Training
245 posts since Dec 2010
Reputation Points: 5
Solved Threads: 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).

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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

asif49
Posting Whiz in Training
245 posts since Dec 2010
Reputation Points: 5
Solved Threads: 0
 

Okay, next time say so, to avoid this ;)

Thought I had code, but cannot locate it. If I find it, I'll post it.

http://blog.sherifmansour.com/?p=302

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
^^ 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');
?>
cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

You're right^^ it does work!

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

asif49
Posting Whiz in Training
245 posts since Dec 2010
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: