0

I am using the below code to iterate through multiple nodes of XML but it seems not working

var xmlDat = $.parseXML($('#xmlText').val()); 
// Here I am reading an XML content on paste event of xmlText
var xml = $(xmlDat);
var path = xml.find('ProjectID').text();
xml.find('a,b,c,d').each(function(){
{
       // Giving unwanted results
}

but if use like below

xml.find('a').each(function(){
{
       // Giving results
}

Please let me know how can i correct the code to make this working in above case also...

Exception
  • 8,111
  • 22
  • 85
  • 136
  • 2
    Please provide an example of the XML document, the result you get and the result you want to get. [A quick test](http://jsfiddle.net/fkling/bUvPB/) shows that it should work. – Felix Kling Dec 12 '11 at 13:33
  • @FelixKling kling It works perfect in google Chrome but not in IE. I am working on IE. I am sorry for not mentioning the browser before. Check here please http://jsfiddle.net/bUvPB/1/ – Exception Dec 12 '11 at 13:41
  • 2
    Btw I don't think you are supposed to pass `xmlDat` to jQuery again. It should already be a jQuery object. I don't have access to IE, so I cannot really help, but have you had a look at [this question](http://stackoverflow.com/questions/562283/jquery-find-doesnt-return-data-in-ie-but-does-in-firefox-and-chrome)? – Felix Kling Dec 12 '11 at 13:46
  • @FelixKling Just seen that Question and that is an excellent Question. But as I am not loading any data from Ajax and I am just parsing a string I am bit confused to correlate my Question with that provided answer. – Exception Dec 12 '11 at 13:51
  • @FelixKling Yes, you are supposed to [pass it to jQuery again](http://api.jquery.com/jQuery.parseXML/): _"This document can then be passed to `jQuery` to create a typical jQuery object that can be traversed and manipulated."_ `parseXML()` just returns a standard XML DOM `Document`. – Phrogz Dec 12 '11 at 13:53

2 Answers2

2

It works fine in ie9.

In ie7 however you will need to use filter instead of find since ie shaves of the outer element, so <root><a></a><b></b></root> effectively becomes <a></a><b></b>.

All in all

xml.find('a,b,c,d').each(function(){
{
       // works in modern browsers
}


xml.filter('a,b,c,d').each(function(){
{
       // works in old ie versions
}

Example here: http://jsfiddle.net/bUvPB/5/

Alternativly, to make the same code work in all browsers, wrap your xml in a dummy element first, and then use find always, that way you don't need browser sniffing and other ugly tools to be cross browser compatible.

$('<div></div>').append(xml).find('a,b,c,d')

example: http://jsfiddle.net/bUvPB/25/

Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68
1

I tried wrapping the XML document in an HTML element and it worked for me in IE7 and IE8. Your original code fragment worked fine in IE9 and in other browsers. Here is the code:

var xmlDat = "<div><root><a></a><b></b></root></div>";
var xml = $(xmlDat);
xml.find('a,b,c,d').each(function(){
    alert('Hi'); 
});

Here is the jsfiddle link http://jsfiddle.net/AyRkX/

epignosisx
  • 6,152
  • 2
  • 30
  • 31