4

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
2
  • 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. Commented May 14, 2012 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. Commented May 14, 2012 at 20:30

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

return xmlHttp.responseXML; is exactly what I needed thank you.
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.
That is another question entirely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.