PDA

View Full Version : XML namespaces and SimpleXML



JBottero
02-16-2009, 08:21 PM
I'm using PHP's simple XML functions to parse RSS feeds.

Here's a sample of a particular feed (Yahoo News):
<item>
<title>Plane that crashed near Buffalo was on autopilot
(AP)
</title>
<link>http://us.rd.yahoo.com/dailynews/rss/topstories/*http://news.yahoo.com/s/ap/20090216/ap_on_re_us/plane_into_home</link>

<guid isPermaLink="false">ap/20090216/plane_into_home</guid>
<source>AP</source>
<pubDate>Mon, 16 Feb 2009 01:24:34 GMT</pubDate>
<description><p><a href="http://us.rd.yahoo.com/dailynews/rss/topstories/*http://news.yahoo.com/s/ap/20090216/ap_on_re_us/plane_into_home"><img src="http://d.yimg.com/img.news.yahoo.com/util/anysize/130,http%3A%2F%2Fd.yimg.com%2Fa%2Fp%2Fap%2F20090215%2Fcapt.af477835fefa4943beb124233d37e568.plane_into_home_nyrb104.jpg?v=2" align="left" height="93" width="130" alt="Women leave flowers at a makeshift memorial to the victims of Continental Connection Flight 3407, at the Clarence Center United Methodist Church, Sunday, Feb. 15, 2009 in Clarence, N.Y. (AP Photo/Robert F. Bukaty)" border="0" /></a>AP - The commuter plane that crashed near Buffalo was on autopilot until just before it went down in icy weather, indicating that the pilot may have ignored federal safety recommendations and violated the airline's own policy for flying in such conditions, an investigator said Sunday.</p><br clear="all"/></description>
<media:content url="http://d.yimg.com/img.news.yahoo.com/util/anysize/130,http%3A%2F%2Fd.yimg.com%2Fa%2Fp%2Fap%2F20090215%2Fcapt.af477835fefa4943beb124233d37e568.plane_into_home_nyrb104.jpg?v=2" type="image/jpeg" height="93" width="130"/>
<media:text type="html"><p><a href="http://us.rd.yahoo.com/dailynews/rss/topstories/*http://news.yahoo.com/s/ap/20090216/ap_on_re_us/plane_into_home"><img src="http://d.yimg.com/img.news.yahoo.com/util/anysize/130,http%3A%2F%2Fd.yimg.com%2Fa%2Fp%2Fap%2F20090215%2Fcapt.af477835fefa4943beb124233d37e568.plane_into_home_nyrb104.jpg?v=2" align="left" height="93" width="130" alt="photo" title="Women leave flowers at a makeshift memorial to the victims of Continental Connection Flight 3407, at the Clarence Center United Methodist Church, Sunday, Feb. 15, 2009 in Clarence, N.Y. (AP Photo/Robert F. Bukaty)" border="0"/></a></p><br clear="all"/></media:text>

<media:credit role="publishing company">(AP)</media:credit>
</item>

I'm having a hard time figuring out how to access the media:content (the link to the associated image).

<?php

$xml = simplexml_load_file('http://rss.news.yahoo.com/rss/topstories');

echo $xml->item->children("http://search.yahoo.com/mrss/")->credit;

?>Gets me nothing...

Any ideas?

Nile
02-16-2009, 08:25 PM
What about:


<?php
$xml = new SimpleXMLElement("http://finance.yahoo.com/rss/topstories", null, true);
echo $xml->item->media:credit;
?>

I'm new to SimpleXML, but give it a try.

(I didn't have time to do a highlight, sorry)

JBottero
02-16-2009, 10:28 PM
What about:


<?php
$xml = new SimpleXMLElement("http://finance.yahoo.com/rss/topstories", null, true);
echo $xml->item->media:credit;
?>

I'm new to SimpleXML, but give it a try.

(I didn't have time to do a highlight, sorry)Tried it. Nope, that doesn't get me into the media namespace. But thanks.