2012-09-20 2 views
3

내가 RSS 피드의 각 게시물에 대한 media:content에서 (다른 내용과 함께) 이미지를 검색하려고으로 : http://bits.blogs.nytimes.com/feed/미디어에서 URL을 가져 오기 : RSS의 XML 피드 콘텐츠를 jQuery를

내가 함께이 링크를 (시도 많은 다른 사람) jQuery XML parsing how to get element attribute 그러나 나는 아직도 오류를 얻고있다.

PHP 파일도 있습니다 (하단 참조).하지만 문제는 아닌 것 같습니다. 이 로그는 (내가 무엇을 잡기 위해 노력하고 여기에

TypeError: 'undefined' is not an object (evaluating '$item.find('media\\:content').attr('url').text')는 RSS 피드에서 XML의 예입니다 말하고있는 것입니다

나는 몇 가지 변화를 시도하고 난 <img src="undefined" alt="image">

와 함께 종료하고 is url = "this value") <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>

내 HTML은 jQuery Embeded입니다. 모든 솔루션, 조언 및 제안에 감사드립니다. 나는 지금 방책을 때리고있다. 여기

<!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() { 

// RSS Feed Var 
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> 

내 PHP

<?php 

    ob_start(); 

    $curl = curl_init($_GET['url']); 

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_HEADER, false); 

    $xml = curl_exec($curl); 
    header("Content-Type: text/xml"); 
    echo $xml; // Results 

    $output = ob_get_clean(); 

    curl_close($curl); 
    echo $output; 

    ob_end_flush(); 


?> 

답변

4

않습니다 :

var media = $item.find('media\\:content, content').attr('url'): 

편집

(한 다른 변화의 추가) :

var media = $item.find('media\\:content').attr('url').text(); 

이이어야한다

내가 변경 :

$(d).find('item').each(function() {

행 (경우

if (index >= 3) { return false }

을 그리고 추가 : 내가 추가

$(d).find('item').each(function(index) {

(세 개의 피드 항목을 제한하는) 빈 이미지로 설정된 이미지가 없습니다. GE) :

if (typeof mediatest === 'undefined') { var mediatest = 'blank.png' };

모든 것은 적어도 지금까지 ... 잘 작동합니다 :)

관련 문제