telophase: (Near - que?)
telophase ([personal profile] telophase) wrote2008-09-16 11:22 am
Entry tags:

Anyone here know ASP/VBscript?

I'm trying to convert an ASP RSS parsing script from reading *every* item in an RSS feed to just reading the 3 most recent items. The original script uses a FOR EACH loop that I'm just not quite figuring out how to change over to a regular loop that I can go through three times.

I did find a version of this online that just breaks it after three iterations, but there's got to be a better way than that. Right?



Is it possible to just set objXML.Load to load the first three items? (Hmm... must invesitgate that...)

	objXML.Load(rssFile)
	
	If (objXML.parseError.errorCode = 0) Then
	Set objRoot = objXML.documentElement
	If IsObject(objRoot) = False Then
	response.Write "No Root Found in Rss File"
	End If

Set objItems = objRoot.getElementsByTagName("item")
If IsObject(objItems) = True Then
	Dim objItem

For Each objItem in objItems
	strTitle = objItem.selectSingleNode("title").Text
	On Error Resume Next
	strDesc = objItem.selectSingleNode("description").Text
	On Error Resume Next
	strLink = objItem.selectSingleNode("link").Text
	On Error Resume Next
	strDate = objItem.selectSingleNode("pubDate").Text
	On Error Resume Next
					
	response.Write [formatting and items]		
Next

[identity profile] jarodrussell.livejournal.com 2008-09-16 04:40 pm (UTC)(link)
Try this. (http://andrewburton.biz/blog/wp-content/uploads/rss_cache.asp.txt)

Set RSSItems = xmldoma.getElementsByTagName("item")
    
For X = 0 To 3
        
    Set RSSItem = RSSItems.Item(X)

    ...
        
Next

[identity profile] telophase.livejournal.com 2008-09-16 04:48 pm (UTC)(link)
Yay! It works! Thank you! :D

[identity profile] thomasyan.livejournal.com 2008-09-16 04:59 pm (UTC)(link)
The for loop processes only the first three items. But will everything still get read in on the previous line?

[identity profile] jarodrussell.livejournal.com 2008-09-16 05:21 pm (UTC)(link)
Yes. As far as I know, you can't get around loading all "item" nodes into memory without writing your own RegExp-ish parser.

EDIT: I take that back...*codes*

EDIT: No, I take it back. Even if you use XPath to pull out the individual nodes, you've still got to load each node into memory. The best alternative I could come up with is this, which might save a bit of memory since it only declares those objects in the scope of the loop:

For X = 0 To 2
        
    Set RSSItem = xmldoma.getElementsByTagName("item")(X)

    ...

Next


EDIT: Okay, seriously, the last time. XPath does work. Here:

Set RSSItems = xmldoma.selectNodes("/rss/channel/item[position()<4]")
    
For X = 0 To RSSLen
        
    Set RSSItem = RSSItems.Item(X)

    ...

Next