Extracting an attribute with a SimpleXMLElement class
Posted: Tue Sep 27, 2011 4:44 am
Hi guys, and thanks for you help in advance. I would appreciate if you can help me with this code:
I have an XML with this structure:
And I have this function to read it:
My problem is that I don't know how to get the image from the "enclosure" tag on the XML. I have tried to use the "attributes" function but it seems that I don't know how to use it because it's just not working. Can anybody tell me what would be the right code to pull the image and use as follow (look for "THE-IMAGE-HERE" in the next code):
Thanks in advance
I have an XML with this structure:
Code: Select all
<item>
<title><![CDATA[Here is the title of the item]]></title>
<link><![CDATA[http://www.thedomain.com]]></link>
<description><![CDATA[The content of the item]]></description>
<enclosure url="http://www.thedomain.com/theimage.jpg" length="17800" type="image/jpeg"/>
</item>Code: Select all
<?php
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link' title='$entry->title' target='_blank'>" . $entry->title . "</a>
</li>";
}
echo "</ul>";
}
?>Code: Select all
<?php
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link' title='$entry->title' target='_blank'><img src='THE-IMAGE-HERE'>" . $entry->title . "</a>
</li>";
}
echo "</ul>";
}
?>