Monday, April 19, 2010

A Class for RSS Data

The first goal of decoding the various forms of RSS files is to create a base class to encapsulate the functionality and data of the RSS file.  The basic structure of an RSS file is a channel with a title, link, description, and several items with titles, links,and descriptions.  This statement should make the initial structure of the base class obvious.  It is also good to note that other data besides these three fields can be present in the various formats.  For instance, the channel may contain a webmaster email address or other such items.  Such information is mostly metadata by definition.  We should add functionality for dealing with this data if we so choose in any instance of the class.  With this in mind, I created the following CRSSItem class in the Class Designer:


I decided to handle metadata by creating a Collection object to store the data as Name Value pairs and a Flag to tell the class whether that data should be captured or ignored.

Private m_col_metadata As Collection
Private m_hasmetadata As Boolean = False

The MetaInfo and SetMetaInfo Methods expose the m_col_metadata Collection to the calling application.  The MetaInfo method takes a key representing the name of metadata and returns the corresponding value from the Collection.

Public Function MetaInfo(ByVal Key As String) As String
       MetaInfo = m_col_metadata(Key)
End Function

The SetMetaInfo simply adds or replaces metadata info for a given key.

Public Sub SetMetaInfo(ByVal key As String, ByVal data As String)
     If m_col_metadata.Contains(key) Then
            m_col_metadata.Remove(key)
     End If
     m_col_metadata.Add(data, key)

End Sub

Not shown in the diagram are the OnError and Progress events I added to enhance communication to the calling app.  The OnError event allows the application to handle all errors from which the class can't correct itself.  The Progress event can be used to pass status information back to the app in the case of long process locking calls.  This allows for status bar updates and such.

Now that we have created our base class, we can use VB class inheritance to create a class to process the RSS feed and store the data into a collection of RSSItems.



CRSSFeed inherits from CRSSItem using the following statement:

Inherits CRSSItem

I added an Items property to expose the collection of RSS items to the calling application.  The collection of RSS Items is a Generic.Dictionary collection created like this:

Private m_col_items As New System.Collections.Generic.Dictionary(Of Integer, CRSSItem)


Public ReadOnly Property Items() As System.Collections.Generic.Dictionary(Of Integer, CRSSItem)
     Get
           Items = m_col_items
     End Get

End Property

This allows for a call to the Values member of Items for For...Each looping in the calling app.

I also added a Version property to relay what we find out about the version of the RSS file back.  There is also a Create method which takes a URL as an argument.  In upcoming posts I will explain the various formats of RSS files such as RDF, RSS 2.0, and Atom and we will develop our Create Method to put data from these files into the class structure.

No comments:

Post a Comment