?
Solved

XML in Internet Explorer - Problems using the For In loop

Posted on 2005-03-30
11
Medium Priority
?
4,529 Views
Last Modified: 2008-01-09
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].nodeName == '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].nodeName == '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?
0
Comment
Question by:kibatsu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 13668652
does this work?

for (i in xml.childNodes) {
0
 
LVL 2

Author Comment

by:kibatsu
ID: 13668702
No. I still get the same error; "Object doesn't support this action".
0
 
LVL 1

Expert Comment

by:radhika1306
ID: 13669199
function process(xml) {
  for (var i =0; i < xml.childNodes.length; ++i) {
    if (xml.childNodes.items(i).tagname == 'sponge') {
      alert('you got a sponge');
    }
  }
}


try this .
can u please send us ur xml.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Zvonko
ID: 13669203
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.childNodes["length"].nodeName == 'sponge') {

Do you see the difference betwean looping trough numerc indexes givven by length and all properties givven by "in" loop?

0
 
LVL 2

Author Comment

by:kibatsu
ID: 13677446
> 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.
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 1000 total points
ID: 13677716
You said you used xmlhttp to return the data.  That is a string, not a nodeList.  if you want a nodeList in IE you have to use XMLDOM.

Cd&
0
 
LVL 2

Author Comment

by:kibatsu
ID: 13677993
Ahh, now that's interesting. So even if I access the .responseXML property of the XMLHTTPP object its still a string?
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 13679680
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&
0
 
LVL 2

Author Comment

by:kibatsu
ID: 15888270
Thanks cem - no satisfactory answer was posted for this - should probably have cleaned it up myself.

Cheers!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question