📄 dom_nodes_get.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 - Get Node Values</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 Get Node Values</h1>
<a href="dom_nodes_navigate.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_set.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<hr />
<p class="intro">The nodeValue property is used to get the text value of a node.</p>
<p class="intro">The getAttribute() method returns the value of an attribute.</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_getelementsbytagname">Get
an element's value</a><br />
This example uses the getElementsByTagname() method to get the first <title>
element in "books.xml"</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_getattribute">Get an
attribute's value</a><br />
This example uses the getAttribute() method to get the value of the "category"
attribute of the first <title> element in "books.xml".</p>
<hr />
<h2>Get the Value of an Element</h2>
<p>In the DOM, everything is a node. Element nodes does not have a text value.</p>
<p>The text of an element node is stored in a child node. This node is called a
text node.</p>
<p>The way to get the text of an element, is to get the value of the child
node (text node).</p>
<hr />
<h2>Get an Element Value</h2>
<p>The getElementsByTagName() method returns a node list containing all
elements with the specified tag name in the same order as they appear in the
source document.</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 retrieves the first <title> element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table24">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title")[0];</pre>
</td></tr>
</table>
<p>
The childNodes property returns a list of child nodes. The <title> element has
only one child node. It is a text node.</p>
<p>
The following code retrieves the text node of the <title> element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table21">
<tr><td>
<pre>x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];</pre>
</td></tr>
</table>
<p>
The nodeValue property returns the text value of the text node:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table26">
<tr><td>
<pre>x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
txt=y.nodeValue;</pre>
</td></tr>
</table>
<p>Result: txt = "Everyday Italian"</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_getelementsbytagname">Try
it yourself</a></p>
<p>Loop through all <title> elements:
<a target="_blank" href="tryit.asp@filename=try_dom_getelementsbytagname1">Try
it yourself</a></p>
<hr />
<h2>Get the Value of an Attribute</h2>
<p>In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have
text values.</p>
<p>The way to get the value of an attribute, is to get its text value.</p>
<p>This can be done using the getAttribute() method or using the nodeValue
property of the attribute node.</p>
<hr />
<h2>Get an Attribute Value - getAttribute()</h2>
<p>The getAttribute() method returns an attribute <b>value</b>.</p>
<p>
The following code retrieves the text value of the "lang" attribute of the first
<title> element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table22">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");</pre>
</td></tr>
</table>
<p>Result: txt = "en"</p>
<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 txt variable to be the value of the "lang" attribute of the
first title element node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_getattribute">Try it
yourself</a></p>
<p>Loop through all <book> elements and get their "category" attributes:
<a target="_blank" href="tryit.asp@filename=try_dom_getattribute1">Try
it yourself</a><br />
</p>
<hr />
<h2>Get an Attribute Value - getAttributeNode()</h2>
<p>The getAttributeNode() method returns an attribute <b>node</b>.</p>
<p>
The following code retrieves the text value of the "lang" attribute of the first
<title> element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table27">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");
txt=x.nodeValue;</pre>
</td></tr>
</table>
<p>Result: txt = "en"</p>
<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 "lang" attribute node of the first <title> element node</li>
<li>Set the txt variable to be the value of the attribute</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_getattributenode">Try it
yourself</a></p>
<p>Loop through all <book> elements and get their "category" attributes:
<a target="_blank" href="tryit.asp@filename=try_dom_getattributenode2">Try
it yourself</a></p>
<hr />
<a href="dom_nodes_navigate.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_set.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 + -