📄 dom_methods.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 Methods</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 - Properties and Methods</h1>
<a href="dom_loadxmldoc.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" /></a>
<a href="dom_nodes_access.asp"><img border="0" alt="next" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">Properties and methods define the programming interface to the
XML DOM.</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.<br />
A function, <a href="dom_loadxmldoc.asp">loadXMLString()</a>, in an external JavaScript is used to load the XML
string.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_parsertest">Load and parse an XML file</a></p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_parsertest2">Load and parse an XML
string</a></p>
<hr />
<h2>Programming Interface</h2>
<p>The DOM models XML as a set of node objects. The nodes can be accessed with
JavaScript or other programming languages. In this tutorial we use JavaScript.</p>
<p>The programming interface to the DOM is defined by a set standard properties
and methods.</p>
<p><b>Properties</b> are often referred to as something that is (i.e. nodename
is "book").</p>
<p><b>Methods</b> are often referred to as something that is done (i.e. delete
"book").</p>
<hr />
<h2>XML DOM Properties</h2>
<p>These are some typical DOM properties:</p>
<ul>
<li>x.nodeName - the name of x</li>
<li>x.nodeValue - the value of x</li>
<li>x.parentNode - the parent node of x</li>
<li>x.childNodes - the child nodes of x</li>
<li>x.attributes - the attributes nodes of x</li>
</ul>
<p>Note: In the list above, x is a node object.</p>
<hr />
<h2>XML DOM Methods</h2>
<ul>
<li>x.getElementsByTagName(<i>name</i>) - get all elements with a specified
tag name</li>
<li>x.appendChild(<i>node</i>) - insert a child node to x</li>
<li>x.removeChild(<i>node</i>) - remove a child node from x</li>
</ul>
<p>Note: In the list above, x is a node object.</p>
<hr />
<h2>
Example</h2>
<p>
The JavaScript code to get the text from the first <title> element in books.xml:</p>
<p>
txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue</p>
<p>After the execution of the statement, txt will hold the value "Everyday
Italian" </p>
<p>
Explained:</p>
<ul>
<li><b>xmlDoc</b> - the XML DOM object created by the parser.</li>
<li><b>getElementsByTagName("title")[0]</b> - the first <title> element</li>
<li><b>childNodes[0]</b> - the first child of the <title> element (the text
node)</li>
<li><b>nodeValue</b> - the value of the node (the text itself)</li>
</ul>
<p>In the example above, getElementsByTagName is a method, while childNodes and
nodeValue are properties. </p>
<hr />
<h2>Parsing an XML File - A Cross browser Example</h2>
<p>The following code fragment uses the loadXMLDoc function (described in the
previous chapter) to load <a target="_blank" href="books.xml">books.xml</a> into
the XML parser, and displays data from the first book:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table25">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>document.write(xmlDoc.getElementsByTagName("title")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("author")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("year")
[0].childNodes[0].nodeValue);</pre>
</td></tr>
</table>
<p>Output:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table28">
<tr><td>
<pre>Everyday Italian
Giada De Laurentiis
2005</pre>
</td></tr>
</table>
<p><a target="_blank" href="tryit.asp@filename=try_dom_parsertest">Try it yourself</a>
</p>
<p>In the example above we use childNodes[0] for each text node, even if
there is only one text node for each element. This is because the getElementsByTagName() method always
returns an array.</p>
<hr />
<h2>Parsing an XML String - A Cross browser Example</h2>
<p>The following code loads and parses an XML string:</p>
<p>The following code fragment uses the loadXMLString function (described in the
previous chapter) to load <a target="_blank" href="books.xml">books.xml</a> into
the XML parser, and displays data from the first book:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table27">
<tr><td>
<pre>text="<bookstore>"
text=text+"<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
text=text+"</bookstore>";
xmlDoc=loadXMLString(text);
document.write(xmlDoc.getElementsByTagName("title")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("author")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("year")
[0].childNodes[0].nodeValue);
</pre>
</td></tr>
</table>
<p>Output:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table29">
<tr><td>
<pre>Everyday Italian
Giada De Laurentiis
2005</pre>
</td></tr>
</table>
<p><a target="_blank" href="tryit.asp@filename=try_dom_parsertest2">Try it
yourself</a></p>
<hr />
<a href="dom_loadxmldoc.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="dom_nodes_access.asp"><img border="0" alt="next" src="../images/btn_next.gif" 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 + -