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

📄 dom_nodes_create.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 - Create Nodes</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 Create Nodes</h1>

<a href="dom_nodes_replace.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_add.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<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_createelement1">Create an 
element node</a><br />
This example uses createElement() to create a new element node, and appendChild() to 
add it to a node.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createattribute">Create an attribute 
node using createAttribute </a><br />
This example uses createAttribute() to create a new attribute node, and 
setAttributeNode() to insert it to an element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createattribute3">Create an attribute 
node using setAttribute </a><br />
This example uses setAttribute() to create a new attribute for an element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createelement1">Create a 
text node</a><br />
This example uses createTextNode() to create a new text node, and appendChild() to 
add it to an element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createcdatasection1">Create a CDATA section node</a><br />
This example uses createCDATAsection() to create a CDATA section node, and 
appendChild() to add it to an element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createcomment1">Create a 
comment node</a><br />
This example uses createComment() to create a comment node, and appendChild() to 
add it to an element.</p>
<hr />

<h2>Create a New Element Node</h2>
<p>The createElement() method creates a new element node:</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table18">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);

newel=xmlDoc.createElement(&quot;edition&quot;);

x=xmlDoc.getElementsByTagName(&quot;book&quot;)[0];
x.appendChild(newel);</pre>
</td></tr>
</table>
<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>Create a new element node &lt;edition&gt;</li>
	<li>Append the element node to the first &lt;book&gt; element</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createelement2">Try 
it yourself</a></p>
<p>Loop through and add an element to all &lt;book&gt; elements:
<a target="_blank" href="tryit.asp@filename=try_dom_createelement">Try 
it yourself</a><br />
</p>
<hr />

<h2>Create a New Attribute Node</h2>
<p>The createAttribute() is used to create a new attribute node:</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table31">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>newatt=xmlDoc.createAttribute(&quot;edition&quot;);
newatt.nodeValue=&quot;first&quot;;</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;);
x[0].setAttributeNode(newatt); </pre>
</td></tr>
</table>
<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>Create a new attribute node &quot;edition&quot;</li>
	<li>Set the value of the attribute node to &quot;first&quot;</li>
	<li>Add the new attribute node to the first &lt;title&gt; element</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createattribute">Try 
it yourself</a></p>
<p>Loop through all &lt;title&gt; elements and add a new attribute node:
<a target="_blank" href="tryit.asp@filename=try_dom_createattribute2">Try 
it yourself</a></p>
<p><b>Note:</b> If the attribute already exists, it is replaced by the new one.</p>
<hr />
<h2>Create an Attribute Using setAttribute()</h2>
<p>Since the setAttribute() method creates a new attribute if the attribute does 
not exist, it can be used to create a new attribute.</p>
<table class="ex" id="table32" border="1" cellspacing="0" width="100%">
	<tr>
		<td>
		<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
		<pre>x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute(&quot;edition&quot;,&quot;first&quot;);</pre>
		</td>
	</tr>
</table>
<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 (create) the attribute &quot;edition&quot; with the value &quot;first&quot; 
	for the first &lt;book&gt; element</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createattribute3">Try 
it yourself</a></p>
<p>Loop through all &lt;title&gt; elements and add a new attribute:
<a target="_blank" href="tryit.asp@filename=try_dom_createattribute4">Try 
it yourself</a></p>
<hr />

<h2>Create a Text Node</h2>
<p>The createTextNode() method creates a new text node:</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table25">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>newel=xmlDoc.createElement(&quot;edition&quot;);
newtext=xmlDoc.createTextNode(&quot;first&quot;);
newel.appendChild(newtext);</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;book&quot;)[0];
x.appendChild(newel);</pre>
</td></tr>
</table>
<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>Create a new element node &lt;edition&gt;</li>
	<li>Create a new text node with the text &quot;first&quot;</li>
	<li>Append the new text node to the element node</li>
	<li>Append the new element node to the first &lt;book&gt; element</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createelement1">Try 
it yourself</a></p>
<p>Add an element node, with a text node, to all &lt;book&gt; elements:
<a target="_blank" href="tryit.asp@filename=try_dom_createelement">Try 
it yourself</a></p><hr />

<h2>Create a CDATA Section Node</h2>
<p>The createCDATASection() method creates a new CDATA section node.</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table29">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>newCDATA=xmlDoc.createCDATASection(&quot;Special Offer &amp; Book Sale&quot;);</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;book&quot;)[0];
x.appendChild(newCDATA);</pre>
</td></tr>
</table>
<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>Create a new CDATA section node</li>
	<li>Append the new CDATA node to the first &lt;book&gt; element</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createcdatasection1">Try 
it yourself</a></p>
<p>Loop through, and add a CDATA section, to all &lt;book&gt; elements:
<a target="_blank" href="tryit.asp@filename=try_dom_createcdatasection">Try 
it yourself</a><br />
</p>
<hr />

<h2>Create a Comment Node</h2>
<p>The createComment() method creates a new comment node.</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table30">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>newComment=xmlDoc.createComment(&quot;Revised March 2008&quot;);</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;book&quot;)[0];
x.appendChild(newComment);</pre>
</td></tr>
</table>
<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>Create a new comment node</li>
	<li>Append the new comment node to the first &lt;book&gt; element</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createcomment1">Try 
it yourself</a></p>
<p>Loop through, and add a comment node, to all &lt;book&gt; elements:
<a target="_blank" href="tryit.asp@filename=try_dom_createcomment">Try 
it yourself</a><br />
</p>
<hr />

<a href="dom_nodes_replace.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_add.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 + -