Error thrown with Microsoft.XMLDOM XML Parser in Edge with XML string content

后端 未结 1 1821
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 10:58

I\'m getting the following error with the Microsoft.XMLDOM XML parser in Microsoft EDGE:

Script(1,1)

Sometimes it says start tag does not mat

相关标签:
1条回答
  • 2021-01-26 11:41

    There was an error in my other code that was causing the error I was encountering but I also found out that when there's an error in Edge or IE they will log an error in the console.

    Also, starting around IE 10 or 11 DOMParser is supported. The solution is to switch the if statement conditions to check for Domparser

    if (window.DOMParser || document.implementation.createDocument)

    and then put a try catch block around the parse method.

    Although, it doesn't look like IE gives a line or column error information. I haven't been able to extensively test it.

    The updated codepen can be tested:

    function validateXML(txt) {
    
    
        // Mozilla, Firefox, Opera, newer IE and Edge, etc.
        if (document.implementation.createDocument) {
            console.log("Before creating domparser");
            var parser = new DOMParser();
            try {
                var xmlDoc = parser.parseFromString(txt, "text/xml");
            } catch(error) {
                console.log(error);
            };
    
            console.log("After DomParser instance. Errors: "+ xmlDoc.getElementsByTagName("parsererror").length);
            if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
                return xmlDoc.getElementsByTagName("parsererror")[0];
            }
            else {
                return "No errors found";
            }
        }
        // code for older IE
        else if (window.ActiveXObject) {
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(txt);
    
            if (xmlDoc.parseError.errorCode != 0) {
                txt = "Error Code: " + xmlDoc.parseError.errorCode + "\\n";
                txt = txt + "Error Reason: " + xmlDoc.parseError.reason;
                txt = txt + "Error Line: " + xmlDoc.parseError.line;
                console.log("I work in Windows IE");
                return txt;
            }
            else {
                return "No errors found";
            }
        }
        else {
            return "Your browser does not support XML validation";
        }
    }
    
    
    var xml = '<s:RichText x="118" xmlns:s="f">test</f/></s:RichText>';
    var result = validateXML(xml);
    console.log(result); 
    if (typeof result == "string") {
      document.body.innerHTML = "<pre>"+result+"</pre>";
    }
    else {
      document.body.innerHTML = "<pre>"+result.outerHTML+"</pre>";
    }
    
    0 讨论(0)
提交回复
热议问题