Edge中带有XML字符串内容的Microsoft.XMLDOM XML解析器抛出错误 [英] Error thrown with Microsoft.XMLDOM XML Parser in Edge with XML string content

查看:986
本文介绍了Edge中带有XML字符串内容的Microsoft.XMLDOM XML解析器抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Microsoft EDGE中,Microsoft.XMLDOM XML解析器出现以下错误:

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

Script(1,1)

有时会显示start tag does not match end tag.而其他时候,它又给出了另一个错误.我希望我可以提供实际的错误消息,但是我不在Windows计算机上,这是我从内存中记住的信息.

Sometimes it says start tag does not match end tag. And other times it gives another error. I wish I could provide the actual error messages but I'm away from a Windows machine and this is what I remember from memory.

完全相同的XML内容可在Firefox和其他浏览器中使用.谁能看到发生了什么事?这可能是一个简单的修复,但是我又没有Windows计算机.

The exact same XML content works in Firefox and other browsers. Can anyone see what is going on? This could be an easy fix but again I don't have a Windows computer.

这是我的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.

Can someone with Windows 10 run this? I've created a codepen here.

推荐答案

我的其他代码中有一个错误导致我遇到该错误,但是我还发现,当Edge或IE中出现错误时,它们将记录控制台中的错误.

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.

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

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

如果(window.DOMParser || document.implementation.createDocument)

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

,然后在parse方法周围放置try catch块.

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

尽管如此,IE似乎没有提供行或列错误信息.我无法对其进行广泛的测试.

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

可以测试更新的 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>";
}

这篇关于Edge中带有XML字符串内容的Microsoft.XMLDOM XML解析器抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆