javascript-rss-parser

Gesponsorde koppelingen

PHP script bestanden

  1. javascript-rss-parser

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
** Javascript RSS parser
**
** copyright (c) Stas Verberkt (Legolas), 2001 - 2006
** Released under the GNU General Public License
**
** WWW: http://www.legolasweb.nl/
** E-mail: [email protected]
**
** Using:
** Include this JS file in your page
** Make sure there's a div with id="rss"
** To change the feed edit the feed var at the bottom
** default it uses the query string
** (it prepends the query string with get.php?,
** because it isn't allowed to open extern sites using xmlHTTPRequest.
** get.php is a php file which servers the page.)
*/

/* Functions */
function init() {
    if (!feed) {
        var goal = document.getElementById('rss');
        goal.innerHTML = 'No feed specified';
    }
    else {
        getFeed();
    }
}

function createHTTPHandler() {
    var httphandler = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
        // JScript gives us Conditional compilation, we can cope with old IE versions.
        // and security blocked creation of the objects.
        try {
            httphandler = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httphandler = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E) {
                httphandler = false;
            }
        }
    @end @*/
    if (!httphandler && typeof XMLHttpRequest != 'undefined') {
        httphandler = new XMLHttpRequest();
    }
    return httphandler;
}

function getFeed() {
    xmlhttprequest.open('GET', feed, true);
    xmlhttprequest.onreadystatechange = parseFeed;
    xmlhttprequest.send(null);
}

function parseFeed() {
    if (xmlhttprequest.readyState == 4) {
        var error = null;
        if (xmlhttprequest.responseXML) {
            if (xmlhttprequest.responseXML.documentElement) {
                if (xmlhttprequest.responseXML.documentElement.tagName == 'parsererror') {
                    error = 'Error parsing XML response:<br /> ' + xmlhttprequest.responseXML.documentElement.firstChild.nodeValue;
                }
                else if (xmlhttprequest.responseXML.documentElement.tagName == 'error') {
                    error = 'Error retrieving XML response:<br /> ' + xmlhttprequest.responseXML.documentElement.firstChild.nodeValue;
                }
            }
            else {
                if (xmlhttprequest.responseXML.parseError) {
                    error = 'Error parsing XML response:<br /> ' + xmlhttprequest.responseXML.parseError.reason + ' (' + url + ')';
                }
                else {
                    error = 'No valid data in XML response';
                }
            }
        }
        else {
            error = 'The responseXML object was empty';
        }
        var goal = document.getElementById('rss');
        if (!error) {
            var rss = xmlhttprequest.responseXML;
            goal.innerHTML = null;
            parseRSS(rss, goal);
        }
        else {
            goal.innerHTML = error;
        }
    }
}

function parseRSS(rss, goal) {
    var roots = rss.getElementsByTagName('channel');
    for (var i = 0; i < roots.length; i++) {
        var rssdiv = document.createElement('div');
        goal.appendChild(rssdiv);
        rssdiv.setAttribute('id', 'rss' + i);

        var title = null;
        var link = null;
        var items = new Array();
        var image = new Array();
        var index = 0;

        for (var j = 0; j < roots[i].childNodes.length; j++) {
            if (roots[i].childNodes[j].tagName == 'title') {
                title = roots[i].childNodes[j].firstChild.nodeValue;
            }
            else if (roots[i].childNodes[j].tagName == 'link') {
                link = roots[i].childNodes[j].firstChild.nodeValue;
            }
            else if (roots[i].childNodes[j].tagName == 'image') {
                image = parseElement(roots[i].childNodes[j]);
            }
            else if (roots[i].childNodes[j].tagName == 'item') {
                items[index] = parseElement(roots[i].childNodes[j]);
                index++;
            }
        }

        var eltitle = document.createElement('h1');
        rssdiv.appendChild(eltitle);
        var ellink = document.createElement('a');
        eltitle.appendChild(ellink);
        ellink.setAttribute('href', link);
        ellink.setAttribute('title', title);
        ellinktitle = document.createTextNode(title);
        ellink.appendChild(ellinktitle);
        var elimgdiv = document.createElement('div');
        rssdiv.appendChild(elimgdiv);
        elimgdiv.setAttribute('id', 'rss' + i + '-image');
        elimgdiv.setAttribute('class', 'rssimage');
        var elimg = document.createElement('img');
        elimgdiv.appendChild(elimg);
        elimg.setAttribute('src', image['url']);
        elimg.setAttribute('alt', image['title']);
        var elitems = document.createElement('div');
        rssdiv.appendChild(elitems);
        elitems.setAttribute('id', 'rss' + i + '-items');
        elitems.setAttribute('class', 'rssitems');

        for (var j = 0; j < items.length; j++) {
            var elitem = document.createElement('div');
            elitems.appendChild(elitem);
            elitem.setAttribute('id', 'rss' + i + '-item' + j);
            elitem.setAttribute('class', 'rssitem');
            var elitemtitle = document.createElement('h2');
            elitem.appendChild(elitemtitle);
            var elitemlink = document.createElement('a');
            elitemtitle.appendChild(elitemlink);
            elitemlink.setAttribute('href', items[j]['link']);
            elitemlink.setAttribute('title', items[j]['title']);
            var elitemlinktitle = document.createTextNode(items[j]['title']);
            elitemlink.appendChild(elitemlinktitle);
            var elitemdate = document.createElement('span');
            elitem.appendChild(elitemdate);
            elitemdate.setAttribute('id', 'rss' + i + '-item' + j + '-date');
            elitemdate.setAttribute('class', 'rssitemdate');
            var elitemdatetext = document.createTextNode(items[j]['pubDate']);
            elitemdate.appendChild(elitemdatetext);
            var elitemtext = document.createElement('p');
            elitem.appendChild(elitemtext);
            elitemtext.setAttribute('id', 'rss' + i + '-item' + j + '-content');
            elitemtext.setAttribute('class', 'rssitemcontent');
            var elitemtexttext = document.createTextNode(items[j]['description']);
            elitemtext.appendChild(elitemtexttext);
        }
    }
}

function parseElement(root) {
    var element = new Array();
    for (var i = 0; i < root.childNodes.length; i++) {
        if (root.childNodes[i].tagName) {
            element[root.childNodes[i].tagName] = root.childNodes[i].firstChild.nodeValue;
        }
    }

    return element;
}



/* Onload */
var feed = 'get.php?' + window.location.search.substr(1);
var xmlhttprequest = createHTTPHandler();
window.onload = init;

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.