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
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
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
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.
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.