JavaScript:ActiveXObject MSXML2.XMLHTTP 在成功加载时未返回 XML...?

发布于 2024-10-16 20:27:13 字数 1464 浏览 5 评论 0原文

我正在尝试编写一个 Internet Explorer 8 解决方案,用于通过“文件”协议加载 XML,因为我正在构建的网站旨在作为包直接发送给用户。我在尝试使用 XMLHttpRequest 来处理这个问题时所经历的一切似乎都支持我在网上读到的内容:IE8 的 XMLHttpRequest 实现不喜欢该协议,因此我必须使用 ActiveXObject 来处理加载。

我尝试了各种人的建议,最后得到了似乎成功获取文件的代码,因为responseText字段填充了文件的内容。然而,应该保存 XML 的 responseXML.xml 字段(或其文本表示形式,我读过的文档都不是很清楚)始终是一个空字符串。如何配置 ActiveXObject 以正确加载 XML?

作为奖励,有人还可以解释一下,一旦加载成功,我应该如何使用加载的 XML?我还没有找到任何解释这一点的文件。

这是我的 JavaScript:

function isIE() {
    return navigator.userAgent.lastIndexOf('Trident') > 0;
}

// This block ensures that the XML request occurs in the same domain.
var path = document.location.href;
path = path.substr(0, path.lastIndexOf('/') + 1);

if (isIE() && location.protocol == 'file:') {
    var xmlRequest = new ActiveXObject('MSXML2.XMLHTTP');
    xmlRequest.open('GET', path + 'xml/shared.xml', false);
    xmlRequest.onreadystatechange = useXML;
    xmlRequest.send();

    function useXML() {
        if (xmlRequest && xmlRequest.readyState && xmlRequest.readyState == 4) {
            alert(xmlRequest.responseText);    // displays the file
            alert(xmlRequest.responseXML.xml); // displays nothing
        }
    }
}

这是我的 XML 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<shared>
    <page_title>
        Test Page Title
    </page_title>
</shared>

我使用 w3schools XML 验证器来检查该文件是否存在某种格式错误。它不是。

I am trying to write an Internet Explorer 8 solution for loading XML through the 'file' protocol, since the site I am building is intended to be sent as packages directly to the users. Everything I have experienced in attempting to use XMLHttpRequest to handle this seems to support what I've read online: IE8's XMLHttpRequest implementation dislikes the protocol, so I have to use an ActiveXObject to handle the loading.

I have experimented with various people's suggestions, and finally have code that appears to be successfully obtaining the file, as the responseText field is filled with the contents of the file. However, the responseXML.xml field that is supposed to hold the XML (or a text representation of it, none of the documentation I've read has been very clear) is always an empty string. How can I configure the ActiveXObject to load the XML properly?

As a bonus, could someone also explain how I am supposed to use the loaded XML once it is loading successfully? I have yet to find any documents that explain that bit.

Here is my JavaScript:

function isIE() {
    return navigator.userAgent.lastIndexOf('Trident') > 0;
}

// This block ensures that the XML request occurs in the same domain.
var path = document.location.href;
path = path.substr(0, path.lastIndexOf('/') + 1);

if (isIE() && location.protocol == 'file:') {
    var xmlRequest = new ActiveXObject('MSXML2.XMLHTTP');
    xmlRequest.open('GET', path + 'xml/shared.xml', false);
    xmlRequest.onreadystatechange = useXML;
    xmlRequest.send();

    function useXML() {
        if (xmlRequest && xmlRequest.readyState && xmlRequest.readyState == 4) {
            alert(xmlRequest.responseText);    // displays the file
            alert(xmlRequest.responseXML.xml); // displays nothing
        }
    }
}

And here is my XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<shared>
    <page_title>
        Test Page Title
    </page_title>
</shared>

I used the w3schools XML validator to check whether this file was somehow malformed. It is not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-10-23 20:27:13

这是因为本地文件不是作为 text/xml 提供的(如服务器所做的),因此 IE 不会解析它。

您可以使用 Microsoft.XMLDOM< 手动解析它/代码> 对象

function useXML() {
        if (xmlRequest && xmlRequest.readyState && xmlRequest.readyState == 4) {
            alert(xmlRequest.responseText);    // displays the file
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async="false";
            xmlDoc.loadXML(xmlRequest.responseText);
            title = xmlDoc.documentElement.getElementsByTagName('page_title')[0];
            alert(title.childNodes[0].nodeValue);
        }
    }

This is because the local file is not served as text/xml (as a server would do) and so IE will not parse it..

you can parse it manually with the Microsoft.XMLDOM object

function useXML() {
        if (xmlRequest && xmlRequest.readyState && xmlRequest.readyState == 4) {
            alert(xmlRequest.responseText);    // displays the file
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async="false";
            xmlDoc.loadXML(xmlRequest.responseText);
            title = xmlDoc.documentElement.getElementsByTagName('page_title')[0];
            alert(title.childNodes[0].nodeValue);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文