Jump to content

RSS Feed to Array


Recommended Posts

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
    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>")
    return $rssArray

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

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.

Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...