Table of Contents
24.12.1 RSS feed Projects
The gb.web.feed component - WebFeed for short - complies with the RSS 2.0 specification and maps the RSS specification 1:1. This means that
- only RSS feeds that comply with the specification are parsed without errors and
- valid RSS feeds are only generated if the specification is followed!
24.12.1.1 Project 1
The first project creates an empty RSS feed:
Public hRSS As Rss Private Function WriteFeed() As String Dim hXML As String Dim hRSSItem As RssItem '-- Es wird ein neues RSS-Objekt erzeugt hRSS = New Rss '-- Der Feed wird gerendert → XML-Format hXML = hRSS.ToString() Return hXML End
This is the content of hXML - as you can see, it is a valid RSS document:
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"> <channel> <title /> <link /> <description /> </channel></rss>
24.12.1.2 Projekt 2
The second project inserts two entries into the above template in the channel. Then the XML document is saved (temporarily) in the file rss.xml. Afterwards, the file can be loaded, the content read in and parsed, and selected data output:
' Gambas class file Public hRSS As Rss Public Sub Form_Open() FMain.Caption = "Schreiben und Lesen eines RSS-Feeds → Datei ~/rss.xml" End Public Sub btnWriteFeed_Click() WriteFeed() txaOutput.Insert(File.Load(Temp("rss.xml"))) End Public Sub btnReadFeed_Click() ' '-- Der RSS-Feed wird aus einer XML-Datei geladen - wenn sie existiert If Not Exist(Temp("rss.xml")) Then Return ReadFeed(File.Load(Temp("rss.xml"))) txaOutput.SetFocus() txaOutput.Pos = txaOutput.Length End Public Sub btnClear_Click() txaOutput.Clear() End Private Function WriteFeed() As String Dim hXML As String Dim hRSSItem As RssItem '-- Es wird ein neues RSS-Objekt erzeugt hRSS = New Rss '-- Ausgewählte Channel-Eigenschaften werden festgelegt hRSS.Title = "CHANNEL 1 - TITEL" hRSS.Link = "CHANNEL 1 - LINK: www.channel.net" hRSS.Description = "CHANNEL 1 - DESCRIPTION" hRSS.Pub = New RssDate(DateAdd(Now, gb.Hour, 0)) '-- Es wird ein neuer Eintrag 1 erzeugt hRSSItem = New RssItem '-- Ausgewählte Item-Eigenschaften werden festgelegt hRSSItem.Title = "ITEM 1 - TITEL" hRSSItem.Link = "ITEM 1 - LINK 1: www.example.com/item_1" hRSSItem.Description = "ITEM 1 - DESCRIPTION" hRSSItem.Pub = New RssDate '-- Der Eintrag 1 wird in den Feed eingefügt hRSS.Add(hRSSItem) '-- Es wird ein neuer Eintrag 2 erzeugt hRSSItem = New RssItem '-- Ausgewählte Item-Eigenschaften werden festgelegt hRSSItem.Title = "ITEM 2 - TITEL" hRSSItem.Link = "ITEM 2 - LINK 2: www.example.com/item_2" hRSSItem.Description = "ITEM 2 - DESCRIPTION" hRSSItem.Pub = New RssDate '-- Der Eintrag 2 wird in den Feed eingefügt hRSS.Add(hRSSItem) '-- Der Feed wird gerendert -> XML-Format hXML = hRSS.ToString() '-- Der RSS-Feed wird in einer (temporären) XML-Datei gespeichert File.Save(Temp("rss.xml"), hXML) Return hXML End Private Function ReadFeed(argData As String) Dim hXML As String Dim iRSSCount As Integer Dim hRSSItem As RssItem '-- Der RSS-Feed wird aus einer XML-Datei geladen - wenn sie existiert If Not Exist(Temp("rss.xml")) Then Return hXML = File.Load(Temp("rss.xml")) '-- Es wird ein neues RSS-Objekt erzeugt hRSS = New Rss hRSS.FromString(hXML) '-- Ausgewählte Channel-Eigenschaften werden ausgelesen *und* angezeigt If hRSS.Title Then txaOutput.Insert(hRSS.Title & gb.NewLine) If hRSS.Link Then txaOutput.Insert(hRSS.Link & gb.NewLine) If hRSS.Description Then txaOutput.Insert(hRSS.Description & gb.NewLine) If hRSS.Pub Then txaOutput.Insert(Format(hRSS.Pub.Date, "dddd, dd.mm.yyyy hh:nn") & gb.Lf & gb.Lf) '-- Ausgewählte Item-Eigenschaften werden ausgelesen *und* angezeigt For Each hRSSItem In hRSS If hRSSItem.Title Then txaOutput.Insert(hRSSItem.Title & gb.NewLine) If hRSSItem.Link Then txaOutput.Insert(hRSSItem.Link & gb.NewLine) If hRSSItem.Description Then txaOutput.Insert(hRSSItem.Description & gb.NewLine) Inc iRSSCount If hRSSItem.Pub Then If iRSSCount < hRSS.Count Then txaOutput.Insert(Format(hRSSItem.Pub.Date, "dddd, dd.mm.yyyy hh:nn") & gb.NewLine & gb.NewLine) Else txaOutput.Insert(Format(hRSSItem.Pub.Date, "dddd, dd.mm.yyyy hh:nn") & gb.NewLine) Endif Endif Next End
Abbildung 24.12.1.1: Inhalt RSS-Feed
Figure 24.12.1.2: Display of selected data in the RSS feed
The source code of project 2 can be found in the download area.
24.12.1.3 Project 3
Project 3 is based on project 2. The extension consists of displaying selected data in a ColumnView. The ListContainer component previously used for such use cases is considered obsolete and should no longer be used for current projects. After clicking on a row in the ColumnView, the web page whose URL is stored in the RSSItem.Link property is displayed in a WebView.
Notes:
- The selected RSS feed contains proprietary XML tags in their own namespaces that do not conform to the RSS 2.0 specification. This generates errors during parsing when using the gb.web.feed component.
- To show you Project 3 with the special way of presenting data in a ColumnView and in a WebView, an original RSS feed was shortened, all proprietary XML tags removed and the feed saved in an XML file. Parsing thus succeeds without errors because the document now conforms to the specification.
- The gb.web.feed component is currently being revised. The implementation of the RSS 2.0 specification is being maintained. However, it is intended that proprietary XML tags will be ignored in future and will then no longer generate errors during parsing.
Notice:
The project in https://gambas-buch.de/doku.php?id=k18:k18.12:start#projekt_rss-reader can be seen as an alternative in case proprietary XML tags - which must be known - are also to be processed.
You can also find the source code of project 3 in the download area.
