This project is archived and is in readonly mode.

#441 ✓wontfix
Jasper

responseXML not working with $ in IE

Reported by Jasper | October 27th, 2008 @ 12:52 PM

passing a valid XML doc to the second argument of onSuccess doesn't work with IE and I can't get it to work at all.

javascript, the xml and comments of what the alert box shows:

http://pastebin.com/f38452d64

Comments and changes to this ticket

  • epplestun

    epplestun October 28th, 2008 @ 07:21 AM

    Hi Jasper, you need to do this on IE (it works on Firefox too):

    ...

    onSuccess : function(text, xml) { var xml = xml.documentElement;

    for(var i = 0; i < xml.childNodes.length; i++) { var node = xml.childNodes[i];

    var id = node.getAttribute('value'); // Get attribute value

    var value = node.firstChild.nodeValue; // Get node value }

    }

    ...

  • Jasper

    Jasper October 28th, 2008 @ 08:00 AM

    oh right, that's less than ideal. Are there plans to implement mootools-style selectors with XML in IE, or is it simply a no-go? I know using something like JSON can give me similar results but I prefer XML!

  • epplestun

    epplestun October 28th, 2008 @ 10:46 AM

    Hi again Jasper!

    It's imposible to made $(xml) beacuse XML is not in the DOM structure.

    A good idea would be to return an object that mapease the original XML ... otherwise the only way to work with the XML is natively.

  • Tom Occhino

    Tom Occhino October 28th, 2008 @ 01:25 PM

    • Milestone cleared.
    • State changed from “new” to “hold”
    • Assigned user changed from “Valerio” to “Tom Occhino”

    Jasper, like epplestun said, it's impossible to give XML Element nodes prototypes in IE. We will hopefully be introducing an XML.js and Request.XML.js in 1.3 which will make working with XML documents a little bit easier...

    but i definitely wish we could just use all the Element Dom methods like... getNext, getChildren, getElements... how amazing would it be to use selectors in an XML Document... :/ Maybe we will be able to provide generic versions or something...

  • epplestun

    epplestun November 13th, 2008 @ 11:47 AM

    Hi again!!!

    I did a small class to transform responseXML to DOM structure, here the class:

    var XML = new Class({

      initialize: function(options){
         this.options = options;
         return this.getXML();
      },
    
       getXML : function() {
         var xml = this.options.xml.documentElement;
    
       return this.createXML(xml);
       },
    
       createXML : function(xml, parent) {
          if(!parent) {
            parent = new Element(xml.nodeName);
         }
    
       if(xml.childNodes.length) {
            for(var i = 0; i < xml.childNodes.length; i++) {
               if(xml.childNodes[i].nodeType == 1) { // Element type
                  // NodeName
                  var el = new Element(xml.childNodes[i].nodeName);
    
                  // Attributes
                  if(xml.childNodes[i].attributes.length) {
                     for(var j = 0; j < xml.childNodes[i].attributes.length; j++) {
                        var property = xml.childNodes[i].attributes[j].nodeName;
                        var value    = xml.childNodes[i].attributes[j].nodeValue;
                        el.setProperty(property, value);
                     }
                  }
    
                  // Value
                  el.set({
                     value : xml.childNodes[i].firstChild.nodeValue
                  })
    
                  parent.adopt(el);
    
                  var son = xml.childNodes[i];
                  if(son.childNodes.length) {
                     this.createXML(son, el);
                  }
               }
            }
         }
         return parent;
       }
    
    });
    
    

    An example of the use:

    window.addEvent('domready', function() {

       var x = new Request({
          'url' : 'test03.xml',
          'onSuccess' : function(text, xml) {
          var a = new XML({
             xml : xml
          });
    
    
          console.log(a.getChildren());
          }
      }).send();
    
    

    });

  • Vigvari

    Vigvari January 22nd, 2009 @ 01:01 PM

    Hy

    I expanded epplestun's script a little bit. And now xml documents can be used with the Element Dom methods. It worked for me under firefox 3 and ie 6.

    
    Request.XML = new Class({
    	Extends: Request,
    
    	success: function(text, xml){
    		if(Browser.Features.xpath) {
    			xml.selectNodes = function(xpath){
    				var nodes = [];	
    				var result = this.evaluate(xpath, this, this.createNSResolver(xml.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
    				if (result){
    					var node = result.iterateNext() ;
    					while(node){
    						nodes.push(node);
    						node = result.iterateNext();
    					}
    				} 
    				return nodes;
    			}	
    			xml.selectSingleNode = function(xpath){
    				var result = this.evaluate(xpath, this, this.createNSResolver(this.documentElement), 9, null);
    				if (result && result.singleNodeValue) return result.singleNodeValue ;
    				else return [] ;
    			}
    		// ie
    		}else{
    			xml = this.createXML(xml.documentElement);
    		}
    		this.onSuccess(xml, text);
    	},
    	
    	createXML : function(xml, parent) {
    		if(!parent) {
    			parent = new Element(xml.nodeName);
    		}
    	
    		if(xml.childNodes.length) {
    			for(var i = 0; i < xml.childNodes.length; i++) {
    				if(xml.childNodes[i].nodeType == 1) { // Element type
    					// NodeName
    					var el = new Element(xml.childNodes[i].nodeName);
    				
    					// Attributes
    					if(xml.childNodes[i].attributes.length) {
    						for(var j = 0; j < xml.childNodes[i].attributes.length; j++) {
    							var property = xml.childNodes[i].attributes[j].nodeName;
    							var value    = xml.childNodes[i].attributes[j].nodeValue;
    							el.setProperty(property, value);
    						}
    					}
    	
    					// Value
    					el.set({'text' : xml.childNodes[i].firstChild.nodeValue});
    	
    					parent.adopt(el);
    	
    					var son = xml.childNodes[i];
    					if(son.childNodes.length) {
    						this.createXML(son, el);
    					}
    				}
    			}
    		}
    		return parent;
    	}
    });
    
  • epplestun

    epplestun January 22nd, 2009 @ 03:17 PM

    Hi Vigvari!

    Good job my friend!!! I test on Firefox 2 and work fine :-)

  • josh vermette

    josh vermette August 6th, 2009 @ 07:18 AM

    Awesome extension, it just solved some major inconveniences I was running into! Fwiw I "fixed it up" a tiny bit:
    1) attributes weren't being set in the top-level documentElement node (just ie I think)
    2) there was an inconsistency in that for IE the xml object returned was itself the documentElement, but for Firefox etc the xml contained the documentElement as xml.documentElement

    also while I was in there I added some holder vars for a tiny increase in efficiency in the loops

    Thanks again for the helpful extension! Anyone know if this is being added to the code base? It seems necessary, since for ie and apparently Safari the Request.HTML extension drops non-html tags, and the base Request class seems impossibly hard to work with for inspecting XML.

    Updated code follows:

    Request.XML = new Class({

    Extends: Request,
    
    success: function(text, xml){
        if(Browser.Features.xpath) {
            xml.selectNodes = function(xpath){
                var nodes = []; 
                var result = this.evaluate(xpath, this, this.createNSResolver(xml.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
                if (result){
                    var node = result.iterateNext() ;
                    while(node){
                        nodes.push(node);
                        node = result.iterateNext();
                    }
                } 
                return nodes;
            }   
            xml.selectSingleNode = function(xpath){
                var result = this.evaluate(xpath, this, this.createNSResolver(this.documentElement), 9, null);
                if (result && result.singleNodeValue) return result.singleNodeValue ;
                else return [] ;
            }
        }
        else {
            // ie
            xml = { documentElement: this.createXML(xml.documentElement) };
        }
    
        this.onSuccess(xml, text);
    },
    
    createXML : function(xml, parent) {
        if (! parent) parent = new Element(xml.nodeName);
    
        var tmp;
        for(var j = 0; j < xml.attributes.length; j++) {
            tmp = xml.attributes[j];
            parent.setProperty(tmp.nodeName, tmp.nodeValue);
        }
    
        if (! xml.childNodes.length) return parent;
    
        var i, j, currChildNode;
        for(i = 0; i < xml.childNodes.length; i++) {
            currChildNode = xml.childNodes[i];
    
            if (currChildNode.nodeType == 1) { // Element type
                // NodeName
                var el = new Element(currChildNode.nodeName);
    
                // Attributes
                if(currChildNode.attributes.length) {
                    for(j = 0; j < currChildNode.attributes.length; j++) {
                        tmp = currChildNode.attributes[j];
                        el.setProperty(tmp.nodeName, tmp.nodeValue);
                    }
                }
    
                // Value
                el.set({'text' : currChildNode.firstChild.nodeValue});
    
                parent.adopt(el);
    
                if (currChildNode.childNodes.length) this.createXML(currChildNode, el);
            }
        }
    
        return parent;
    }
    

    });

  • Christoph Pojer

    Christoph Pojer November 9th, 2010 @ 07:25 PM

    • State changed from “hold” to “wontfix”
    • Milestone order changed from “0” to “0”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Tags

Pages