Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ParseFromString throws error in IE, but not in Chrome

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

like image 593
user3413723 Avatar asked Jul 07 '15 18:07

user3413723


1 Answers

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.

like image 97
1.21 gigawatts Avatar answered Oct 06 '22 03:10

1.21 gigawatts