Get image URL from media:content in RSS XML feed with jQuery

Discussion in 'jQuery' started by commandz, Sep 20, 2012.

  1. #1
    I am trying to retrieve the images (along with other content) from media:content for each post in a RSS feed (doesn't have to be this one): http://bits.blogs.nytimes.com/feed/

    Here is an example of the XML from the RSS feed (I am trying to grab what is in url="this value")
    [COLOR=#000000][FONT=Consolas]<media:content url="http://graphics8.nytimes.com/images/2011/11/30/technology/bits-daily-report/bits-daily-report-thumbStandard.jpg" medium="image" width="75" height="75"></media:content>[/FONT][/COLOR]
    Code (markup):


    I have tried several variations and I am ending up with
    [COLOR=#000000][FONT=Consolas]<img src="undefined" alt="image">[/FONT][/COLOR]
    Code (markup):


    This is what the log is saying
    [COLOR=#000000][FONT=Consolas]TypeError: 'undefined' is not an object (evaluating '$item.find('media\\:content').attr('url').text')[/FONT][/COLOR]
    Code (markup):


    There is a PHP file as well (see at bottom) but I don't think that it is the issue.

    I tried this link (along with many others) <http://stackoverflow.com/questions/4825920/jquery-xml-parsing-how-to-get-element-attribute> but I am still getting errors.

    Here is the HTML with jQuery Embeded. Thank you for all ideas, pointers, and suggestions.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    
        <title>Your Site Title</title>
    
    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        
        <script type="text/javascript">
        $(document).ready(function(){
    
    
    function get_rss_feed() {
            
        // Using CNN's RSS Feed as an example
        var rssUrl = 'http://bits.blogs.nytimes.com/feed/';
            
        //clear the content in the div for the next feed.
        $("#feedContent").empty();
                
        //use the JQuery get to grab the URL from the selected item, put the results in to an argument for parsing in the inline function called when the feed retrieval is complete
        $.get('rss.php?url='+rssUrl, function(d) {
                    
            //find each 'item' in the file and parse it
            $(d).find('item').each(function() {
    
    
                //name the current found item this for this particular loop run
                var $item = $(this);
                // grab the post title
                var title = $item.find('title').text();
                // grab the post's URL
                var link = $item.find('link').text();
                // next, the description
                var description = $item.find('description').text();
                //don't forget the pubdate
                var pubDate = $item.find('pubDate').text();
                var media = $item.find('media\\:content').attr('url').text();
                        
                    // now create a var 'html' to store the markup we're using to output the feed to the browser window
                var html = "<div class=\"entry\"><h2 class=\"postTitle\">" + title + "<\/h2>";
                html += "<em class=\"date\">" + pubDate + "</em>";
                html += "<p class=\"description\">" + description + "</p>";
                html += "<img src=\"" + media + "\" alt=\"image\"/>";
                html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a><\/div>";
                        
                //Send Results to a div
                $('#rss').append($(html));  
            });
        });
                
    };
    get_rss_feed();
    
    
          });
        </script>
    </head>
    
    
    <body>
    <h1>RSS TEST</h1>
    <!-- Your RSS DIV -->
    <div id="rss"></div>
    
    
    </body>
    
    </html>
    Code (markup):


    Here is the PHP

    [COLOR=#000000][FONT=Consolas]<?[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]php[/FONT][/COLOR]
    
    
      ob_start();
    
      $curl = curl_init($_GET[[COLOR=#800000]'url'[/COLOR]]);
    
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, [COLOR=#00008B]true[/COLOR]);
      curl_setopt($curl, CURLOPT_HEADER, [COLOR=#00008B]false[/COLOR]);
    
      $xml = curl_exec($curl);  
      header([COLOR=#800000]"Content-Type: text/xml"[/COLOR]);
      echo $xml; [COLOR=gray]// Results[/COLOR]
    
      $output = ob_get_clean();
    
      curl_close($curl);
      echo $output;
    
      ob_end_flush();
    
    
    [COLOR=#000000][FONT=Consolas]?>[/FONT][/COLOR]
    Code (markup):

     
    commandz, Sep 20, 2012 IP