使用javascript解析RSS提要 [英] parse rss feed using javascript
问题描述
我正在使用 PHP 和 JavaScript 解析 RSS 提要.首先,我用 PHP 创建了一个代理来获取 RSS 提要.然后使用 JavaScript 从这个 RSS 提要中获取单个数据.我的 JavaScript 问题.如果我使用 console.log(rssData);
并且没有错误,我就可以获得整个 JavaScript 文档.如果我尝试获取本文档中的单个元素,例如:
、
或
使用 rssData.getElementsByName("title");
它会给出错误Uncaught TypeError: Object....has no method 'getElementsByName'".所以我的问题是如何获取 RSS 提要中的元素?
I am parsing an RSS feed using PHP and JavaScript. First I created a proxy with PHP to obtain the RSS feed. Then get individual data from this RSS feed using JavaScript. My issue with with the JavaScript. I am able to get the entire JavaScript document if I use console.log(rssData);
with no errors. If I try to get individual elements within this document say for example: <title>
, <description>
, or <pubDate>
using rssData.getElementsByName("title");
it gives an error "Uncaught TypeError: Object....has no method 'getElementsByName'". So my question is how to I obtain the elements in the RSS feed?
Javascript(更新)
function httpGet(theUrl) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
return xmlHttp.responseXML;
}
// rss source
var rssData = httpGet('http://website.com/rss.php');
// rss values
var allTitles = rssData.getElementsByTagName("title"); // title
var allDate = rssData.getElementsByTagName("pubDate"); // date
推荐答案
尝试将 httpGet
函数的最后一行更改为:
Try changing the last line of the httpGet
function to:
return xmlHttp.responseXML;
毕竟,您期待的是 XML 响应.您可能还需要将此行添加到您的 PHP 代理:
After all, you are expecting an XML response back. You may also need to add this line to your PHP proxy:
header("Content-type: text/xml");
强制将返回内容作为 XML 发送.
To force the return content to be sent as XML.
这篇关于使用javascript解析RSS提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!