Reading an RSS Feed with jQuery - Unable to read <link> element
-
28-06-2021 - |
Question
I'm building a RSS feed of my website using jquery and the link element keeps returning blank. I do not have a problem with the javascript solution i built on my own but the jquery seems to hate the link element.
below is the jquery code:
function loadData(xml, ifid) {
var htmlStr;
var iframeToWrite = document.getElementById(ifid);
htmlStr = "<html><body>"
var items = $(xml).find("channel item").each(function () {
var $article = $(this);
var title = $article.find("title").text();
var description = $article.find("description").text();
var link = $article.find("link").text();
var pubDate = $article.find("pubDate").text();
htmlStr += "<div class='Rssitem'>\n";
htmlStr += "\t<h3><a href='" + link + "' target='_blank' >" +
title + "</a></h3>\n";
htmlStr += "\t<p>" + description + "</p>\n";
htmlStr += "\t<h6>" + pubDate + "</h6>\n";
htmlStr += "</div>\n"
});
htmlStr += "</body></hmtl>";
iframeToWrite.contentDocument.write(htmlStr);
}
below is a sample xml i edited from the npr stream I get:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:npr="http://www.npr.org/rss/" xmlns:nprml="http://api.npr.org/nprml" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>News</title>
<link>http://www.npr.org/templates/story/story.php?storyId=1001&ft=1&f=1001</link>
<description>NPR news, audio, and podcasts. Coverage of breaking stories, national and world news, politics, business, science, technology, and extended coverage of major national and world events.</description>
<language>en</language>
<copyright>Copyright 2012 NPR - For Personal Use Only</copyright>
<generator>NPR API RSS Generator 0.94</generator>
<lastBuildDate>Tue, 28 Aug 2012 12:19:00 -0400</lastBuildDate>
<image>
<url>http://media.npr.org/images/npr_news_123x20.gif</url>
<title>News</title>
<link>http://www.npr.org/templates/story/story.php?storyId=1001&ft=1&f=1001</link>
</image>
<item>
<title>Reports: Obama Administration Will Unveil New Fuel-Efficiency Standards</title>
<description>The new rules will require U.S. cars to average 54.5 miles per gallon by 2025.</description>
<pubDate>Tue, 28 Aug 2012 12:19:00 -0400</pubDate>
<link>http://www.npr.org/blogs/thetwo-way/2012/08/28/160172356/reports-obama-administration-will-unveil-new-fuel-efficiency-standards?ft=1&f=1001</link>
<guid>http://www.npr.org/blogs/thetwo-way/2012/08/28/160172356/reports-obama-administration-will-unveil-new-fuel-efficiency-standards?ft=1&f=1001</guid>
<content:encoded><![CDATA[<p>The new rules will require U.S. cars to average 54.5 miles per gallon by 2025.</p><p><a href="http://www.npr.org/templates/email/emailAFriend.php?storyId=160172356">» E-Mail This</a> <a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.npr.org%2Ftemplates%2Fstory%2Fstory.php%3FstoryId%3D160172356">» Add to Del.icio.us</a></p>]]></content:encoded>
</item>
</channel>
</rss>
Solution
You have to parse your xml string into a xml document first jQuery.parseXML().
To quote the documentation:
jQuery.parseXML uses the native parsing function of the browser to create a valid XML Document. This document can then be passed to jQuery to create a typical jQuery object that can be traversed and manipulated.
If you don't use parseXML and wrap a jQuery selector around it, jQuery may not be able to interpet all the nodes properly.
In your example the <link>
tags are not inerpeted correctly and the </link>
closing tags have been removed without parsing, hence you cannot query the link text correctly.
You can confirm that if you do a console.log($(this))
and check the content in the console output. You can see the <link>
then text but the closing </link>
is missing.
However, when parsing your xml first to a document object you can now wrap a jQuery selector around it and access all nodes reliably. Off course, assuming the inital XML string was valid XML.
DEMO - Using parseXml on your xml string and querying it after
Applying this to your code would look similar to this:
htmlStr = "<html><body>";
// Parse the XML to a document object and then wrap it into a jQuery selector
var $xml = $($.parseXML(xml));
var items = $xml.find("channel item").each(function() {
var $article = $(this);
//..... rest of your code as is