⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dom_nodes_access.asp@output=print

📁 W3Schools tutorial..web designing
💻 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 Access Nodes</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 - Accessing Nodes</h1>
<a href="dom_methods.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" /></a>
<a href="dom_nodes_info.asp"><img border="0" alt="next" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />

<p class="intro">With the DOM, you can access every node in an XML document.</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">loadXMLDoc()</a>, in an external JavaScript is used to load the XML file.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_index">Access a node 
using its index number in a node list</a><br />
This example uses the getElementsByTagname() method to get the third &lt;title&gt; 
element in &quot;books.xml&quot;</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_list_loop">Loop through 
nodes using the length property</a><br />
This example uses the length property to loop through all &lt;title&gt; 
elements in &quot;books.xml&quot;</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_root">See the node type 
of an element</a><br />
This example uses the nodeType property to get node type of the root element in &quot;books.xml&quot;.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_loop">Loop through 
element nodes</a><br />
This example uses the nodeType property to only process element nodes in &quot;books.xml&quot;.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_navigate">Loop through 
element nodes using node realtionships</a><br />
This example uses the nodeType property and the nextSibling property to process 
element nodes in &quot;books.xml&quot;.</p>

<hr />

<h2>Accessing Nodes</h2>
<p>You can access a node in three ways:</p>
<p>1. By using the getElementsByTagName() method</p>
<p>2. By looping through (traversing) the nodes tree.</p>
<p>3. By navigating the node tree, using the node relationships.</p>

<hr />

<h2>The getElementsByTagName() Method</h2>
<p>getElementsByTagName() returns all elements with a specified tag 
name.</p>
<h3>Syntax</h3>
<table border="1" width="100%" class="ex" cellspacing="0" id="table10">
<tr>
	<td valign="top">
	<pre><i>node</i>.getElementsByTagName(<i>&quot;tagname&quot;</i>);</pre>
	</td>
</tr>
</table>

<h3>Example</h3>
<p>The following example returns all &lt;title&gt; elements under the x element:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table7">
<tr>
	<td valign="top">
	<pre>x.getElementsByTagName(&quot;title&quot;);</pre>
	</td>
</tr>
</table>

<p>Note that the example above only returns &lt;title&gt; elements under the x node. To return all &lt;title&gt; elements in the XML document use: </p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table11">
<tr>
	<td valign="top">
	<pre>xmlDoc.getElementsByTagName(&quot;title&quot;);</pre>
	</td>
</tr>
</table>

<p>where xmlDoc is the document itself (document node).</p>

<hr />

<h2>DOM Node List</h2>
<p>The getElementsByTagName() method returns a node list. A node list is an 
array of nodes.</p>
<p>The following code loads &quot;<a target="_blank" href="books.xml">books.xml</a>&quot; 
	into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> and stores a list of &lt;title&gt; nodes (a node list) in the 
variable x:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table4">
<tr>
	<td valign="top">
	<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
	<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;);</pre>
	</td>
</tr>
</table>

<p>The &lt;title&gt; elements in x can be accessed by index number. To access the third &lt;title&gt; you can write:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table12">
<tr>
	<td valign="top">
	<pre>y=x[2];</pre>
	</td>
</tr>
</table>

<p><a target="_blank" href="tryit.asp@filename=try_dom_index">Try it yourself.</a></p>
<p><b>Note:</b> The index starts at 0.</p>
<p>You will learn more about node lists in a later chapter of this tutorial.</p>

<hr />

<h2>DOM Node List Length</h2>
<p>The length property defines the length of a node list (the number of nodes).</p>
<p>You can loop through a node list by using the length property:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table5">
<tr>
	<td valign="top">
	<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
	<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;);</pre>
	<pre>for (i=0;i&lt;x.length;i++)
  { 
  document.write(x[i].childNodes[0].nodeValue);
  document.write(&quot;&lt;br /&gt;&quot;);
  }</pre>
	</td>
</tr>
</table>

<p>Example explained:</p>
<ol>
	<li>Load &quot;<a target="_blank" href="books.xml">books.xml</a>&quot; 
	into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
	<li>Get all &lt;title&gt; element nodes</li>
	<li>For each title element, output the value of its text node</li>
</ol>

<p><a target="_blank" href="tryit.asp@filename=try_dom_list_loop">Try it 
yourself.</a></p>

<hr />
<h2>Node Types</h2>
<p>The <b>documentElement</b> property of the XML document is the root node.</p>
<p>The <b>nodeName</b> property of a node is the name of the node.</p>
<p>The <b>nodeType</b> property of a node is the type of the node.</p>
<p>You will learn more about the node properties in the next chapter of this 
tutorial.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_root">Try it yourself.</a></p>

<hr />

<h2>Traversing Nodes</h2>
<p>The following code loops through the child nodes, that are also element 
nodes, of the root node:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table17">
<tr>
	<td valign="top">
	<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
	<pre>x=xmlDoc.documentElement.childNodes;

for (i=0;i&lt;x.length;i++)
{ 
  if (x[i].nodeType==1)
  {//Process only element nodes (type 1) 
  document.write(x[i].nodeName);
  document.write(&quot;&lt;br /&gt;&quot;);
  } 
}</pre>
	</td>
</tr>
</table>

<p>Example explained:</p>
<ol>
	<li>Load &quot;<a target="_blank" href="books.xml">books.xml</a>&quot; 
	into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
	<li>Get the child nodes of the root element</li>
	<li>For each child node, check the node type of the node. If the node type 
	is &quot;1&quot; it is an element node</li>
	<li>Output the name of the node if it is an element node</li>
</ol>

<p><a target="_blank" href="tryit.asp@filename=try_dom_loop">Try it yourself.</a></p>

<hr />

<h2>Navigating Node Relationships</h2>
<p>The following code navigates the node tree using the node relationships:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table18">
<tr>
	<td valign="top">
	<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
	<pre>x=xmlDoc.getElementsByTagName(&quot;book&quot;)[0].childNodes;
y=xmlDoc.getElementsByTagName(&quot;book&quot;)[0].firstChild;

for (i=0;i&lt;x.length;i++)
{
if (y.nodeType==1)
  {//Process only element nodes (type 1)
  document.write(y.nodeName + &quot;&lt;br /&gt;&quot;);
  }
y=y.nextSibling;
}</pre>
	</td>
</tr>
</table>

<ol>
	<li>Load &quot;<a target="_blank" href="books.xml">books.xml</a>&quot; 
	into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
	<li>Get the child nodes of the first book element</li>
	<li>Set the &quot;y&quot; variable to be the first child node of the first book 
	element</li>
	<li>For each child node (starting with the first child node &quot;y&quot;):</li>
	<li>Check the node type. If the node type is &quot;1&quot; it is an element node</li>
	<li>Output the name of the node if it is an element node</li>
	<li>Set the &quot;y&quot; variable to be the next sibling node, and run through the 
	loop again</li>
</ol>

<p><a target="_blank" href="tryit.asp@filename=try_dom_navigate">Try it 
yourself.</a></p>

<hr />
<a href="dom_methods.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="dom_nodes_info.asp"><img border="0" alt="next" src="../images/btn_next.gif" 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 + -