views:

60

answers:

4

Hi,

I'm using the following parser to parse xml

function parseXML(text) {
    var doc;

    if(window.DOMParser) {
        var parser = new DOMParser();
        doc = parser.parseFromString(text, "text/xml");
    }
    else if(window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(text);
    }
    else {
        throw new Error("Cannot parse XML");
    }

    return doc;
}

I can't understand why it isn't working on my XML document, obtained via AJAX.

Result via AJAX request:

X-Powered-By    PHP/5.2.11
Content-Length  887
Keep-Alive  timeout=5, max=95
Connection  Keep-Alive
Content-Type    text/xml

<?xml version="1.0" encoding="UTF-8"?>
<xml_test>wont work!</xml_test>

Test Code:

    var xml = parseXML(data);
    $(xml).find("xml_test").each(function()
    {
        console.info('found xml_test... never happen..');
    });

But if I use it like this it works nicely!

    var data = '<xml_test>works</xml_test>';
    var xml = parseXML(data);
    $(xml).find("xml_test").each(function()
    {
        alert('this works!');
    });

I know that this is a specific question but I would appreciate your help and/or suggestions...

Thanks in advance Pedro

A: 

If you are using jQuery (as your test code suggests), you can simply pass the xml to it.

var xml = $(data);
elusive
Hi elusive, but we shouldn't avoid parsing xml with jQuery?
Pedro Gil
@Pedro Gil: As jQuery is extensively tested, we can be quite sure that is parses the XML right. It uses some tricks to do the parsing via the browser instead of parsing it manually. I think this is better than trying to parse it yourself.
elusive
@elusive: no, jQuery doesn't parse XML. It pretends it's HTML using `innerHTML`, which is not reliable. See http://stackoverflow.com/questions/2908899/jquery-wont-parse-xml-with-nodes-called-option
Tim Down
@elusive: wrong. jQuery states that [`$` is not to be used to parse an xml string](http://api.jquery.com/jQuery/#jQuery2). It can be used however to wrap an already parsed xml document.
Roatin Marth
+3  A: 

If you use jQuery to request your resource, you should already get XML DOM document in case it was served with text/xml mime-type. Thus no need to parse.

Sergey Ilinsky
+1  A: 

I use this function and gives me good result:

var myLoadXml = function(s){

  var objxml = null;

  if(document.implementation && document.implementation.createDocument) {

     var objDOMParser = new DOMParser();
     objxml = objDOMParser.parseFromString(s, "text/xml");

  } else if (window.ActiveXObject) {

     objxml = new ActiveXObject('MSXML2.DOMDocument.3.0');
     objxml.async = false;
     objxml.loadXML(s);

  }

  return objxml;
};


var xml = myLoadXml(data);

$(xml).find("xml_test").each(function()
{
    console.info('found xml_test... never happen..');
});

EDIT Example

** EDIT II **

function parseXML(text) {
    var doc;

    if (typeof text == 'object'){ // check type of text
        return text; 
    }

    if(window.DOMParser) {
        var parser = new DOMParser();
        doc = parser.parseFromString(text, "text/xml");
    }
    else if(window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(text);
    }
    else {
        throw new Error("Cannot parse XML");
    }

    return doc;
}
andres descalzo
This is pretty much what the OP is doing already. AND IT'S NOT WORKING.
Roatin Marth
I made an example. and it works
andres descalzo
Yeah it should definitely work. I think it is not working for the OP because he is passing an already parsed DOM to his `parseXML` function (which is the same as your `myLoadXml` function).
Roatin Marth
+1  A: 

If you're getting your XML via Ajax, there's no need to parse it because the browser will do it for you. Simply use the responseXML property of the XMLHttpRequest object, which will give you an XML document object. jQuery wraps this using "xml" for the dataType:

 $.ajax({
     type: "GET",
     url: "foo.xml",
     dataType: "xml",
     success: function(xml) {
         alert(xml.documentElement.nodeName);
     }
 });
Tim Down