📄 dom_nodes_traverse.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 - Traverse Node Tree</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 Traverse Node Tree</h1>
<a href="dom_nodes_nodelist.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_mozilla_vs_ie.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<hr />
<p class="intro">Traversing means looping through or traveling across the node
tree.</p>
<hr />
<h2>Examples</h2>
<p>The examples below use the XML file
<a target="_blank" href="books.xml">books.xml</a>. <br />
A function, <a href="dom_loadxmldoc.asp">loadXMLString()</a>, in an external JavaScript is used to load the XML
string.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_parsertest3">Traverse a
node tree</a><br />
Loop through all child nodes of the <book> element</p>
<hr />
<h2>Traversing the Node Tree</h2>
<p>Often you want to loop an XML document, for example: when you want to extract
the value of each element.</p>
<p>This is called "Traversing the node tree"</p>
<p>The example below loops through all child nodes of <book>, and
displays their names and values:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table15">
<tr><td>
<pre><html>
<head>
<script type="text/javascript" src="loadxmlstring.js"></script>
</head>
<body>
<script type="text/javascript">
text="<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";</pre>
<pre>xmlDoc=loadXMLString(text);
// documentElement always represents the root node
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
document.write(x[i].nodeName);
document.write(": ");
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
}
</script>
</body>
</html></pre>
</td></tr>
</table>
<p>Output:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table21">
<tr><td>
<pre>title: Everyday Italian
author: Giada De Laurentiis
year: 2005</pre>
</td></tr>
</table>
<p>Example explained:</p>
<ol>
<li><a href="dom_loadxmldoc.asp">loadXMLString()</a> loads the XML string into xmlDoc</li>
<li>Get the child nodes of the root element</li>
<li>For each child node, output the node name and the node value of the text
node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_parsertest3">Try it
yourself</a></p>
<hr />
<a href="dom_nodes_nodelist.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_mozilla_vs_ie.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 + -