wiki:UsingFeedsInCode

Version 4 (modified by nick, 15 years ago) (diff)

add javascript example

Having identified which feed to use, it is then very easy to make use of this feed data in your applications.

Below, we hope to present example code for using OpenGuides RSS feeds in a number of common web application programming languages.

PHP

Ruby On Rails

ColdFusion

Django (Python)

JavaScript

Note that the JavaScript? will either need to come from the same server as the feed, or be running with the browser granting it extra security rights, otherwise you'll get a security exception when you try to request something off another server.

An example for pulling out titles and modified dates from the recent changes RSS feed, and chucking them into a paragraph with the id "data" follows.

// This will fetch the Recent Changes RSS from an OpenGuide on the
//  same server as the script. 
// It will then output the node title and modified date, into a
//  paragraph on the page with id "data"
// You will need to ensure that there is:
//      <p id="data" />
// Already on the page before calling this
var feed_url = "/og-wiki/wiki.cgi?action=rc;format=rss";

function go() {
    // Get a XMLHttpRequest Object
    var xmlhttp = false;
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    // Use it
    if(xmlhttp) {
        xmlhttp.open("GET", feed_url, false);
        xmlhttp.send(null);

        var status = xmlhttp.status;
        var p = document.getElementById("data");

        if(status == 200 || status == 304 || status == 0 || status == null) {
            // Bingo, we have the feed
            var feed = xmlhttp.responseXML;

            // Go find the individual item entries
            var items = feed.getElementsByTagName("item");

            // Loop over the items, getting the titles and dates out
            for(var i=0; i<items.length; i++) {
                var item = items[i];
                var ic = item.childNodes;

                var title;
                var date;
                for(var j=0; j<ic.length; j++) {
                    var tag = ic[j];
                    if(tag.nodeType == Node.ELEMENT_NODE) {
                        if(tag.tagName == "title") {
                            title = tag;
                        }
                        if(tag.tagName == "dc:date") {
                            date = tag;
                        }
                    }
                }

                // Add a div with the title and date
                var sub_div = document.createElement("div");
                sub_div.appendChild(document.createTextNode(
                    title.firstChild.nodeValue + " - modified " + date.firstChild.nodeValue
                ));
                p.appendChild(sub_div);
            }
        } else {
            var text = "Error code " + status;
            p.appendChild(document.createTextNode(text));
        }
    }
}
go();