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 (Updated)
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
xmlHttp.responseText
is aString
.String
s do not have agetElementsByName()
function. Are you expecting an XML response? TryxmlHttp.responseXML
instead. Also, you might run into cross-domain issues doing this with JS. You might want to get the feed on the PHP side through an AJAX request to your own application. – Cory May 14 '12 at 20:22xmlHttp.responseXML
is exactly what I was looking for. I also added the origin policy header to all access. – Mr. 1.0 May 14 '12 at 20:30