📄 dom_nodes_nodelist.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 - Node List and NamedNodeMap</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 Node List</h1>
<a href="dom_nodes_info.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_traverse.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<hr />
<p class="intro">A list of nodes is returned by the getElementsByTagName()
method and the childNodes property.</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_nodelist">Get the text
from the first <title> element</a><br />
This example uses the getElementsByTagName() method to get the text from the
first <title> element in "books.xml".</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_nodelist_length">Loop through
nodes using the length property</a><br />
This example uses node list and the length property to loop through all <title>
elements in "books.xml"</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_attributes">Get the
attribute of an element</a><br />
This example uses a attribute list to get attribute from the first <book>
element in "books.xml".</p>
<hr />
<h2>DOM Node List</h2>
<p>When using properties or methods like childNodes or getElementsByTagName(),
a node list object is returned.</p>
<p>A node list object represents a list of nodes, in the same order as in the
XML.</p>
<p>Nodes in the node list are accessed with index numbers starting from 0.</p>
<p>The following image represents a node list of the <title> elements in "<a target="_blank" href="books.xml">books.xml</a>":</p>
<img border="0" src="nodelist.gif" alt="DOM node list" /><br />
<p>The following code fragment loads "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> and returns a node list of title elements in "books.xml":</p>
<table class="ex" id="table38" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title");</pre>
</td>
</tr>
</table>
<p>After the execution of the statement above, x is a node list object.</p>
<p>The following code fragment returns the text from the first <title> element
in the node list (x):</p>
<table class="ex" id="table40" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>txt=x[0].childNodes[0].nodeValue;</pre>
</td>
</tr>
</table>
<p>After the execution of the statement above, txt = "Everyday Italian".</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_nodelist">Try it yourself.</a></p>
<hr />
<h2>Node List Length</h2>
<p>A node list object keeps itself up-to-date. If an element is
deleted or added, the list is automatically
updated.</p>
<p>The length property of a node list is
the number of nodes in the list.</p>
<p>The following code fragment loads "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> and returns the number of <title> elements in "books.xml":</p>
<table class="ex" id="table19" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName('title').length;</pre>
</td>
</tr>
</table>
<p>After the execution of the statement above, x = 4.</p>
<p>The length of the node list can be used to loop through all the elements in
the list.</p>
<p>The following code fragment uses the length property to loop through the list
of <title> elements:</p>
<table class="ex" id="table22" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>//the x variable will hold a node list
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>Output:</p>
<table class="ex" id="table23" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML</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>Set the x variable to hold a node list of all title elements</li>
<li>Output the value from the text node of all <title> elements</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_nodelist_length">Try it yourself.</a></p>
<hr />
<h2>DOM Attribute List (Named Node Map)</h2>
<p>The attributes property of an element node returns a list of attribute nodes.</p>
<p>This is called a named node map, and is similar to a node list, except for
some differences in methods and properties.</p>
<p>A attribute list keeps itself up-to-date. If an attribute is
deleted or added, the list is automatically
updated.</p>
<p>The following code fragment loads "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> and returns a list of attribute nodes from the first <book> element in "books.xml":</p>
<table class="ex" id="table43" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName('book')[0].attributes;</pre>
</td>
</tr>
</table>
<p>After the execution of the code above, x.length = is the number of attributes
and x.getNamedItem() can be used to return an attribute node.</p>
<p>The following code fragment displays the value of the "category"
attribute, and the number of attributes, of a book:</p>
<table class="ex" id="table44" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("book")[0].attributes;</pre>
<pre>document.write(x.getNamedItem("category").nodeValue);
document.write("<br />" + x.length);</pre>
</td>
</tr>
</table>
<p>Output:</p>
<table class="ex" id="table45" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>cooking
1</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>Set the x variable to hold a list of all attributes of the first <book>
element</li>
<li>Output the value from the "category" attribute</li>
<li>Output the length of the attribute list</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_attributes">Try it yourself.</a></p>
<hr />
<a href="dom_nodes_info.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_traverse.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 + -