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
share|improve this question
xmlHttp.responseText is a String. Strings do not have a getElementsByName() function. Are you expecting an XML response? Try xmlHttp.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:22
@Cory thank you for the response, and yes xmlHttp.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

1 Answer

up vote 2 down vote accepted

Try changing the last line of the httpGet function to:

return xmlHttp.responseXML;

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");

To force the return content to be sent as XML.

share|improve this answer
return xmlHttp.responseXML; is exactly what I needed thank you. – Mr. 1.0 May 14 '12 at 20:28
Can you help me with one more thing? I want to loop over all the dates and only return the titles that match the current date of today. I updated my post. – Mr. 1.0 May 14 '12 at 21:09
That is another question entirely. – D. Strout May 14 '12 at 21:16

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.