View Full Version : javascript and XML

07-17-2007, 07:03 AM
Hi all..

I am having problems with coding object oriented javascript along with xml in IE..

My xml is like this:
</BOOK> and so on...

I do the following:
bookObject = data.getElementsByTagName("BOOK");
/* data is the XML from server through responseXML */
bookObject.authorList = bookObject.getElementsByTagName("AUTHOR");

the second javascript line causes no problems in firefox but IE says
"Object doesn't support property or method."
Someone please tell me what am i doing wrong.. This thing is driving me crazy..

Thanks in advance!

07-17-2007, 07:26 AM
the getElementsByTagName() returns a collection, so that if you want to create a new property for one of the objects in that collection, you need an index.

That means

bookObject.authorList = bookObject.getElementsByTagName("AUTHOR");

is not a correct statement, You must have something like

bookObject[i].authorList = bookObject[i].getElementsByTagName("AUTHOR");

in a loop

07-17-2007, 03:29 PM
Sorry, made a mistake..

the first line is :
bookObject.authorList = bookObject.getElementsByTagName("AUTHOR")[0];

I used IE script debugger to see if bookObject.getElementsByTagName("AUTHOR")[0] exists and it does.. so somehow the authorList property is not getting created or something like that.. Just to re-iterate, it works fine in firefox and not in IE 6.. Please help, thanks..

07-17-2007, 11:32 PM
You were not attentive... bookObject is also a collection, as it is created with the same getElementsByTagName() method as well, so that it needs an index...

07-18-2007, 03:49 PM
I'm so sorry! I'm making too many mistakes.

Here's the code
bookObject = data.getElementsByTagName("BOOK")[0];
/* data is the XML from server through responseXML */
bookObject.authorList = bookObject.getElementsByTagName("AUTHOR");

and Kor, bookObject is not a collection.. its a single element.. isn't it the first BOOK element of the XML? because i have said data.getElementsByTagName("BOOK")[0] :confused:

07-18-2007, 09:23 PM
Ok, now can we see the whole picture? What's wrong now?

rnd me
07-19-2007, 03:38 AM
try using this instead of document.getElementsByTagName()
it returns a true array instead of a collection, saves a lot of repetitive coding...

function tags(tid) {
var ob=document.getElementsByTagName(tid);
var r = [], mx = ob.length;
for (var z = 0; z < mx; z++)
{r[z] = ob[z];}
return r;
}//end function 'tags

// or from a subtag, wrap your return with an array maker:
function obVals(ob) {var r = [], mx = ob.length;for (var z = 0; z < mx; z++) {r[z] = ob[z];}return r;}

var rez=someDiv.getElementsByTagName(tid) //BECOMES:

var rez=obVals(someDiv.getElementsByTagName(tid))

and the result once again is now a true array.

08-10-2007, 05:55 AM
Hi all!

I got the solution a while ago, but forgot to update you guys..

The problem was IE is more strict with its object creation.. Firefox seems to be more lenient somehow.. The object(s) that were storing the XML data were completely out of gear. Learned my lesson though..

Appreciate the help!