kibatsu
asked on
XML in Internet Explorer - Problems using the For In loop
At present I am working on a web application which processes an XML file retreived from the server using the XMLHTTP object. I'm looping through the childNodes in the XML file and testing the nodeName property of each so I can determine which functions I need to pass each node to.
For example, I pass the XML object to a function called process:
function process(xml) {
for (var i in xml.childNodes) {
if (xml.childNodes[i].nodeNam e == 'sponge') {
alert('you got a sponge');
}
}
}
This works correctly in Firefox, but I have a problem in IE. I have an error which says "Object doesn't support this action", referring to the line where I start my For In loop.
If I do this instead (a normal For loop):
function process(xml) {
for (var i =0; i < xml.childNodes.length; ++i) {
if (xml.childNodes[i].nodeNam e == 'sponge') {
alert('you got a sponge');
}
}
}
It does work correctly in IE. I don't what to have to do this since it is more code and means modifying all the For In loops I am using throughout the application.
Anyone know why IE is freaking out over the For In loop?
For example, I pass the XML object to a function called process:
function process(xml) {
for (var i in xml.childNodes) {
if (xml.childNodes[i].nodeNam
alert('you got a sponge');
}
}
}
This works correctly in Firefox, but I have a problem in IE. I have an error which says "Object doesn't support this action", referring to the line where I start my For In loop.
If I do this instead (a normal For loop):
function process(xml) {
for (var i =0; i < xml.childNodes.length; ++i) {
if (xml.childNodes[i].nodeNam
alert('you got a sponge');
}
}
}
It does work correctly in IE. I don't what to have to do this since it is more code and means modifying all the For In loops I am using throughout the application.
Anyone know why IE is freaking out over the For In loop?
ASKER
No. I still get the same error; "Object doesn't support this action".
function process(xml) {
for (var i =0; i < xml.childNodes.length; ++i) {
if (xml.childNodes.items(i).t agname == 'sponge') {
alert('you got a sponge');
}
}
}
try this .
can u please send us ur xml.
for (var i =0; i < xml.childNodes.length; ++i) {
if (xml.childNodes.items(i).t
alert('you got a sponge');
}
}
}
try this .
can u please send us ur xml.
I do not understand what you expect from Experts???
You do not want to change the loop to the obvious solution and expect some shorter workaround?
Did you test what properties you get in the for loop with the "in"?
So as you see you get also the childNodes length as a property name beside the indexes, so does this statement make no sense:
if (xml.childNodes["length"]. nodeName == 'sponge') {
The workaround would be to test first wheter a node with the i index exist, but that is obviously longer the the stright way of looping trough the index given by length and not looping trough all properties of childNodes:
if (xml.childNodes["length"]& &xml.child Nodes["len gth"].node Name == 'sponge') {
Do you see the difference betwean looping trough numerc indexes givven by length and all properties givven by "in" loop?
You do not want to change the loop to the obvious solution and expect some shorter workaround?
Did you test what properties you get in the for loop with the "in"?
So as you see you get also the childNodes length as a property name beside the indexes, so does this statement make no sense:
if (xml.childNodes["length"].
The workaround would be to test first wheter a node with the i index exist, but that is obviously longer the the stright way of looping trough the index given by length and not looping trough all properties of childNodes:
if (xml.childNodes["length"]&
Do you see the difference betwean looping trough numerc indexes givven by length and all properties givven by "in" loop?
ASKER
> I do not understand what you expect from Experts???
Expertise obviously, not antagonism.
>You do not want to change the loop to the obvious solution and expect some shorter workaround?
No I don't want to change the loop if I can avoid it, because I have used the For In loop extensively in the XML processing code. If there is no other solution I would quite happily use a normal For loop, but I thought that at least I could find out for certain by asking here.
> Did you test what properties you get in the for loop with the "in"?
Yes, I get the same error "Object doesn't support this action".
> So as you see you get also the childNodes length as a property name beside the indexes, so does this
> statement make no sense:
> if (xml.childNodes["length"]. nodeName == 'sponge') {
I understand that. That is not my problem here. Simply, I am unable to loop through the properties of the XML object in IE using the For In loop.
even if I do something like
for (i in xml) {
alert(i)
}
I would expect the code to alert the name of each property and method. It doesn't even do that. I get the "..doesn't support action.." error.
Expertise obviously, not antagonism.
>You do not want to change the loop to the obvious solution and expect some shorter workaround?
No I don't want to change the loop if I can avoid it, because I have used the For In loop extensively in the XML processing code. If there is no other solution I would quite happily use a normal For loop, but I thought that at least I could find out for certain by asking here.
> Did you test what properties you get in the for loop with the "in"?
Yes, I get the same error "Object doesn't support this action".
> So as you see you get also the childNodes length as a property name beside the indexes, so does this
> statement make no sense:
> if (xml.childNodes["length"].
I understand that. That is not my problem here. Simply, I am unable to loop through the properties of the XML object in IE using the For In loop.
even if I do something like
for (i in xml) {
alert(i)
}
I would expect the code to alert the name of each property and method. It doesn't even do that. I get the "..doesn't support action.." error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ahh, now that's interesting. So even if I access the .responseXML property of the XMLHTTPP object its still a string?
IE does not directly support XML, that is why yu have to use activex for xml related operations. More advanced browsers like Mozilla/FF can use the whole document as a nodeList because build everything as a dom object. IE can't do that very well because it has to screw up the DOM to support proprietary legacy objects like document.all
Try bringing in the data with XMLDOM and then see if the nodeList you get in the documentElement.childNodes property of the returned object allows you to d it the way you want.
No guarantees of course. With IE what will or will not work within standards is always a crap shoot.
Cd&
Try bringing in the data with XMLDOM and then see if the nodeList you get in the documentElement.childNodes
No guarantees of course. With IE what will or will not work within standards is always a crap shoot.
Cd&
ASKER
Thanks cem - no satisfactory answer was posted for this - should probably have cleaned it up myself.
Cheers!
Cheers!
for (i in xml.childNodes) {