📄 dom_loadxmldoc.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</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 Load Function</h1>
<a href="dom_parser.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_methods.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<hr />
<p class="intro">
The code for loading XML documents can be stored in a function.</p>
<p class="intro">The function described below, is used in all examples in this tutorial.</p>
<hr />
<h2>An XML Load Function - loadXMLDoc()</h2>
<p>The XML DOM contains methods (functions) to traverse XML trees, access,
insert, and delete nodes.</p>
<p>However, before an XML document can be accessed and manipulated, it must be
loaded into an XML DOM object.</p>
<p>The previous chapter demonstrated how to load XML documents. To make it
simpler to maintain this code, it should be written as a function: </p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table28">
<tr><td>
<pre>function loadXMLDoc(dname)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try
{
xmlDoc.async=false;
xmlDoc.load(dname);
return(xmlDoc);
}
catch(e) {alert(e.message)}
return(null);
}
</pre>
</td></tr>
</table>
<p>The function above can be stored in the <head> section of an HTML page, and
called from a script in the page.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_loadxmldoc">Try it yourself</a></p>
<hr />
<h2>An External Load Function</h2>
<p>To make the function above even easier to maintain, and to make sure the same
code is used in all pages, it can be stored in an external file.</p>
<p>We have called the file "loadxmldoc.js"</p>
<p>The file can be loaded in the <head> section of an HTML page, and loadXMLDoc()
can be called from a script in the page:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table29">
<tr><td>
<pre><html>
<head>
<script type="text/javascript" src="loadxmldoc.js">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
document.write("xmlDoc is loaded, ready for use");
</script>
</body>
</html></pre>
</td></tr>
</table>
<p><a target="_blank" href="tryit.asp@filename=try_dom_loadxmldoc2">Try it
yourself</a></p>
<p><img border="0" src="../images/lamp.gif" width="15" height="15" alt="lamp" />
<b>The function described above, is used in all examples in this
tutorial</b></p>
<hr />
<h2>An XML Load Function - loadXMLString()</h2>
<p>A similar function can be used to load an XML string (instead an XML file):</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table30">
<tr><td>
<pre>function loadXMLString(txt)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
return(xmlDoc);
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
return(xmlDoc);
}
catch(e) {alert(e.message)}
}
return(null);
}
</pre>
</td></tr>
</table>
<p>To make the function above easier to maintain, and to make sure the same
code is used in all pages, it can be stored in an external file.</p>
<p>We have stored in in a file called "loadxmlstring.js".</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_loadxmlstring">Try it yourself</a></p>
<hr />
<a href="dom_parser.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_methods.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 + -