最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Safari Won't Work With Microsoft.XMLDOM ActiveX Object - Stack Overflow

programmeradmin1浏览0评论

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 Console 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

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 Console 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

Share Improve this question edited Jan 7, 2011 at 12:24 Nathan Campos asked Jan 7, 2011 at 12:17 Nathan CamposNathan Campos 29.5k61 gold badges200 silver badges307 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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/En/E4X/Processing_XML_with_E4X).

If using jQuery is an option then you can look into marcgrabanski./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 patible 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 remend:

  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)

  1. 暂无评论