📄 dom_nodes_access.asp
字号:
<h3>Example</h3>
<p>The following example returns all <title> elements under the x element:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table7">
<tr>
<td valign="top">
<pre>x.getElementsByTagName("title");</pre>
</td>
</tr>
</table>
<p>Note that the example above only returns <title> elements under the x node. To return all <title> 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("title");</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 "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> and stores a list of <title> 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("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title");</pre>
</td>
</tr>
</table>
<p>The <title> elements in x can be accessed by index number. To access the third <title> 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("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title");</pre>
<pre>for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
}</pre>
</td>
</tr>
</table>
<p>Example explained:</p>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
<li>Get all <title> 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("books.xml");</pre>
<pre>x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==1)
{//Process only element nodes (type 1)
document.write(x[i].nodeName);
document.write("<br />");
}
}</pre>
</td>
</tr>
</table>
<p>Example explained:</p>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
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 "1" 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("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;
for (i=0;i<x.length;i++)
{
if (y.nodeType==1)
{//Process only element nodes (type 1)
document.write(y.nodeName + "<br />");
}
y=y.nextSibling;
}</pre>
</td>
</tr>
</table>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
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 "y" variable to be the first child node of the first book
element</li>
<li>For each child node (starting with the first child node "y"):</li>
<li>Check the node type. If the node type is "1" it is an element node</li>
<li>Output the name of the node if it is an element node</li>
<li>Set the "y" 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 + -