ParseFromString throws error in IE but not Chrome

I am using a brochure KML plugin that works great in Google Chrome. However, in IE, it throws an error in 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 bug in this code - the author must pass the actual XML string, not just the URL of the XML document to the parser.parseFromString () function. It makes sense that the parser will have an error since the file path is not a valid XML file (Note: kml files are just XML). However, this doesn't throw any errors in the Chrome debugger tools, which is really strange.

It seems to me that this should fail in both cases. Trusted MDN docs on DOMParser have no mention of putting the url in the parseFromString () parameter. So my question is why does this work in Chrome but throws an error in IE, then what can I do to fix it?

Please note that this question is different from the following url because this is not a common error - it is about something that works in Chrome but doesn't work in IE: Internet Explorer 11 (IE 11) Throws a syntax error using parseFromString in DOMParser

+3


source to share


1 answer


When XML is malformed by a non-Microsoft browser (Firefox, Chrome, etc.), it will create an XML document with an error message as content. Click here (<- click here).

When XML is invalid in Microsoft, IE and Edge browsers, it will throw an error, write the error to the console, and stop the script. Note. I'm on a Mac, so I tested this remotely, but I haven't had a chance to test it personally. You can put this code in a try catch block for IE, but I mean I don't know if it will stop him from writing a message to the console.

Here's a handle of deliberately malformed XML and an error message is written on the output. There is no error in the encoder or output. I am deliberately writing the error code from the analyzer to the output window. Open your console to see what's going on.

FWIW IE is the correct behavior IMHO. The mistakes were not a mistake, as the internet method was made until relatively recently. The problem with erroneous errors is that you don't know what you did wrong or where. Write once, debug everything.



In addition, until later 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 .

+1


source







All Articles