ParseFromString throws error in IE, but not in Chrome

早过忘川 提交于 2019-12-07 10:01:08

问题


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

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.

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?

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


回答1:


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).

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.

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

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

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";
    }
}

Related question.



来源:https://stackoverflow.com/questions/31277058/parsefromstring-throws-error-in-ie-but-not-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!