Reading+RSS+Feeds

//by Tony Tooth and Richard Russell, Revised July 2009//

There is a rapidly growing body of raw data available from certain sites, in the form of so-called [|RSS Feeds] which can be downloaded and used within a BB4W application. **RSS** (**R**eally **S**imple **S**yndication) conveniently packages the data in an [|XML wrapper].

Typically there are two steps required to use RSS data in your own program:
 * Download the RSS data from the internet to a local disk file.
 * Extract the wanted information by parsing the contents of the XML file.

Downloading the RSS data can easily be achieved by calling the function **PROCurldownload** which can be found here (there are two versions of the procedure listed in that article; the second, longer, version may be more reliable). Extracting the wanted information from the file can be achieved by calling routines within the XMLLIB library.

RSS data is **hierarchical**, and the structure of the data is reflected in the structure of the XML file. If you want to examine what data is available in a particular RSS feed you can adapt the program **xml2tree** (available [|here]) by changing the URL to that of the feed you are interested in; the program displays the data as a **Tree View**.

Here is a typical example of the hierarchy of the parts of an RSS feed in which you are most likely to be interested:

code format="xml" code To illustrate the process of downloading, extracting and using the data two examples follow; both make use of RSS feeds provided by the BBC.

Weather example
The code below displays a summary of the weather forecast for the next three days (if you happen to live in Buckingham!):

code format="bb4w" INSTALL @lib$+"XMLLIB" url$ = "http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/2688.xml" XMLfile$ = @tmp$+"temp.xml" PROCurldownload(url$, XMLfile$) PROC_initXML(xml{}, XMLfile$) rss% = FN_skipTo(xml{}, "rss", 0) IF rss% THEN channel% = FN_skipTo(xml{}, "channel", rss%) IF channel% THEN IF FN_skipTo(xml{}, "title", channel%) THEN PRINT FN_repEnt(FN_nextToken(xml{})) ENDIF WHILE FN_skipTo(xml{}, "item", channel%) item% = FN_getLevel(xml{}) IF FN_skipTo(xml{}, "title", item%) THEN PRINT 'FN_repEnt(FN_nextToken(xml{})) ENDIF ENDWHILE ENDIF ENDIF PROC_exitXML(xml{}) END code If you want a more detailed forecast you can include the 'description' data as well as the 'title':

code format="bb4w" INSTALL @lib$+"XMLLIB" url$ = "http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/2688.xml" XMLfile$ = @tmp$+"temp.xml" PROCurldownload(url$, XMLfile$) PROC_initXML(xml{}, XMLfile$) rss% = FN_skipTo(xml{}, "rss", 0) IF rss% THEN channel% = FN_skipTo(xml{}, "channel", rss%) IF channel% THEN IF FN_skipTo(xml{}, "title", channel%) THEN PRINT FN_repEnt(FN_nextToken(xml{})) ENDIF WHILE FN_skipTo(xml{}, "item", channel%) item% = FN_getLevel(xml{}) IF FN_skipTo(xml{}, "title", item%) THEN PRINT 'FN_repEnt(FN_nextToken(xml{})) ENDIF IF FN_skipTo(xml{}, "description", item%) THEN PRINT FN_repEnt(FN_nextToken(xml{})) ENDIF ENDWHILE ENDIF ENDIF PROC_exitXML(xml{}) END code

News example
The code below lists the news headlines:

code format="bb4w" INSTALL @lib$+"XMLLIB" url$ = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml" XMLfile$ = @tmp$+"temp.xml" PROCurldownload(url$, XMLfile$) VDU 14 PROC_initXML(xml{}, XMLfile$) rss% = FN_skipTo(xml{}, "rss", 0) IF rss% THEN channel% = FN_skipTo(xml{}, "channel", rss%) IF channel% THEN WHILE FN_skipTo(xml{}, "item", channel%) item% = FN_getLevel(xml{}) IF FN_skipTo(xml{}, "description", item%) THEN PRINT FN_repEnt(FN_nextToken(xml{}))' ENDIF ENDWHILE ENDIF ENDIF PROC_exitXML(xml{}) END code VDU 14 is used to pause the display at the end of each page until **Shift** is pressed.