📄 dom_errors_crossbrowser.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XML DOM Parser Errors</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>XML DOM Parser Errors</h1>
<a href="dom_errors.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_summary.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<hr />
<p class="intro">When Firefox encounter a parser error, it loads an XML document
containing the error</p>
<hr />
<h2>Parser Error in Firefox</h2>
<p>When trying to open an XML document, a parser-error may occur.</p>
<p>Unlike Internet Explorer, if Firefox encounters an error, it loads an XML
document containing the error description.</p>
<p>The root node name of the XML error document is "parsererror". This is used
to check if there is an error.</p>
<hr />
<h2>XML Error
</h2>
<p>In the following code we let the parser load an XML document that is not well-formed.
</p>
<p>(You can read more about well-formed and valid XML in our
<a href="../xml/default.asp">XML tutorial</a>)
</p>
<table border="1" width="100%" class="ex" cellspacing="0"><tr><td>
<pre>xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load("note_error.xml");
if (xmlDoc.documentElement.nodeName=="parsererror")
{
errStr=xmlDoc.documentElement.childNodes[0].nodeValue;
errStr=errStr.replace(/</g, "&lt;");
document.write(errStr);
}
else
{
document.write("XML is valid");
}</pre></td></tr></table>
<p>Example explained:</p>
<ol>
<li>Load the xml file</li>
<li>Check if the nodeName of the root node is "parsererror"</li>
<li>Load the error string into a variable "errStr"</li>
<li>Replace "<" characters with "&lt;" before the error string can be
written as HTML</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=note_error_firefox">Try it yourself</a>
or just <a target="_blank" href="note_error.xml">look at the XML file</a></p>
<p><b>Note:</b> Only Internet Explorer will actually check your XML against the
DTD. Firefox will not.</p>
<hr />
<h2>Cross Browser Error Check</h2>
<p>Here we have created an XML load function that checks for parser errors in
both Internet Explorer and Firefox:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table1">
<tr><td>
<pre>function loadXMLDocErr(dname)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(dname);
if (xmlDoc.parseError.errorCode != 0)
{
alert("Error in line " + xmlDoc.parseError.line +
" position " + xmlDoc.parseError.linePos +
"\nError Code: " + xmlDoc.parseError.errorCode +
"\nError Reason: " + xmlDoc.parseError.reason +
"Error Line: " + xmlDoc.parseError.srcText);
return(null);
}
}
catch(e)
{
try //Firefox
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load(dname);
if (xmlDoc.documentElement.nodeName=="parsererror")
{
alert(xmlDoc.documentElement.childNodes[0].nodeValue);
return(null);
}
}
catch(e) {alert(e.message)}
}
try
{
return(xmlDoc);
}
catch(e) {alert(e.message)}
return(null);
}</pre>
</td></tr>
</table>
<p>Example explained - Internet Explorer:</p>
<ol>
<li>The first line creates an empty Microsoft XML document object</li>
<li>The second line turns off asynchronized loading, to make sure that the
parser will not continue execution of the script before the document is
fully loaded</li>
<li>The third line tells the parser to load an XML document called "note_error.xml"</li>
<li>If the errorCode property of the parseError object is different from
"0", alert the error and exit the function</li>
<li>If the errorCode property is "0", return the XML document</li>
</ol>
<p>Example explained - Firefox:</p>
<ol>
<li>The first line creates an empty XML document object.</li>
<li>The second line turns off asynchronized loading, to make sure that the
parser will not continue execution of the script before the document is
fully loaded.</li>
<li>The third line tells the parser to load an XML document called "note_error.xml".</li>
<li>If the returned document is an error document, alert the error and exit
the function</li>
<li>If not, return the XML document</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=note_error_crossbrowser">Try it yourself</a>
or just <a target="_blank" href="note_error.xml">look at the XML file</a><br />
</p>
<hr />
<a href="dom_errors.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_summary.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -