使用XML字符串内容在Edge中使用Microsoft.XMLDOM XML Parser引发错误

时间:2017-05-24 01:48:26

标签: javascript xml xml-parsing microsoft-edge xmldom

我在Microsoft EDGE中使用Microsoft.XMLDOM XML解析器时出现以下错误:

Script(1,1)

有时会说start tag does not match end tag。有时它又会出现另一个错误。我希望我能提供实际的错误消息,但我远离Windows机器,这是我记忆中的记忆。

完全相同的XML内容适用于Firefox和其他浏览器。任何人都可以看到发生了什么?这可能是一个简单的修复,但我再次没有Windows计算机。

这是我的XML:

<s:RichText x="118" visible="true" y="238" text="Text" fontSize="58.73271028037384">
    <s:filters>
        <BorderStrokeFilter alpha="1" angle="45" blurX="3" blurY="3" color="#FFFFFF" distance="0" hideObject="false" inner="false" knockout="false" multiplier="6" quality="3" strength="30" weight="3" xmlns="library://ns.flexcapacitor.com/flex"/>
        <BorderStrokeFilter alpha="1" angle="45" blurX="3" blurY="3" color="#000000" distance="0" hideObject="false" inner="false" knockout="false" multiplier="6" quality="3" strength="30" weight="3" xmlns="library://ns.flexcapacitor.com/flex"/>
    </s:filters>
    <s:textFlow>
        <s:TextFlow whiteSpaceCollapse="preserve" version="3.0.0" xmlns:s="library://ns.adobe.com/flex/spark"><s:p><s:span s:fontWeight="bold">Here is some text</s:span></s:p></s:TextFlow>
    </s:textFlow>
</s:RichText>

这是我的验证方法:

function validateXML(txt) {

    // code for IE
    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;
            return txt;
        }
        else {
            return "No errors found";
        }
    }
    // Mozilla, Firefox, Opera, etc.
    else if (document.implementation.createDocument) {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(txt, "text/xml");

        if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
            return xmlDoc.getElementsByTagName("parsererror")[0];
        }
        else {
            return "No errors found";
        }
    }
    else {
        return "Your browser does not support XML validation";
    }
}


var xml = '<s:RichText x="118"></s:RichText>';
var result = validateXML(xml);

Windows 10的用户可以运行吗?我创建了一个codepen here

1 个答案:

答案 0 :(得分:0)

我的其他代码出错导致我遇到的错误但我也发现当Edge或IE出现错误时,他们会在控制台中记录错误。

此外,支持从IE 10或11开始使用DOMParser。解决方案是切换if语句条件以检查Domparser

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

然后在解析方法周围放置一个try catch块。

虽然,它看起来不像IE给出行或列错误信息。我还没有能够对它进行广泛的测试。

可以测试更新的codepen

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