div中的Javascript feedreader

时间:2011-10-20 23:17:00

标签: javascript xml html rss

实现JS脚本来提取/解析XML提要并将内容返回到html。不幸的是,它似乎没有这样做,因为页面上只显示div标签之间的文本(Feed!)。

function loadRSSFeed(param){
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET",'http://feeds.feedburner.com/engadget/Dnjv',false);
  xmlhttp.send();
  xmlDoc = xmlhttp.responseXML;
  var strBuffer= "";
  strBuffer = strBuffer +"<table border='1'>";
  var x=xmlDoc.getElementsByTagName("item");
  for (i=0;i<x.length;i++){   
        strBuffer = strBuffer +"<tr><td><a href='";
        strBuffer = strBuffer +(x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
        strBuffer = strBuffer +"'>";
        strBuffer = strBuffer +(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
        strBuffer = strBuffer +"</a></td></tr><tr><td>";
        strBuffer = strBuffer +(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue.substring(0,180));
        strBuffer = strBuffer + "<a href='";
        strBuffer = strBuffer +(x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
        strBuffer = strBuffer +"'>... Read More...</a>";
        strBuffer = strBuffer +"</td></tr>";
        if(i==10){
          break;
        }
    }
  strBuffer = strBuffer +"</table>";
  document.getElementById(param).innerHTML =strBuffer;
}

它是这样导入的(成功):       然后在身体中调用

<body onload ="loadRSSFeed('feeddisplay');">
  <h1>All the news that's fit to render</h1>
  <div id="feeddisplay">Feed!</div>
</body>

但它没有显示出来!页面的其余部分确实如此......

思想?

2 个答案:

答案 0 :(得分:2)

不应该是XMLHttp * 请求 *?

答案 1 :(得分:1)

谷歌供稿阅读器api更强大,并且没有更多功能来处理rss

http://code.google.com/apis/feed/v1/

另外我认为onLoad可能无法找到div ...?你在加载div feed之后尝试执行脚本吗...只需要2美分。