<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> RSS </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function createXMLHttpRequest(){
if (window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
function update()
{
request = false;
try{
request = new XMLHttpRequest();
}catch (trymicrosoft){
try{
request = new ActiveXObject("Msxml2.XMLHTTP");
}catch (othermicrosoft){
try{
request = new ActiveXObject("Microsoft.XMLHTTP");
}catch (failed){
request = false;
}
}
}
request=createXMLHttpRequest();
if (!request){
alert("Error initializing XMLHttpRequest!");
return;
}
request.onreadystatechange=handleResponse;
request.open("GET","./rss.xml",true);
request.send(null);
}
function handleResponse() {
if(request.readyState == 4){
if(request.status == 200){
var doc = request.responseXML;//.documentElement
var resp = document.getElementById("resp");
var info="";
noteTemp=doc.getElementsByTagName("channel");
if(noteTemp.length>0)
node=noteTemp[0].getElementsByTagName("title");//getAttribute
if(node.length>0)
info+="标题:"+node[0].firstChild.nodeValue+"<BR>";
node=noteTemp[0].getElementsByTagName("link");
if(node.length>0)
info+="链接:"+node[0].firstChild.nodeValue+"<BR>";
node=noteTemp[0].getElementsByTagName("description");
if(node.length>0)
info+="描述:"+node[0].firstChild.nodeValue+"<BR>";
node=noteTemp[0].getElementsByTagName("pubDate");
if(node.length>0)
info+="发布时间:"+node[0].firstChild.nodeValue+"<BR>";
node=noteTemp[0].getElementsByTagName("language");
if(node.length>0)
info+="语言:"+node[0].firstChild.nodeValue+"<BR>";
node=noteTemp[0].getElementsByTagName("image");
node=node[0].getElementsByTagName("url");;
if(node.length>0)
info+="Logo:<BR><img src='"+node[0].firstChild.nodeValue+"'><BR>";
if(noteTemp.length>0)
noteTemp=noteTemp[0].getElementsByTagName("item");
info+="消息数量:"+noteTemp.length+"<BR>";
info+="<table width=100%>";
for(i=0;i<noteTemp.length;i++)
{
title=noteTemp[i].getElementsByTagName("title")[0].firstChild.nodeValue;
link=noteTemp[i].getElementsByTagName("link")[0].firstChild.nodeValue;
pubDate=noteTemp[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;
description=noteTemp[i].getElementsByTagName("description")[0].firstChild.nodeValue;
info+="<tr><td><a href=/""+link+"/" target=/"_blank/" title='"+description+"'>"+title+" ["+pubDate+"]</a></td></tr>";
}
info+="</table>";
resp.innerHTML=info;
}
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<div id="resp" width=100% height=400>Loading....</div>
</BODY>
</HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
update();
//-->
</SCRIPT>
此代码对于标准的rss,可以正常运行,并兼容IE和FireFox。不过有些网站提供的RSS不规范,导致无法正常解析,比如cnBeta的rss,内容看起来正常,但是他的Content-Type错误。对于此类问题,如下的办法可以解决IE的问题,FireFox的问题研究中:
增加如下vbscript函数:
<SCRIPT LANGUAGE="vbScript">
<!--
Function bytes2BSTR(vIn)
Dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
strReturn=""
For i=1 To LenB(vIn)
ThisCharCode=AscB(MidB(vIn,i,1))
If ThisCharCode<&H80 Then
strReturn=strReturn & Chr(ThisCharCode)
Else
NextCharCode=AscB(MidB(vIn,i+1,1))
strReturn=strReturn&Chr(CLng(ThisCharCode)*&H100+CInt(NextCharCode))
i=i+1
End If
Next
bytes2BSTR=strReturn
End Function
//-->
</SCRIPT>
查找代码:
var doc = request.responseXML;
将该代码注释掉,并在下面添加:
doc = new ActiveXObject("Microsoft.XMLDOM");
var xml=bytes2BSTR(request.responseBody);
doc.loadXML(xml);