Quiero usar jQuery para analizar los canales RSS. ¿Se puede hacer con la biblioteca jQuery de base fuera de la caja o tendré que usar un plugin?
Respuestas
¿Demasiados anuncios?No hay necesidad de todo un plugin. Esto le devolverá la RSS como un objeto JSON a una función de devolución de llamada:
function parseRSS(url, callback) {
$.ajax({
url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url),
dataType: 'json',
success: function(data) {
callback(data.responseData.feed);
}
});
}
Uso jFeed - un plugin jQuery RSS / Atom. Según los documentos, es tan simple como:
jQuery.getFeed({
url: 'rss.xml',
success: function(feed) {
alert(feed.title);
}
});
Para aquellos de nosotros de venir a la discusión tarde, a partir de 1,5 jQuery se ha incorporado en las capacidades de análisis de XML, lo que hace que sea muy fácil de hacer esto sin plugins o servicios de 3 ª parte. Tiene una función parseXML, y también xml auto-parse al utilizar la función $ .get. Por ejemplo:
$.get(rssurl, function(data) {
var $xml = $(data);
$xml.find("item").each(function() {
var $this = $(this),
item = {
title: $this.find("title").text(),
link: $this.find("link").text(),
description: $this.find("description").text(),
pubDate: $this.find("pubDate").text(),
author: $this.find("author").text()
}
//Do something with item here...
});
});
jFeed no funciona en IE.
Utilice zRSSFeed . Si hubiera trabajo en 5 minutos
function getFeed(sender, uri) {
jQuery.getFeed({
url: 'proxy.php?url=' + uri,
success: function(feed) {
jQuery(sender).append('<h2>'
+ '<a href="'
+ feed.link
+ '">'
+ feed.title
+ '</a>'
+ '</h2>');
var html = '';
for(var i = 0; i < feed.items.length && i < 5; i++) {
var item = feed.items[i];
html += '<h3>'
+ '<a href="'
+ item.link
+ '">'
+ item.title
+ '</a>'
+ '</h3>';
html += '<div class="updated">'
+ item.updated
+ '</div>';
html += '<div>'
+ item.description
+ '</div>';
}
jQuery(sender).append(html);
}
});
}
<div id="getanewbrowser">
<script type="text/javascript">
getFeed($("#getanewbrowser"), 'http://feeds.feedburner.com/getanewbrowser')
</script>
</div>