0

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

Pedro Gil
  • 532
  • 2
  • 7
  • 18
  • 1
    My guess is that you try to pass already parsed XML (like responseXML document or something). Can you show the code that initializes data variable to be passed here? var xml = parseXML(data); $(xml).find("xml_test").each(function() { console.info('found xml_test... never happen..'); }); – Sergey Ilinsky Sep 30 '10 at 13:33
  • maybe the problem is related with doc = parser.parseFromString(text, "text/xml"); ?! – Pedro Gil Sep 30 '10 at 13:34
  • Sergey, $.ajax({ url: '...' type: 'POST', dataType: 'xml', data: formulario.serialize(), timeout: 15000, success: function(data) {.........} – Pedro Gil Sep 30 '10 at 13:38
  • 3
    That's right, so you get your XMLDOM document here - check it: function(data) {alert(data.nodeType)} – Sergey Ilinsky Sep 30 '10 at 13:41

4 Answers4

3

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
  • 31,255
  • 9
  • 54
  • 56
1

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
  • 14,887
  • 13
  • 64
  • 115
  • This is pretty much what the OP is doing already. AND IT'S NOT WORKING. – Roatin Marth Sep 30 '10 at 14:07
  • 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 Sep 30 '10 at 17:09
1

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
  • 318,141
  • 75
  • 454
  • 536
0

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

var xml = $(data);
jwueller
  • 30,582
  • 4
  • 66
  • 70
  • 2
    Hi elusive, but we shouldn't avoid parsing xml with jQuery? – Pedro Gil Sep 30 '10 at 13:36
  • @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. – jwueller Sep 30 '10 at 13:46
  • 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 Sep 30 '10 at 14:05
  • 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 Sep 30 '10 at 14:05