問題描述
我在 Microsoft EDGE 中使用 Microsoft.XMLDOM XML 解析器時遇到以下錯誤:
I'm getting the following error with the Microsoft.XMLDOM XML parser in Microsoft EDGE:
腳本(1,1)
有時它會說開始標簽與結束標簽不匹配
.有時它會給出另一個錯誤.我希望我能提供實際的錯誤消息,但我遠離 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
if (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>";
}
這篇關于帶有 XML 字符串內容的 Edge 中的 Microsoft.XMLDOM XML 解析器引發錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!