Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

All the XML files are corrupt, or something

Status
Not open for further replies.

astralis

Programmer
Nov 1, 2002
1
US
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(&quot;Scripting.FileSystemObject&quot;)
strMyName = Request.ServerVariables(&quot;SCRIPT_NAME&quot;)
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 & &quot;0&quot; & Month(Now) Else GetDateTime = GetDateTime & Month(Now)
If Day(Now) < 10 Then GetDateTime = GetDateTime & &quot;0&quot; & Day(Now) Else GetDateTime = GetDateTime & Day(Now)
GetDateTime = GetDateTime & Year(Now)
If Hour(Now) < 10 Then GetDateTime = GetDateTime & &quot;0&quot; & Hour(Now) Else GetDateTime = GetDateTime & Hour(Now)
If Minute(Now) < 10 Then GetDateTime = GetDateTime & &quot;0&quot; & Minute(Now) Else GetDateTime = GetDateTime & Minute(Now)
If Second(Now) < 10 Then GetDateTime = GetDateTime & &quot;0&quot; & 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) & &quot;/&quot; & Right(strTemp, 2)
strTemp = Left(strDate, 6)
strDate = Right(strDate, 4)
SplitDateTime = SplitDateTime & &quot;/&quot; & Left(strTemp, 4) & &quot; &quot; & Right(strTemp, 2) & &quot;:&quot; & Left(strDate, 2) & &quot;:&quot; & 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, &quot;&quot;)
Else
RemoveItem = strInput
End If
End Function


'If objFSO.FileExists(Server.MapPath(&quot;.&quot;) & &quot;/&quot; & strNewsfile) Then
' Set objFile = objFSO.OpenTextFile(Server.MapPath(&quot;.&quot;) & &quot;/&quot; & strNewsfile, 1, False)
' i = 0
' Do While Not objFile.AtEndOfStream
' ReDim Preserve arrFeeds(1,i)
' strTemp = objFile.Readline
' If Not Instr(strTemp, &quot;,&quot;) = 0 Then
' arrFeed = Split(strTemp, &quot;,&quot;)
' 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(&quot;Microsoft.XMLDOM&quot;)

'For i = 0 to UBound(arrFeeds,2)
strXMLfile = strRSSURL
strFile = strRSSNAME
strCache = &quot;&quot;
blnFound = False

If blnCache Then 'Begin caching code
Set objFolder = objFSO.GetFolder(Server.MapPath(&quot;.&quot;))
For Each objFile In objFolder.Files
If Right(objFile.Name, Len(strFile & &quot;.xml&quot;)) = strFile & &quot;.xml&quot; Then 'Do we have a match for the XML file?
If DateDiff(&quot;n&quot;, SplitDateTime(Left(objFile.Name, 14)), Now) < intDelay Then 'Is the file within the time-to-live?
If Not strCache = &quot;&quot; Then 'Have we found a cache file for this already?
If DateDiff(&quot;n&quot;, SplitDateTime(Left(objFile.Name, 14)), Now) < DateDiff(&quot;n&quot;, 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(&quot;./&quot; & 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(&quot;./&quot; & strCache))
Else 'We're either not caching files, or we didn't find one. Get a new copy.
Set xmlHTTP = Server.CreateObject(&quot;Microsoft.XMLHTTP&quot;)
xmlHTTP.Open &quot;GET&quot;,strXMLfile,false
xmlHTTP.SetRequestHeader &quot;Content-type&quot;, &quot;text/xml&quot;
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, &quot; encoding=&quot;&quot;&quot;, &quot;&quot;&quot;&quot;)
strXML = RemoveItem(strXML, &quot;<!DOCTYPE&quot;, &quot;>&quot;)

'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 &quot;[&quot; & strFile & &quot;] Error: &quot; & .reason & &quot;<BR>&quot; & VbCrLf
Response.Write &quot;[&quot; & strFile & &quot;] Line: &quot; & .line & &quot; (&quot; & .linepos & &quot;) - &quot; & .srcText & &quot;<BR>&quot; & 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(&quot;..&quot;) & &quot;/data/&quot; & GetDateTime & &quot;_&quot; & strFile & &quot;.xml&quot;)

'Start dumping parsed XML into the Response.Buffer
Response.Write &quot;<table>&quot; & VbCrLf
set objRoot = xmlObj.documentElement 'Set the root of the XML object
set objChannel = objRoot.selectSingleNode(&quot;channel&quot;) 'Get the channel object for newsfeed info
set objLinks = objRoot.getElementsByTagName(&quot;item&quot;) 'Get a collection of all the items in the channel

'Dump the channel info into the buffer
Response.Write &quot;<thead><a href=&quot;&quot;&quot; & objChannel.selectSingleNode(&quot;link&quot;).text & &quot;&quot;&quot; title=&quot;&quot;&quot; & objChannel.selectSingleNode(&quot;description&quot;).text & &quot;&quot;&quot;>&quot; & objChannel.selectSingleNode(&quot;title&quot;).text & &quot;</a> - <a href=&quot;&quot;&quot; & strMyName & &quot;?full=true&quot;&quot;>Show Descriptions</a></thead>&quot;
Response.Write &quot;<tr>&quot; & VbCrLf & &quot; <td>&quot; & VbCrLf

'Dump the links into the buffer, if &quot;Show Descriptions&quot; is clicked,
'then we dump those into the buffer, too.
For Each objChild in objLinks
Response.Write &quot; <a href=&quot;&quot;&quot; & objChild.selectSingleNode(&quot;link&quot;).text & &quot;&quot;&quot; target=&quot;&quot;_blank&quot;&quot;>&quot; & objChild.selectSingleNode(&quot;title&quot;).text & &quot;</a><BR>&quot; & VbCrLf
If Request(&quot;full&quot;) = &quot;true&quot; Then Response.Write &quot;&nbsp;&nbsp;&quot; & objChild.selectSingleNode(&quot;description&quot;).text & &quot;<BR><BR>&quot; & VbCrLF
Next

'Clean up after yourself
Set objRoot = Nothing
set objChannel = Nothing
set objLinks = Nothing
set objChild = Nothing
End If
Response.Write &quot; </td>&quot; & VbCrLf & &quot;</tr>&quot; & VbCrLf & &quot;</table>&quot; & VbCrLf
' If Not i = UBound(arrFeeds,2) Then Response.Write &quot;<BR>&quot; & VbCrLf
Response.Flush 'Dump the buffer to the browser
'Next 'Do it all again!
set xmlObj = nothing
%>
<!-- END SAMPLE XML -->
 
Maybe the RemoveItem functions messes things up.
It looks suspicious enough ;-)
Check if strXML is valid xml before and after
strXML = RemoveItem(strXML, &quot; encoding=&quot;&quot;&quot;, &quot;&quot;&quot;&quot;)
strXML = RemoveItem(strXML, &quot;<!DOCTYPE&quot;, &quot;>&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top