views:

215

answers:

3

I am loading some XML via Ajax.

here is a snippet of the script:

ajaxRequest.onreadystatechange = function()
    {
     if(ajaxRequest.readyState == 4)
     {
      document.getElementById('loading').innerHTML = 'done';
      google_xml = ajaxRequest.responseXML;
      document.getElementById('xml').innerHTML = 'XML: '+google_xml.toXMLString();
     }
    }

The page I am loading is an XML file, the file loads because if I alert(ajaxRequest.responseText); I see it. But I am not sure how to process it as XML I thought the above script would put the XML as a string into the < div id="xml" >< /div > element but it doesnt.

I have used E4X to process XML a lot in Actionscript but never in JS, can someone give me a hint? Thanks!

+2  A: 

I'm not sure what browser you are using but Firefox is currently the only browser with good support for E4X.

Though, to expand on this answer, you may want to be aware of the alternative:

There's plenty of support for parsing XML in the browser, just not with E4X. You may want to use the jQuery and use dataType:XML. See xml.com/pub/a/

altCognito
Really? thats crappy..it doesn't matter what browser I am on, this is for my users not for me...this puts a big dent in my whole application. :(
John Isaacks
There's plenty of support for parsing XML in the browser, just not with E4X. You may want to use the jQuery and use dataType:XML. See http://www.xml.com/pub/a/2007/10/10/jquery-and-xml.html
altCognito
A: 

Just use xhr.responseXML which is already an XML object with DOM support after the request is fullfilled. If the browser supports the XmlHttpRequest object, this should work directly. Also, IIRC, you can use jQuery to wrap the object and use it's own find/each methods for traversing the object.

Tracker1
A: 

For those who follow after with this same question:

var x = new XML('<xml>string value</xml>');

...now your x value is an xml object that you can manipulate with E4X!

langsor