Forum Moderators: open

Message Too Old, No Replies

Reading xml via Ajax but ie missing some data

         

cgiscripts4u

6:38 pm on Nov 14, 2008 (gmt 0)

10+ Year Member



I have an xml file in the following with the following data:

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
.
.
<item>
<description>29443</description>
<link>a url</link>
<guid>same url</guid>
<title>a title</title>
</item>
.
.
.
</channel>
</rss>

There are a number of items within the xml feed. The feed is being read using ajax, everything works fine in Firefox but in Internet Explorer the description of each element is not being read, or is being read as empty.

Here is the bit of code being used to fetch the item:


var results=xhrlatest.getElementsByTagName("rss");
var channel = results[0].getElementsByTagName("channel");
var item=channel[0].getElementsByTagName("item");
for(var i=0;i<item.length;i++){
var dsc='';
var title='';
var url='';
for (var j=1;j<item[i].childNodes.length;j++) {
if (item[i].childNodes[j].nodeName=='description') dsc=item[i].childNodes[j].firstChild.nodeValue;
else if (item[i].childNodes[j].nodeName=='link') url=item[i].childNodes[j].firstChild.nodeValue;
else if (item[i].childNodes[j].nodeName=='title') title=item[i].childNodes[j].firstChild.nodeValue;
}
//do stuff with the three variables
}

the variables set correctly when using firefox but the 'dsc' variable is remaining empty when using Internet Explorer 7.

I am a novice when it comes to Javascript/Ajax but this is really bugging me. Any idea's what I am doing wrong?

Fotiman

7:24 pm on Nov 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's just a guess, but your second loop is starting at 1 instead of 0 for some reason. That my be part of the problem.

Also, you are getting the firstChild's nodeValue of each 'node'. I think I remember something about it being possible that your node contains multiple TEXT node children with, for example, a new line character being the only one in the first child. In other words:

<description>
foo
</description>

That is actually:

<description>\n
foo\n
</description>

Where the first text node contains only a newline. Not sure if this is what you're seeing or not, but rather than relying on the firstChild containing your data, you may want to either check all of the child nodes and get each that has a node type of TEXT, or perhaps there is a method (innerText or something like that) that would get the text value?

Also, you might try doing getElementsByTagName instead of your second loop:

d = item[i].getElementsByTagName('description');
u = item[i].getElementsByTagName('link');
t = item[i].getElementsByTagName('title');

Just a thought.