ParseFromString在IE中引发错误,但在Chrome中不会引发错误 [英] ParseFromString throws error in IE, but not in Chrome

查看:331
本文介绍了ParseFromString在IE中引发错误,但在Chrome中不会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用适用于小册子的KML插件,该插件在Google Chrome中效果很好。但是,在IE中,它会在以下代码中抛出错误。

I'm using a KML plugin for leaflet that works great in Google Chrome. In IE, however, It throws an error at the following code.

parser=new DOMParser();
console.log(url) // outputs: "path/to/kmlfile.kml" in Chrome debugger
url=parser.parseFromString(url,"text/xml"); //This line throws a parser error in IE 11, but is fine in Chrome

在我看来这段代码中有一个错误 - 作者应该将一个实际的XML字符串传递给parser.parseFromString()函数,而不仅仅是XML文档的url。由于文件的路径不是有效的XML文件(解释:kml文件只是XML),因此解析器会出错是有道理的。但是,这不会导致Chrome调试器工具中出现任何错误,这真的很奇怪。

It seems to me that there is a mistake in this code - the author should pass an actual XML string, not just a url to an XML document to the parser.parseFromString() function. It makes sense that the parser would have an error, as a path to a file is not a valid XML file (Note: kml files are just XML). However, this does not cause any errors to be thrown in the Chrome Debugger tools, which is really strange.

在我看来,这在两个实例中都会失败。 DOMParser上的Trusty MDN文档没有提到将URL作为参数放在parseFromString()中。所以我的问题是为什么这在Chrome中运行,但在IE中出错,然后我该怎么做才能修复它?

It seems to me that this should fail in both instances. Trusty MDN docs on DOMParser have no mention of putting a URL as a parameter in parseFromString(). So my question is why is this working in Chrome, but throwing an error in IE, and then what can I do to fix it?

注意这个问题与跟随网址,因为这不是一般错误 - 这是关于在Chrome中有效但在IE中失败的内容: Internet Explorer 11(IE 11)在DOMParser中使用parseFromString引发语法错误

Note this question is different from the following url because this isn't a general error - this is about something that works in Chrome but fails in IE: Internet Explorer 11 (IE 11) Throws Syntax Error using parseFromString in DOMParser

推荐答案

当XML格式错误的非Microsoft浏览器(Firefox,Chrome等)时,它将创建带有错误消息的XML文档作为其内容。点击此处(< - 点击此处)。

When the XML is malformed non-Microsoft browsers (Firefox, Chrome, etc) it will create the XML document with the error message as it's content. Click here (<-- click there).

当Microsoft浏览器,IE和Edge中的XML格式错误时,它会抛出错误,将错误写入控制台并停止脚本。注意我在Mac上,所以我已经远程测试了这个,但没有机会亲自测试它。您可以将该代码放入IE的try catch块中,但我的意思是我不知道是否会阻止它将消息写入控制台。

When the XML is malformed in Microsoft browsers, IE and Edge, it throws an error, writes an error to the console and your script stops. Note I'm on a Mac so I've tested this remotely but have not had a chance to test it personally. You can put that code in a try catch block for IE but what I mean is I don't know if that will stop it from writing a message to the console.

以下是代码笔故意格式错误的XML,并在输出中写入错误消息。 codepen或输出中没有错误。我故意将解析器中的错误代码写入输出窗口。打开控制台,看看发生了什么。

Here's the code pen with intentionally malformed XML and the error message is written in the output. There is no error in the codepen or output. I'm intentionally writing the error code from the parser to the output window. Open the console to see what's going on.

FWIW IE是正确的行为恕我直言。没有抛出错误是互联网最近做事的方式。没有抛出错误的问题是你不知道你做错了什么或在哪里。写一次,调试一切。

FWIW IE is the correct behavior IMHO. Not throwing errors was the Internet way to do things until relatively recently. The problem with not throwing errors is you don't know what you did wrong or where. Write once, debug everything.

此外,在更新版本之前,IE使用ActiveX来解析XML文档。

Also, until more recent versions, IE used ActiveX to parse XML documents.

来自W3C XML 验证脚本:

From W3C XML validation script:

function validateXML(text) {
    var message;
    var parser;
    var xmlDoc;

    // code for Edge, IE, Mozilla, Firefox, Opera, etc.
    if (document.implementation.createDocument || window.DOMParser) {
        parser = new DOMParser();

        try {
            xmlDoc = parser.parseFromString(text, "text/xml");
        }
        catch (error) {

        }

        if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
            return xmlDoc.getElementsByTagName("parsererror")[0];
        }
        else {
            return "No errors found";
        }
    }
    // code for older versions of IE
    else if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";

        try {
            xmlDoc.loadXML(text);
        }
        catch (error) {

        }

        if (xmlDoc.parseError.errorCode != 0) {
            message = "Error Code: " + xmlDoc.parseError.errorCode + "\\n";
            message = message + "Error Reason: " + xmlDoc.parseError.reason;
            message = message + "Error Line: " + xmlDoc.parseError.line;
            return message;
        }
        else {
            return "No errors found";
        }
    }

    else {
        return "Not supported";
    }
}

相关问题

这篇关于ParseFromString在IE中引发错误,但在Chrome中不会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆