Sign in to follow this  
Followers 0
sjorrel

RSS Feed to Array

2 posts in this topic

For a project I'm working on I needed a way to read data from an RSS feed into an array, so I came up with these functions .

func rssToArray ($feedUrl) 
;This function returns a two dimensional array.
;It requires the URL of an RSS feed as a parameter.
;The first dimension of the array represents each item in the feed.
;The second dimension is as follows:
;   0 = The title of the news item
;   1 = The news item description
;   2 = Link to the item 
;
;   [0][0] returns the number of news items. 
    
    dim $rssString = ""
    dim $rssFilepath = @tempdir & "\tempfeed.dat"
    
    inetget ( $feedUrl, $rssFilepath );downloads the rss feed to the temp dir
    
    $rssFile = fileopen ($rssFilepath,0)    
    While 1;reads the contents of the downloaded rss feed into a string
        $newChar = FileRead($rssFile, 1)
        If @error = -1 Then ExitLoop
        $rssString = $rssString & $newChar
    Wend
    fileclose ( $rssFile ) 
    
    $tempArray = StringSplit ( $rssString, "<item>" , 1 );splits the rss string into news items
    
    $numberOfElements = ( ubound ( $tempArray ) ) 
    
    dim $rssArray [$numberOfElements-1][3];creates the array to hold news items
    
    $rssArray[0][0] = $numberOfElements-2;sets [0][0] to the number of news items available in the feed 
    
    filedelete ( @tempdir & "\tempfeed.data" );cleans up data file
    
    for $x=2 to $numberOfElements-1 ;populates the array with data
        $rssArray[$x-1][0] = extractString ($tempArray[$x],"<title>","</title>")
        $rssArray[$x-1][1] = extractString ($tempArray[$x],"<description>","</description>")
        $rssArray[$x-1][2] = extractString ($tempArray[$x],"<link>","</link>")
    next 
    
    return $rssArray
EndFunc

func extractString ( $string, $startString, $endString ) 
;this function returns the contents of a string between two substrings
    $startStringPos = stringinstr ( $string, $startString ) 
    $endStringPos = stringinstr ( $string, $endString ) 
    
    $startPos = $startStringPos + stringlen ( $startString ) - 1
    $endPos = $endStringPos - 1
    
    $extractedString = stringleft ( $string, $endPos )
    $extractedString = stringtrimleft ( $extractedString, $startPos ) 
    
    return $extractedString
EndFunc

It could do with being expanded upon, maybe by returning details about the feed such as its title, ttl etc. But I didn't need that at the moment.

I did a search before creating this, and didn't see any other function like it. Maybe somebody will have some use from it.

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0