Using ASP, I have the following code which views RSS files. But, recently. every single RSS file that I have connected to is corrupt. I get errors telling me the <hr> tag in the beginning doesn't match the </table> tag at the end.
I'm really confused why this doesn't work.
Any help is appreciated.
<!-- SAMPLE XML -->
<br>
<%
Dim xmlObj,xmlHTTP,objRoot,objLinks,objChannel,objChild
Dim i,arrFeeds(),arrFeed,strMyName,strXMLfile,strFile,intDelay,strXML,strTemp,strNewsfile
Dim blnCache,objFSO,objFolder,objFile,blnFound,strCache
'Initialize.
Set objFSO = Server.CreateObject("Scripting.FileSystemObject"
strMyName = Request.ServerVariables("SCRIPT_NAME"
strNewsfile = strRSSURL 'File that the URL and feed names are stored in
intDelay = 30 'Time-to-live for cached XML files
blnCache = False 'True = Cache files; False = Get file every time
'Creates a timestamp string
Function GetDateTime()
If Month(Now) < 10 Then GetDateTime = GetDateTime & "0" & Month(Now) Else GetDateTime = GetDateTime & Month(Now)
If Day(Now) < 10 Then GetDateTime = GetDateTime & "0" & Day(Now) Else GetDateTime = GetDateTime & Day(Now)
GetDateTime = GetDateTime & Year(Now)
If Hour(Now) < 10 Then GetDateTime = GetDateTime & "0" & Hour(Now) Else GetDateTime = GetDateTime & Hour(Now)
If Minute(Now) < 10 Then GetDateTime = GetDateTime & "0" & Minute(Now) Else GetDateTime = GetDateTime & Minute(Now)
If Second(Now) < 10 Then GetDateTime = GetDateTime & "0" & Second(Now) Else GetDateTime = GetDateTime & Second(Now)
End Function
'Converts a GetDateTime() timestamp back to a date
Function SplitDateTime(strDate)
strTemp = Left(strDate, 4)
strDate = Right(strDate, 10)
SplitDateTime = Left(strTemp, 2) & "/" & Right(strTemp, 2)
strTemp = Left(strDate, 6)
strDate = Right(strDate, 4)
SplitDateTime = SplitDateTime & "/" & Left(strTemp, 4) & " " & Right(strTemp, 2) & ":" & Left(strDate, 2) & ":" & Right(strDate, 2)
End Function
'This function was designed to remove a tag or tag property.
'Specifically, items that cause parsing errors in the XMLDOM.
Function RemoveItem(strInput, strItem, strEnd)
Dim strRem
If InStr(strInput, strItem) > 0 Then
strRem = Mid(strInput,InStr(strInput,strItem),InStr(InStr(strInput,strItem) + Len(strItem),strInput,strEnd) - InStr(strInput,strItem) + 1)
RemoveItem = Replace(strInput, strRem, ""
Else
RemoveItem = strInput
End If
End Function
'If objFSO.FileExists(Server.MapPath("."
& "/" & strNewsfile) Then
' Set objFile = objFSO.OpenTextFile(Server.MapPath("."
& "/" & strNewsfile, 1, False)
' i = 0
' Do While Not objFile.AtEndOfStream
' ReDim Preserve arrFeeds(1,i)
' strTemp = objFile.Readline
' If Not Instr(strTemp, ","
= 0 Then
' arrFeed = Split(strTemp, ","
' arrFeeds(0,i) = arrFeed(0)
' arrFeeds(1,i) = arrFeed(1)
' i = i + 1
' End If
' Loop
'Else
' ReDim arrFeeds(0,0)
' arrFeeds(0,0) = strRSSURL
' arrFeeds(0,1) = strRSSNAME
'End If
set xmlObj = Server.CreateObject("Microsoft.XMLDOM"
'For i = 0 to UBound(arrFeeds,2)
strXMLfile = strRSSURL
strFile = strRSSNAME
strCache = ""
blnFound = False
If blnCache Then 'Begin caching code
Set objFolder = objFSO.GetFolder(Server.MapPath("."
)
For Each objFile In objFolder.Files
If Right(objFile.Name, Len(strFile & ".xml"
) = strFile & ".xml" Then 'Do we have a match for the XML file?
If DateDiff("n", SplitDateTime(Left(objFile.Name, 14)), Now) < intDelay Then 'Is the file within the time-to-live?
If Not strCache = "" Then 'Have we found a cache file for this already?
If DateDiff("n", SplitDateTime(Left(objFile.Name, 14)), Now) < DateDiff("n", SplitDateTime(Left(strCache, 14)), Now) Then 'Is this file younger than the other one?
blnFound = True 'Yup. Delete the old one.
objFSO.DeleteFile(Server.MapPath("./" & strCache))
strCache = objFile.Name
Else 'Nope. Delete it.
objFSO.DeleteFile(objFile.Path)
End If
Else 'Haven't found one yet, this one will work for now.
blnFound = True
strCache = objFile.Name
End If
Else 'Too old. Delete it.
objFSO.DeleteFile(objFile.Path)
End If
End If
Next
End If
If blnCache and blnFound Then 'We're caching files and we've found a cached file. Load it.
xmlObj.async = False
xmlObj.Load(Server.MapPath("./" & strCache))
Else 'We're either not caching files, or we didn't find one. Get a new copy.
Set xmlHTTP = Server.CreateObject("Microsoft.XMLHTTP"
xmlHTTP.Open "GET",strXMLfile,false
xmlHTTP.SetRequestHeader "Content-type", "text/xml"
xmlHTTP.Send
strXML = xmlHTTP.ResponseText
'Microsoft's XMLDOM can't handle encoding or doctypes too well,
'so I'm removing the appropriate tags and properties.
strXML = RemoveItem(strXML, " encoding=""", """"
strXML = RemoveItem(strXML, "<!DOCTYPE", ">"
'This loop changes every character to VB compatible characters.
'Since we're not changing encoding in the files, this will
'fix some errors. I know it's not efficient, but it works.
Dim x
For x = 1 to Len(strXML)
strXML = Left(strXML, x-1) & Chr(Asc(Mid(strXML, x, 1))) & Right(strXML, Len(strXML) - x)
Next
xmlObj.async = False
xmlObj.loadXML(strXML)
Set xmlHTTP = Nothing
End If
'Got a problem? Handle it.
If Not xmlObj.parseError.errorCode = 0 then
With xmlObj.parseError
Response.Write "[" & strFile & "] Error: " & .reason & "<BR>" & VbCrLf
Response.Write "[" & strFile & "] Line: " & .line & " (" & .linepos & "
- " & .srcText & "<BR>" & VbCrLf
End With
Else
'If we're caching and we don't have a cached file, create one.
If blnCache and Not blnFound and Len(strXML) > 0 Then xmlObj.Save(Server.MapPath(".."
& "/data/" & GetDateTime & "_" & strFile & ".xml"
'Start dumping parsed XML into the Response.Buffer
Response.Write "<table>" & VbCrLf
set objRoot = xmlObj.documentElement 'Set the root of the XML object
set objChannel = objRoot.selectSingleNode("channel"
'Get the channel object for newsfeed info
set objLinks = objRoot.getElementsByTagName("item"
'Get a collection of all the items in the channel
'Dump the channel info into the buffer
Response.Write "<thead><a href=""" & objChannel.selectSingleNode("link"
.text & """ title=""" & objChannel.selectSingleNode("description"
.text & """>" & objChannel.selectSingleNode("title"
.text & "</a> - <a href=""" & strMyName & "?full=true"">Show Descriptions</a></thead>"
Response.Write "<tr>" & VbCrLf & " <td>" & VbCrLf
'Dump the links into the buffer, if "Show Descriptions" is clicked,
'then we dump those into the buffer, too.
For Each objChild in objLinks
Response.Write " <a href=""" & objChild.selectSingleNode("link"
.text & """ target=""_blank"">" & objChild.selectSingleNode("title"
.text & "</a><BR>" & VbCrLf
If Request("full"
= "true" Then Response.Write " " & objChild.selectSingleNode("description"
.text & "<BR><BR>" & VbCrLF
Next
'Clean up after yourself
Set objRoot = Nothing
set objChannel = Nothing
set objLinks = Nothing
set objChild = Nothing
End If
Response.Write " </td>" & VbCrLf & "</tr>" & VbCrLf & "</table>" & VbCrLf
' If Not i = UBound(arrFeeds,2) Then Response.Write "<BR>" & VbCrLf
Response.Flush 'Dump the buffer to the browser
'Next 'Do it all again!
set xmlObj = nothing
%>
<!-- END SAMPLE XML -->
I'm really confused why this doesn't work.
Any help is appreciated.
<!-- SAMPLE XML -->
<br>
<%
Dim xmlObj,xmlHTTP,objRoot,objLinks,objChannel,objChild
Dim i,arrFeeds(),arrFeed,strMyName,strXMLfile,strFile,intDelay,strXML,strTemp,strNewsfile
Dim blnCache,objFSO,objFolder,objFile,blnFound,strCache
'Initialize.
Set objFSO = Server.CreateObject("Scripting.FileSystemObject"
strMyName = Request.ServerVariables("SCRIPT_NAME"
strNewsfile = strRSSURL 'File that the URL and feed names are stored in
intDelay = 30 'Time-to-live for cached XML files
blnCache = False 'True = Cache files; False = Get file every time
'Creates a timestamp string
Function GetDateTime()
If Month(Now) < 10 Then GetDateTime = GetDateTime & "0" & Month(Now) Else GetDateTime = GetDateTime & Month(Now)
If Day(Now) < 10 Then GetDateTime = GetDateTime & "0" & Day(Now) Else GetDateTime = GetDateTime & Day(Now)
GetDateTime = GetDateTime & Year(Now)
If Hour(Now) < 10 Then GetDateTime = GetDateTime & "0" & Hour(Now) Else GetDateTime = GetDateTime & Hour(Now)
If Minute(Now) < 10 Then GetDateTime = GetDateTime & "0" & Minute(Now) Else GetDateTime = GetDateTime & Minute(Now)
If Second(Now) < 10 Then GetDateTime = GetDateTime & "0" & Second(Now) Else GetDateTime = GetDateTime & Second(Now)
End Function
'Converts a GetDateTime() timestamp back to a date
Function SplitDateTime(strDate)
strTemp = Left(strDate, 4)
strDate = Right(strDate, 10)
SplitDateTime = Left(strTemp, 2) & "/" & Right(strTemp, 2)
strTemp = Left(strDate, 6)
strDate = Right(strDate, 4)
SplitDateTime = SplitDateTime & "/" & Left(strTemp, 4) & " " & Right(strTemp, 2) & ":" & Left(strDate, 2) & ":" & Right(strDate, 2)
End Function
'This function was designed to remove a tag or tag property.
'Specifically, items that cause parsing errors in the XMLDOM.
Function RemoveItem(strInput, strItem, strEnd)
Dim strRem
If InStr(strInput, strItem) > 0 Then
strRem = Mid(strInput,InStr(strInput,strItem),InStr(InStr(strInput,strItem) + Len(strItem),strInput,strEnd) - InStr(strInput,strItem) + 1)
RemoveItem = Replace(strInput, strRem, ""
Else
RemoveItem = strInput
End If
End Function
'If objFSO.FileExists(Server.MapPath("."
' Set objFile = objFSO.OpenTextFile(Server.MapPath("."
' i = 0
' Do While Not objFile.AtEndOfStream
' ReDim Preserve arrFeeds(1,i)
' strTemp = objFile.Readline
' If Not Instr(strTemp, ","
' arrFeed = Split(strTemp, ","
' arrFeeds(0,i) = arrFeed(0)
' arrFeeds(1,i) = arrFeed(1)
' i = i + 1
' End If
' Loop
'Else
' ReDim arrFeeds(0,0)
' arrFeeds(0,0) = strRSSURL
' arrFeeds(0,1) = strRSSNAME
'End If
set xmlObj = Server.CreateObject("Microsoft.XMLDOM"
'For i = 0 to UBound(arrFeeds,2)
strXMLfile = strRSSURL
strFile = strRSSNAME
strCache = ""
blnFound = False
If blnCache Then 'Begin caching code
Set objFolder = objFSO.GetFolder(Server.MapPath("."
For Each objFile In objFolder.Files
If Right(objFile.Name, Len(strFile & ".xml"
If DateDiff("n", SplitDateTime(Left(objFile.Name, 14)), Now) < intDelay Then 'Is the file within the time-to-live?
If Not strCache = "" Then 'Have we found a cache file for this already?
If DateDiff("n", SplitDateTime(Left(objFile.Name, 14)), Now) < DateDiff("n", SplitDateTime(Left(strCache, 14)), Now) Then 'Is this file younger than the other one?
blnFound = True 'Yup. Delete the old one.
objFSO.DeleteFile(Server.MapPath("./" & strCache))
strCache = objFile.Name
Else 'Nope. Delete it.
objFSO.DeleteFile(objFile.Path)
End If
Else 'Haven't found one yet, this one will work for now.
blnFound = True
strCache = objFile.Name
End If
Else 'Too old. Delete it.
objFSO.DeleteFile(objFile.Path)
End If
End If
Next
End If
If blnCache and blnFound Then 'We're caching files and we've found a cached file. Load it.
xmlObj.async = False
xmlObj.Load(Server.MapPath("./" & strCache))
Else 'We're either not caching files, or we didn't find one. Get a new copy.
Set xmlHTTP = Server.CreateObject("Microsoft.XMLHTTP"
xmlHTTP.Open "GET",strXMLfile,false
xmlHTTP.SetRequestHeader "Content-type", "text/xml"
xmlHTTP.Send
strXML = xmlHTTP.ResponseText
'Microsoft's XMLDOM can't handle encoding or doctypes too well,
'so I'm removing the appropriate tags and properties.
strXML = RemoveItem(strXML, " encoding=""", """"
strXML = RemoveItem(strXML, "<!DOCTYPE", ">"
'This loop changes every character to VB compatible characters.
'Since we're not changing encoding in the files, this will
'fix some errors. I know it's not efficient, but it works.
Dim x
For x = 1 to Len(strXML)
strXML = Left(strXML, x-1) & Chr(Asc(Mid(strXML, x, 1))) & Right(strXML, Len(strXML) - x)
Next
xmlObj.async = False
xmlObj.loadXML(strXML)
Set xmlHTTP = Nothing
End If
'Got a problem? Handle it.
If Not xmlObj.parseError.errorCode = 0 then
With xmlObj.parseError
Response.Write "[" & strFile & "] Error: " & .reason & "<BR>" & VbCrLf
Response.Write "[" & strFile & "] Line: " & .line & " (" & .linepos & "
End With
Else
'If we're caching and we don't have a cached file, create one.
If blnCache and Not blnFound and Len(strXML) > 0 Then xmlObj.Save(Server.MapPath(".."
'Start dumping parsed XML into the Response.Buffer
Response.Write "<table>" & VbCrLf
set objRoot = xmlObj.documentElement 'Set the root of the XML object
set objChannel = objRoot.selectSingleNode("channel"
set objLinks = objRoot.getElementsByTagName("item"
'Dump the channel info into the buffer
Response.Write "<thead><a href=""" & objChannel.selectSingleNode("link"
Response.Write "<tr>" & VbCrLf & " <td>" & VbCrLf
'Dump the links into the buffer, if "Show Descriptions" is clicked,
'then we dump those into the buffer, too.
For Each objChild in objLinks
Response.Write " <a href=""" & objChild.selectSingleNode("link"
If Request("full"
Next
'Clean up after yourself
Set objRoot = Nothing
set objChannel = Nothing
set objLinks = Nothing
set objChild = Nothing
End If
Response.Write " </td>" & VbCrLf & "</tr>" & VbCrLf & "</table>" & VbCrLf
' If Not i = UBound(arrFeeds,2) Then Response.Write "<BR>" & VbCrLf
Response.Flush 'Dump the buffer to the browser
'Next 'Do it all again!
set xmlObj = nothing
%>
<!-- END SAMPLE XML -->