开发者

Safari Won't Work With Microsoft.XMLDOM ActiveX Object

I'm designing a client side script that will read an XML file and display it, like this:

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

function loadXML(xmlFile) {
    xmlDoc.async = "false";
    xmlDoc.onreadystatechange = verify;
    xmlDoc.load(xmlFile);
}

function verify() {
    if(xmlDoc.readyState != 4) {
        return false;
    }
}

function traverse(tree) {
    if(tree.hasChildNodes()) {
        document.write('<ul><li>');
        document.write('<b>' + tree.tagName + ': </b>');
        var nodes = tree.childNodes.length;

        for(var i = 0; i < tree.childNodes.length; i++) {
            traverse(tree.childNodes(i));
        }
        document.write('</il></ul>');
    } else {
        document.write(tree.text);
    }
}

function initTraverse(file) {
    loadXML(file);
    var doc = xmlDoc.documentElement;
    traverse(doc);
}

When I fired Safari I saw that nothing was displayed, then I've opened the Error Co开发者_开发技巧nsole and what I got was this:

ReferenceError: Can't find variable: ActiveXObject

What should I do to make this work?

PS: I would prefer if this page could be capable of running at Mobile Safari


ActiveXObject do not work outside of internet explorer.

There are a few alternative xml parser's and handlers like E4X. Although E4X is currently only done in firefox (https://developer.mozilla.org/En/E4X/Processing_XML_with_E4X).

If using jQuery is an option then you can look into marcgrabanski.com/articles/jquery-makes-parsing-xml-easy


Some interesting stuff going on there. Most interesting is the async = false line. You probably want to re-consider that bit. In order to change to an asynchronous request, you would have to re-write some other code and remove the document.write calls.

Regardless, here is a (untested but hopefully) drop in replacement for what you have using XMLHttpRequest instead of an xml document.

var xmlDoc = null;
function loadXML(xmlFile) {
  var request = new XMLHttpRequest();
  request.open('GET', xmlFile, false); // false is synchronous
  request.send();

  xmlDoc = request.responseXML;
}

You may have to do some debugging...


You should have something cross-browser compatible with either DOMParser or DOMDocument. Of course, I'm not sure if you're wanting to parse a XML URL or a XML string. For a XML URL, I recommend:

  if      (window.XMLHttpRequest) return new window.XMLHttpRequest();
  else if (window.ActiveXObject) {
     // the many versions of IE's XML fetchers
     var AXOs = [
        'MSXML2.XMLHTTP.6.0',
        'MSXML2.XMLHTTP.5.0',
        'MSXML2.XMLHTTP.4.0',
        'MSXML2.XMLHTTP.3.0',
        'MSXML2.XMLHTTP',
        'Microsoft.XMLHTTP',
        'MSXML.XMLHTTP'
     ];
     for (var i = 0; i < AXOs.length; i++) {
        try     { return new ActiveXObject(AXOs[i]); }
        catch() { continue; }
     }
     return null;
  }

For a XML string, this code block would work better:

    if      (window.DOMParser)     return (new DOMParser()).parseFromString(str, 'text/xml');
    else if (window.ActiveXObject) {
        var doc;

        // the many versions of IE's DOM parsers
        var AXOs = [
            'MSXML2.DOMDocument.6.0',
            'MSXML2.DOMDocument.5.0',
            'MSXML2.DOMDocument.4.0',
            'MSXML2.DOMDocument.3.0',
            'MSXML2.DOMDocument',
            'Microsoft.XMLDOM',
            'MSXML.DOMDocument'
        ];
        for (var i = 0; i < AXOs.length; i++) {
            try     { doc = new ActiveXObject(AXOs[i]); break; }
            catch() { continue; }
        }    
        if (!doc) return createElement('div', null);

        if (doc.async) doc.async = false;
        doc.loadXML(str);
        return doc;
    }
    return createElement('div', null);

The DOMDocument objects do support a load() method for loading XML from a URL, but it's a different syntax than the XMLHttpRequest and XMLHTTP methods.

The DOMDocument appears (at least from the MSDN docs) to also contain the XMLHTTP methods, so you could interlace DOMDocument in the AXOs array, but I'm not certain about that. Plus, I can't imagine DOMDocument being in place without XMLHTTP.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜