Results 1 to 3 of 3

Thread: XML namespaces and SimpleXML

  1. #1
    Join Date
    Mar 2006
    Posts
    41
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default XML namespaces and SimpleXML

    I'm using PHP's simple XML functions to parse RSS feeds.

    Here's a sample of a particular feed (Yahoo News):
    Code:
    <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).
    Code:
    <?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?

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    What about:
    Code:
    <?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)
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2006
    Posts
    41
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    What about:
    Code:
    <?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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •