开发者

No root node detected when loading XML in IE using Javascript

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 XMLHtt开发者_如何学JAVApRequest!='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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜