PDA

View Full Version : RSS Newsfeed


dallen24
07-27-2005, 02:48 PM
Hi. I wrote a simple asp script to read various rss newsfeeds. I got one newsfeed, however, that comes back with a runtime error ['null' is null or not an object]. Here's the code, distilled to the point of error:

//
function xml_load(src) {
try {
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
xmlhttp.open("GET",src,false)
xmlhttp.send()
xmlDoc = xmlhttp.responseXML
}
catch (e) {
alert(e)
return null
}
return xmlDoc
}
//
function xml_start() {
var xmlDoc = xml_load("http://stardate.org/feeds/rss.xml")
if (typeof(xmlDoc) != "object") return ''
var root = xmlDoc.documentElement
if (typeof(root) != "object") return ''
if (typeof(root.childNodes) != "object") return '' // bombs here
}


I'm at a loss. How can you have a xmlDoc object having no childNodes? Is there something wrong with the file http://stardate.org/feeds/rss.xml? No encode statement? Any advice would be appreciated...Dennis

shyam
07-28-2005, 12:44 PM
there is one important point you must always keep in mind when you are using XMLHttp which is you can only access content that is served from the same server that served the page which makes the request. which effectively means thats the only feeds you can access are your own (unless u have some server-side coding).

this said, the problem you are facing is because .childNodes returns an array so instead of checking if the type is an object you can better check the length to see if it has any child nodes at all.

Roelf
07-28-2005, 03:18 PM
there is one important point you must always keep in mind when you are using XMLHttp which is you can only access content that is served from the same server that served the page which makes the request. which effectively means thats the only feeds you can access are your own (unless u have some server-side
With a simple javascript xmlhttp object, i can access content from other servers too, not only the one which served the requesting page.

dallen24
07-29-2005, 02:20 AM
I tweeked xml_load() :

//
function xml_load(src) {
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
xmlhttp.open("GET",src,false)
xmlhttp.send()
xmlDoc = xmlhttp.responseXML
// var szResponse = xmlhttp.responseText
// xmlDoc.loadXML(szResponse)
if (typeof(xmlDoc) != "object") return 'no object defined'
if (xmlDoc.parseError.errorCode != 0) {
var x = ''
x += 'Error in File: ' + xmlDoc.parseError.url + '<br>'
x += 'Error on Line: ' + xmlDoc.parseError.line + '<br>'
x += 'Error Code : ' + xmlDoc.parseError.errorCode + '<br>'
x += 'Error Reason : ' + xmlDoc.parseError.reason
return x
}
return xmlDoc
}

Something interesting. On the stardate.org xml file, if I change the code to:

// xmlDoc = xmlhttp.responseXML
var szResponse = xmlhttp.responseText
xmlDoc.loadXML(szResponse)

...it works. But using this code breaks other newsfeeds (DTD not found). Is there a way to test for error and use one way, then the other?

dallen24
07-29-2005, 06:13 AM
Hi. After much trial and error, I found the code that makes all the newsfeeds happy:

//
function xml_load(src) {
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
xmlhttp.open("GET",src,false)
xmlhttp.send()
xmlDoc = xmlhttp.responseXML
if (typeof(xmlDoc) != "object" || xmlhttp.GetResponseHeader("Content-Type") != "text/xml") {
var szResponse = xmlhttp.responseText
xmlDoc.loadXML(szResponse)
}
if (typeof(xmlDoc) != "object") return 'no object defined'
if (xmlDoc.parseError.errorCode != 0) {
var x = ''
x += 'Error in File: ' + xmlDoc.parseError.url + '<br>'
x += 'Error on Line: ' + xmlDoc.parseError.line + '<br>'
x += 'Error Code : ' + xmlDoc.parseError.errorCode + '<br>'
x += 'Error Reason : ' + xmlDoc.parseError.reason
return x
}
return xmlDoc
}