DTD 驗證

Internet Explorer 5.0 可根據某個 DTD 來驗證您的 XML。

通過 XML 解析器進行驗證

當您試圖打開某個 XML 文檔時,XML 解析器有可能會產生錯誤。通過訪問 parseError 對象,就可以取回引起錯誤的確切代碼、文本甚至所在的行。

注釋:load( ) 方法用于文件,而 loadXML( ) 方法用于字符串。

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.validateOnParse="true"
xmlDoc.load("note_dtd_error.xml")
document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)

Try it Yourself 或者 僅僅看一下這個 XML 文件

關閉驗證

通過把 XML 解析器的 validateOnParse 設置為 "false",就可以關閉驗證。

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.validateOnParse="false"
xmlDoc.load("note_dtd_error.xml")
document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)

Try it Yourself

通用的 XML 驗證器

為了幫助您驗證 XML 文件,我們創建了此鏈接,這樣你就可以驗證任何 XML 文件了。

parseError 對象

您可以在我們的《XML DOM 教程》中閱讀更多有關 parseError 對象的信息。