简体   繁体   中英

parse rss feed using javascript

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM