Page 1 of 1

How to parse an RSS feed using JavaScript in QuarkXPress (JQuery example)

Posted: 23 Jul 2018, 01:47
by Sarbjit Singh
This is our first example that uses the JQuery library to read an RSS feed. It logs the results in the JavaScript Debugger Palette's console.

The intent is to show how you can use any web resource using native JavaScript, or a third party JS library. You may extend this example further to populate text boxes on a layout, with content fetched from the RSS feed. We are using the Quark Forum RSS in this example. However, you may use any RSS URL and change the appropriate tags that you need to read from the feed.

Feel free to ask questions.

Here is the code example with detailed comments:

You may download the snippet here:
https://pastebin.com/3bsx3Snq
OR here:
https://github.com/qxpjs/examples

//Import jQuery Library from the CDN, you don't even need to download it!
app.importScript("https://code.jquery.com/jquery-3.3.1.js");

(function () {

//Specify the RSS Feed Link
const rssFeed = "http://forums.quark.com/feed.php";
//Check the feed to know which Tag is used for each entry, specify the correct Tag here
const rssPostTag = "entry";
//Create an array of Article Tags you want to fetch from the feed
const keysToRead = ["title", "author", "id"];

//Call the function to read the feed
readRSS();

//*****************====================================Functions used in the JavaScript===============================****************//

function readRSS() {
let articleList = []; //Create an array to store articles
let article; //variable to store each article
let promise = new Promise(function (resolve, reject)//promise is used to ensure this task completes and returns a promise followed by further execution
{
//Load the RSS Feed XML using JQuery's "get" method
$.get(rssFeed, function (data) {
//Parse all the "entries" in XML
$(data).find(rssPostTag).each(function () {
var el = $(this);
//Create an object to store articles
article = new Object();
//Loop through all the required Tags and store them in the object
for (let i = 0; i < keysToRead.length; i++) {
article[keysToRead] = el.find(keysToRead).text();
}

//Push to the list
articleList.push(article);
//Print the Element on the JavaScript Debugger console
console.log(article);
});

console.log("Found " + articleList.length + " articles in the feed.");
});

// resolve(articleList);
Promise.resolve().then(resolve(articleList));
});
return promise;
}

})();

Re: How to parse an RSS feed using JavaScript in QuarkXPress (JQuery example)

Posted: 23 Jul 2018, 04:42
by Matthias Guenther (Quark)
Great script, Sarbjit.

And congratulations to your 400th posts, despite Facebook taking over ;-)

Regards
Matthias