html - How to parse an RSS feed using JavaScript? -
i need parse rss feed (xml version 2.0) , display parsed details in html page.
parsing feed
with jquery's jfeed
(don't recommend one, see other options.)
jquery.getfeed({ url : feed_url, success : function (feed) { console.log(feed.title); // more stuff here } });
with jquery's built-in xml support
$.get(feed_url, function (data) { $(data).find("entry").each(function () { // or "item" or whatever suits feed var el = $(this); console.log("------------------------"); console.log("title : " + el.find("title").text()); console.log("author : " + el.find("author").text()); console.log("description: " + el.find("description").text()); }); });
with jquery , google ajax feed api
$.ajax({ url : document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeuricomponent(feed_url), datatype : 'json', success : function (data) { if (data.responsedata.feed && data.responsedata.feed.entries) { $.each(data.responsedata.feed.entries, function (i, e) { console.log("------------------------"); console.log("title : " + e.title); console.log("author : " + e.author); console.log("description: " + e.description); }); } } });
but means you're relient on them being online , reachable.
building content
once you've extracted information need feed, create documentfragment
s (with document.createdocumentfragment()
containing elements (created document.createelement()
) you'll want inject display data.
injecting content
select container element want on page , append document fragments it, , use innerhtml replace content entirely.
something like:
$('#rss-viewer').append(adocumentfragmententry);
or:
$('#rss-viewer')[0].innerhtml = adocumentfragmentofallentries.innerhtml;
test data
using question's feed, of writing gives:
<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/atom" xmlns:creativecommons="http://backend.userland.com/creativecommonsrssmodule" xmlns:re="http://purl.org/atompub/rank/1.0"> <title type="text">how parse rss feed using javascript? - stack overflow</title> <link rel="self" href="https://stackoverflow.com/feeds/question/10943544" type="application/atom+xml" /> <link rel="hub" href="http://pubsubhubbub.appspot.com/" /> <link rel="alternate" href="https://stackoverflow.com/q/10943544" type="text/html" /> <subtitle>most recent 30 stackoverflow.com</subtitle> <updated>2012-06-08t06:36:47z</updated> <id>https://stackoverflow.com/feeds/question/10943544</id> <creativecommons:license>http://www.creativecommons.org/licenses/by-sa/3.0/rdf</creativecommons:license> <entry> <id>https://stackoverflow.com/q/10943544</id> <re:rank scheme="http://stackoverflow.com">2</re:rank> <title type="text">how parse rss feed using javascript?</title> <category scheme="https://stackoverflow.com/feeds/question/10943544/tags" term="javascript"/><category scheme="https://stackoverflow.com/feeds/question/10943544/tags" term="html5"/><category scheme="https://stackoverflow.com/feeds/question/10943544/tags" term="jquery-mobile"/> <author> <name>thiru</name> <uri>https://stackoverflow.com/users/1126255</uri> </author> <link rel="alternate" href="https://stackoverflow.com/questions/10943544/how-to-parse-a-rss-feed-using-javascript" /> <published>2012-06-08t05:34:16z</published> <updated>2012-06-08t06:35:22z</updated> <summary type="html"> <p>i need parse rss-feed(xml version2.0) using xml , want display parsed detail in html page, tried in many ways. not working. system running under proxy, since new field, don't know whether possible or not. if 1 knows please me on this. in advance.</p> </summary> </entry> <entry> <id>https://stackoverflow.com/questions/10943544/-/10943610#10943610</id> <re:rank scheme="http://stackoverflow.com">1</re:rank> <title type="text">answer haylem how parse rss feed using javascript?</title> <author> <name>haylem</name> <uri>https://stackoverflow.com/users/453590</uri> </author> <link rel="alternate" href="https://stackoverflow.com/questions/10943544/how-to-parse-a-rss-feed-using-javascript/10943610#10943610" /> <published>2012-06-08t05:43:24z</published> <updated>2012-06-08t06:35:22z</updated> <summary type="html"><h1>parsing feed</h1> <h3>with jquery's jfeed</h3> <p>try this, <a href="http://plugins.jquery.com/project/jfeed" rel="nofollow">jfeed</a> <a href="http://www.jquery.com/" rel="nofollow">jquery</a> plug-in</p> <pre><code>jquery.getfeed({ url : feed_url, success : function (feed) { console.log(feed.title); // more stuff here } }); </code></pre> <h3>with jquery's built-in xml support</h3> <pre><code>$.get(feed_url, function (data) { $(data).find("entry").each(function () { // or "item" or whatever suits feed var el = $(this); console.log("------------------------"); console.log("title : " + el.find("title").text()); console.log("author : " + el.find("author").text()); console.log("description: " + el.find("description").text()); }); }); </code></pre> <h3>with jquery , google ajax apis</h3> <p>otherwise, <a href="https://developers.google.com/feed/" rel="nofollow">google's ajax feed api</a> allows feed json object:</p> <pre><code>$.ajax({ url : document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&amp;num=10&amp;callback=?&amp;q=' + encodeuricomponent(feed_url), datatype : 'json', success : function (data) { if (data.responsedata.feed &amp;&amp; data.responsedata.feed.entries) { $.each(data.responsedata.feed.entries, function (i, e) { console.log("------------------------"); console.log("title : " + e.title); console.log("author : " + e.author); console.log("description: " + e.description); }); } } }); </code></pre> <p>but means you're relient on them being online , reachable.</p> <hr> <h1>building content</h1> <p>once you've extracted information need feed, need create document fragments containing elements you'll want inject display data.</p> <hr> <h1>injecting content</h1> <p>select container element want on page , append document fragments it, , use innerhtml replace content entirely.</p> </summary> </entry></feed>
executions
using jquery's built-in xml support
invoking:
$.get('https://stackoverflow.com/feeds/question/10943544', function (data) { $(data).find("entry").each(function () { // or "item" or whatever suits feed var el = $(this); console.log("------------------------"); console.log("title : " + el.find("title").text()); console.log("author : " + el.find("author").text()); console.log("description: " + el.find("description").text()); }); });
prints out:
------------------------ title : how parse rss feed using javascript? author : thiru https://stackoverflow.com/users/1126255 description: ------------------------ title : answer haylem how parse rss feed using javascript? author : haylem https://stackoverflow.com/users/453590 description:
using jquery , google ajax apis
invoking:
$.ajax({ url : document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeuricomponent('https://stackoverflow.com/feeds/question/10943544'), datatype : 'json', success : function (data) { if (data.responsedata.feed && data.responsedata.feed.entries) { $.each(data.responsedata.feed.entries, function (i, e) { console.log("------------------------"); console.log("title : " + e.title); console.log("author : " + e.author); console.log("description: " + e.description); }); } } });
prints out:
------------------------ title : how parse rss feed using javascript? author : thiru description: undefined ------------------------ title : answer haylem how parse rss feed using javascript? author : haylem description: undefined
Comments
Post a Comment