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

📄 dom_nodes_access.asp

📁 W3Schools tutorial..web designing
💻 ASP
📖 第 1 页 / 共 2 页
字号:

<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>

<br />
<hr />

<!-- **** SPOTLIGHTS 1 **** -->

<iframe src="../banners/aspallframe.asp" height="110" width="485"
marginwidth="0" marginheight="0" frameborder="0" scrolling="no">
Your browser does not support inline frames or is currently configured not to display inline frames.
</iframe>
<hr />
<!-- **** SPOTLIGHTS 2 **** -->

<h2><a target="_blank" href="../../www.altova.com/ref/@s=w3s_spotlight&q=xmlspy">
Altova

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -