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

📄 dom_nodes_get.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 - 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 &lt;title&gt; 
element in &quot;books.xml&quot;</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 &quot;category&quot; 
attribute of the first &lt;title&gt; element in &quot;books.xml&quot;.</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 &quot;<a target="_blank" href="books.xml">books.xml</a>&quot; 
	into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> and retrieves the first &lt;title&gt; element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table24">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;)[0];</pre>
</td></tr>
</table>
<p>
The childNodes property returns a list of child nodes. The &lt;title&gt; element has 
only one child node. It is a text node.</p>
<p>
The following code retrieves the text node of the &lt;title&gt; element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table21">
<tr><td>
<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;)[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(&quot;title&quot;)[0];
y=x.childNodes[0];
txt=y.nodeValue;</pre>
</td></tr>
</table>
<p>Result:&nbsp; txt = &quot;Everyday Italian&quot;</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_getelementsbytagname">Try 
it yourself</a></p>
<p>Loop through all &lt;title&gt; 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 &quot;lang&quot; attribute of the first 
&lt;title&gt; element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table22">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>txt=xmlDoc.getElementsByTagName(&quot;title&quot;)[0].getAttribute(&quot;lang&quot;);</pre>
</td></tr>
</table>
<p>Result:&nbsp; txt = &quot;en&quot;</p>
<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>Set the txt variable to be the value of the &quot;lang&quot; 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 &lt;book&gt; elements and get their &quot;category&quot; 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 &quot;lang&quot; attribute of the first 
&lt;title&gt; element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table27">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;)[0].getAttributeNode(&quot;lang&quot;);
txt=x.nodeValue;</pre>
</td></tr>
</table>
<p>Result:&nbsp; txt = &quot;en&quot;</p>
<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 &quot;lang&quot; attribute node of the first &lt;title&gt; 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 &lt;book&gt; elements and get their &quot;category&quot; 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 + -