Intermediate: RSS Feeds 2

In today's world time is a highly valued commodity. With busy lifestyles and demanding jobs people rarely have time to sit down daily to read their favourite newspaper or website. This has led to a culture in which people demand information immediately and this, in turn, has helped lead to the popularity of RSS feeds.

What is RSS?

Really Simple Syndication, more commonly known by it's acronym RSS, is a format used to send web feeds. Written in XML, RSS feeds are easy to parse and can be searched quickly for items of interest. They are generally used to send summaries of news articles or web forum posts which can be displayed to the user in an easy to read format, providing them with the quick overview they desire and a link they can click if they want to read more.

RSS and LiveCode

RSS feeds can be used within LiveCode using the revXML library. This provides all the functionality required to parse XML files and makes using RSS feeds incredibly easy. The library has functions to create XML trees and retrieve branches and leaves from within it.

Below is a simple explanation of how to create a stack which retrieves and displays RSS feeds. To find out more about revXML please refer to the XML section of the current User Guide.

The RSS Feed

The RSS Feed

When creating an RSS stack the first thing you need to decide is what your subject matter is going to be. Many websites now provide RSS feeds, from BBC News to our very own LiveCode User Forums, so there is a wealth of topics to choose from. For the purpose of this tutorial I have decided to use the slashdot feed.

The RSS Stack

The RSS Stack

Once you know where you are getting your feed from you must decide how often you are going to retrieve it. Some websites update their RSS Feeds every hour while others only update once a day. You can create a function which retrieves the RSS feed within specified time periods or, as in this tutorial, you can create a button which will retrieve the feed when pressed. The best method to take will depend on the update rate of the feed itself and when you expect people to be reading the results you retrieve.

Once you have decided on the content of your stack and how regularly it is to be updated you must make the stack itself. At a basic level this only requires a few objects; a field to display the results and, in the case of this tutorial, a button to retrieve the feed.

RSS and RevXML - Creating the XML tree in memory

When the button is pressed the webpage containing the RSS feed is retrieved using a url request. An XML tree is then created from this using the revXML library command:

revCreateXMLTree(tWebPage, false, true, false)

The first argument of this is a variable containing the actual RSS feed. The second specifies whether the data being parsed must be "well-formed". If this is set to true and bad data is found the function will stop executing and an error will be thrown. The third argument specifies whether or not to create an XML tree in memory and the last whether revXML messages should be sent while the data is being processed. If both of these arguments are false the function will act as an XML validator. Generally, when a tree is not being built in memory, messages should be sent. This will allow you to deal with the XML data as you come across it, as you will be notified when a child node, for example, has been found.

RSS and RevXML - Parsing the XML tree for articles

As RSS feeds are relatively small I have made a tree in memory. This tree is searched for child nodes using revXMLChildNames.

This takes as arguments the id of the xml tree in memory, the name of the node whose children you want, a string delimiter which separates the children, the name of the child you're interested in and a boolean specifying whether you want to know the number of children each child has. Generally in RSS the items which interest you will be contained within tags. From looking at the RSS feed returned from slashdot I was able to identify that the direct parent of these item tags was (which is a tag used to represent the Resource Description Framework). Therefore my revXML call takes the form:

revXMLChildNames(tTreeID, "RDF", return, "item", true)

RSS and RevXML - Building the list of articles

RSS and RevXML - Building the list of articles

Once the child nodes have been found they are placed in a list which is further broken down using revXMLNodeContents. This retrieves any text contained in a node and is carried out on one item from the RSS feed at a time. The text which is found is then arranged in a more human readable format and displayed to the user. This can be done in multiple ways but for this tutorial the data was formatted as html and placed into the results field using the htmlText property.

Customization

Now you know the basics of RSS and LiveCode try changing the value of the sWebPage variable to retrieve feeds from other sources. You could even create a widget to sit on your desktop or, if you are feeling super confident, you could try retrieving world news from multiple sources, and clustering these to find similar stories.

9 Comments

Maggie

I installed the Rev and it's not working. Says, "xmlerr, can't find element". I don't see any reference in the rev to slashdot either.

Elanor Buchanan

Hi Maggie

The lesson and attached file have been updated to fix a couple of issues. One related to changes to to Rev/Livecode and one because the path to the Slashdot RSS feed had changed since the lesson was first written.

If you look at the top of the card script you can see the path to Slashdot is stored in a variable.

local sWebPage = "http://rss.slashdot.org/Slashdot/slashdot"

I hope that helps.

Elanor

Dave

Hi there, I am trying to load an RSS feed form wordpress
which does not sem to be in the same format as slashdot.

No mention of RDF and Im getting "ERROR: Tree not made"

newbie.

Hanson Schmidt-Cornelius

Hi Dave,

wordpress supports the same feed types as are supported by slashdot. You should be able to access the feeds from wordpress from LiveCode too.

Kind Regards,

Hanson

Glenn

Elanor, I cannot seem to locate that variable you're talking about in your explanation on May 9. Could you please explain a bit more where I find it.

Hanson Schmidt-Cornelius

Hi Glenn,

you can access the variable in the sample stack RSS.livecode, which is attached to this lesson. Follow the link under "Attached Files".

Once you have loaded the stack into LiveCode, select "Object->Card Script" from the menu bar. This opens the script editor of the card script with "sWebPage" declared on the first line of the script.

Kind Regards,

Hanson

Jordan

Complete beginner here. I can't for the life of me figure out how to get other RSS feeds to display like the Slashdot example above.

I changed my sWebPage and sItemPath to reflect the different RSS URL's and to RSS/Channel (I read elsewhere that this is necessarily to display the Slashdot feed).

I can get Slashdot RSS feeds to load not anything else. On that note when I click 'Receive Articles' it appears as though the RSS feeds I am attempting to secure in memory do load (the scrolling bar becomes moveable) but they do not translate into readable HTML...help please!

--Jordan

Elanor Buchanan

Hi Jordan

I tried changing sWebPage to look at a Guardian RSS feed

local sWebPage = "http://feeds.pinboard.in/rss/u:guardiantech/"

and it worked ok. If you let me know URL you are trying I can look at it here. Another option is to use the debugger and variable watcher to step through the code and see where the problem is occurring. If you haven't used the debugger before you can read about it in chapter 3.5 of the User Guide.

Kind regards

Elanor

Mark Christie

When using with WordPress I didn't realise there were a number of RSS feed types which WordPress supports. The default suggested feed which is your blog name with /feed appended to the end of your URL doesn't work with the RDF sample in this LiveCode sample, but by simply replacing /feed with /rdf it does. Simple and nice.

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.