views:

237

answers:

1

I am attempting to parse a XML file using Javascript and I'm running into issues on IE7.

If I have this code:

function LoadXml()
{
    var xmlPath = document.getElementById("hsTreeviewXmlPath").value;

    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      alert("here1");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       alert("here2");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
            alert("here3");
        } catch (e) {
            xmlhttp=false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
            alert("here4");
        } catch (e) {
            xmlhttp=false;
        }
    }

    xmlhttp.open("GET",xmlPath,false);
    xmlhttp.send(null);

    var xmlDoc = xmlhttp.responseXML;

    ParseXml(xmlDoc);
}

function ParseXml(xmlDoc)
{
    var root = xmlDoc.documentElement;
    alert(root);

    for(i=0; i< root.childNodes.length; i++)
    {
        var node = root.childNodes[i];
        if(node.nodeType ==1) //process element nodes (type 1)
        {
            if(node.childNodes.length > 1)
            {
                CreateChildren("hsTreeview",node);
            }
            else
            {
                AddNode("hsTreeview", node);
            }
        }
    }
}

In FF and Chrome this works correctly, adding nodes as it should, but on IE7 I get a scripting error and the specific error:

Object required

This gives a line number relating to the line:

for(i=0; i< root.childNodes.length; i++)
{

The alert box tells me that in IE, the root node, which is being populated from xmlDoc.documentElement is null.

I have confirmed, using the alerts here1 etc that in IE7 it is using the xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); object.

Is there some way to fix this up as it's really frustrating?

+1  A: 

Make sure that the XML file is served with the proper text/xml Mime Type.

Edit Bellow:

Also make sure that the XML file is served through an http server from the same domain and port as the web page. IE will prevent a web page from accessing URLs outside their originating domain or files on your local computer for security reasons.

Alexandre Jasmin
and how does one do that? This is just a file sitting on a Sharepoint directory. In my test case (off the HDD), it still doesn't work.
Alastair Pitts
@Alastair Is it possible to rename the file in `var xmlPath` with an .xml extension and load the web page through http?
Alexandre Jasmin
the file is designated an XML file, the path ends in ".xml". What exactly do you mean by load through http?
Alastair Pitts
@Alastair Open the web page using an http URL as opposed to loading it from the local file system (which I assume you mean by "off the HDD"). Also try loading the .xml file through an http URL. It should get syntax coloring in ie if it's recognized as XML
Alexandre Jasmin
indeed this seems to have worked. Weird. Do you have an explanation why? Now to work out why the child elements aren't being shown.
Alastair Pitts