DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Spring Boot and React in Harmony
  • How To Create a Resource Chart in JavaScript
  • TypeScript: Useful Features
  • Tracking Bugs Made Easy With React.js Bug Tracking Tools

Trending

  • Spring Authentication With MetaMask
  • How to Migrate Vector Data from PostgreSQL to MyScale
  • An Introduction to Build Servers and Continuous Integration
  • Information Security: AI Security Within the IoT Industry
  1. DZone
  2. Coding
  3. JavaScript
  4. Parsing RSS Feeds in JavaScript — Options

Parsing RSS Feeds in JavaScript — Options

Raymond Camden user avatar by
Raymond Camden
·
Dec. 15, 15 · Tutorial
Like (4)
Save
Tweet
Share
18.60K Views

Join the DZone community and get the full member experience.

Join For Free

for a while now i’ve used the google feed api to parse rss feeds in javascript. it did a good job of converting various rss flavors into a simple array of entries you could easily work with. unfortunately, google has deprecated the api and while it still worked the last time i used it, i would strongly recommend folks migrate their apps away from it as soon as possible. while this makes me sad, you have to move on.

2000px-sad_panda.svg

so what kind of options do you have?

parsing manually

so, remember that rss is just xml, and xml is just a string, and string parsing is easy, right? of course, there are 2 major flavors of rss, and multiple versions of both flavors, but if you’re just parsing one known rss feed then you can write to that particular flavor and version. (more about the different versions can be found on wikipedia .) unfortunately, if you try to simply xhr to an rss feed you’ll run into the lovely cross-origin browser doohicky that prevents you from making requests to another server. of course, if the rss is on the same domain, that isn’t a problem. and of course, if you are building something in apache cordova, then it isn’t a problem either. (just don’t forget to update the csp!) and finally, if you control the rss, you can add a cors header to it so modern browsers can use it. unfortunately, if none of those apply, you’re out of luck trying to do it completely client-side. (well, until we get to the next options!) let’s pretend that none of the roadblocks apply to you and look at a simple example. (as a quick note, none of my sample code will actually render anything. it will just get crap, parse it, and log data. i’m assuming folks know how to manipulate the dom. i’ve heard there’s a good library for that.)

$(document).ready(function() {
//feed to parse
var feed = "http://feeds.feedburner.com/raymondcamdensblog?format=xml";

$.ajax(feed, {
accepts:{
xml:"application/rss+xml"
},
datatype:"xml",
success:function(data) {
//credit: http://stackoverflow.com/questions/10943544/how-to-parse-an-rss-feed-using-javascript

$(data).find("item").each(function () { // or "item" or whatever suits your feed
var el = $(this);
console.log("------------------------");
console.log("title      : " + el.find("title").text());
console.log("link       : " + el.find("link").text());
console.log("description: " + el.find("description").text());
});


}
});

});

so, all i’m doing here is using jquery to request my rss feed. i then use jquery’s built-in xml parsing to iterate over the <item> blocks in my rss feed. as you can see, i’m using sample code from a stackoverflow answer that i modified a tiny bit. specifically, the answer iterated over <entry> blocks, not <item>. remember when i said there were different flavors of rss? that’s an example of the issue right there. if you must write code to handle both cases, you would need to look for <item> first and then <entry>. but that’s basically it. if your curious, i tested this in canary with –disable-web-security as a command line flag.

shot1

yql

remember yql (yahoo query language) ? the last time i blogged about it was way back in 2010 ( proof of concept 911 viewer ). as a gross simplification, yql acts like a “query language” for the web. you can literally run sql like content against urls and get formatted data out of it. they provide a powerful testing console and wouldn’t you know it, one of the examples is a rss parser:

shot2

just in case that screenshot is a bit too small, here is what the yql statement looks like:

select title from rss where url="http://rss.news.yahoo.com/rss/topstories"

i’ve got one word for that. bad ass! like, kitten in armor bad ass!

shot3

i tested with two different rss flavors and yql had no issue handling either. note the rest query url at the bottom. i copied that into a new file:

$(document).ready(function() {

var yql = "https://query.yahooapis.com/v1/public/yql?q=select%20title%2clink%2cdescription%20from%20rss%20where%20url%3d%22http%3a%2f%2ffeeds.feedburner.com%2fraymondcamdensblog%3fformat%3dxml%22&format=json&diagnostics=true&callback=";

$.getjson(yql, function(res) {
console.log(res);
}, "jsonp");

});

and, it worked like a charm. note the use of json/p to sidestep needing cors. and here is the result:

shot4

a big thank you to addy osmani from google for reminding me that yql was still around. google, i forgive you for killing the feed api now.

feednami

last but not least is a brand new service, feednami , created just in time for the death of the google feeds api. to use it you simply add a new script to your code and then use feednami.load() to get your feed. here is an example:

$(document).ready(function() {

var url = 'http://feeds.feedburner.com/raymondcamdensblog?format=xml';

feednami.load(url,function(result){
if(result.error) {
console.log(result.error);
} else {
var entries = result.feed.entries;
for(var i = 0; i < entries.length; i++){
var entry = entries[i];
console.dir(entry);
}
}
});

});

that's also pretty darn easy to use. here is the result:

shot5

summary

so, you've got options. which one is best? honestly, i don't know. yql requires the least amount of code from what i can see, but i kind of dig feednami's look a bit more. if you've used any of these in production, drop me a comment below with your thoughts!

JavaScript

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot and React in Harmony
  • How To Create a Resource Chart in JavaScript
  • TypeScript: Useful Features
  • Tracking Bugs Made Easy With React.js Bug Tracking Tools

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: