XML DOM节点操作:创建节点

下面的例子使用 XML 文件 books.xml

函数 loadXMLDoc(),位于外部 JavaScript 中,用于加载 XML 文件。

创建元素节点

本例使用 createElement() 来创建一个新的元素节点,并使用 appendChild() 把它添加到一个节点。

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

//Output title and edition
document.write(x.getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x.getElementsByTagName("edition")[0].childNodes[0].nodeValue);
</script>
</body>
</html>

通过使用 createAttribute 来创建属性节点

本例使用 createAttribute() 来创建新的属性节点,并使用 setAttributeNode() 把该节点插入一个元素中。

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";

x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);

document.write("Edition: ");
document.write(x[0].getAttribute("edition"));

</script>
</body>
</html>

通过使用 setAttribute 来创建属性节点

本例使用 setAttribute() 为一个元素创建一个新的属性。

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

x=xmlDoc.getElementsByTagName("title");

x[0].setAttribute("edition","first");

document.write("Edition: ");
document.write(x[0].getAttribute("edition"));

</script>
</body>
</html>

创建文本节点

本例使用 createTextNode() 来创建新的文本节点,并使用 appendChild() 把该文本节点添加到一个元素中。

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

//Output title and edition
document.write(x.getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x.getElementsByTagName("edition")[0].childNodes[0].nodeValue);
</script>
</body>
</html>

创建一个 CDATA section 节点

本例用 createCDATAsection() 来创建 CDATA section 节点,并使用 appendChild() 把它添加到元素中。

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);

document.write(x.lastChild.nodeValue);
</script>
</body>
</html>

创建注释节点

本例使用 createComment() 来创建一个 comment 节点,并使用 appendChild() 把它添加到一个元素中。

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newComment=xmlDoc.createComment("Revised March 2008");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);

document.write(x.lastChild.nodeValue);
</script>
</body>
</html>

创建新的元素节点

createElement() 方法创建新的元素节点:

xmlDoc=loadXMLDoc("books.xml");

newel=xmlDoc.createElement("edition");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

例子解释:

  1. 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  2. 创建一个新的元素节点 <edition>
  3. 向第一个 <book> 元素追加这个元素节点
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newel=xmlDoc.createElement("edition");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

document.write(x.getElementsByTagName("edition")[0].nodeName);
</script>
</body>
</html>

遍历并向所有 <book> 元素添加一个元素:

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

var x=xmlDoc.getElementsByTagName('book');
var newel,newtext

for (i=0;i<x.length;i++)
  {
  newel=xmlDoc.createElement('edition');
  newtext=xmlDoc.createTextNode('First');
  newel.appendChild(newtext);
  x[i].appendChild(newel);
  }

//Output all titles and editions
var y=xmlDoc.getElementsByTagName("title");
var z=xmlDoc.getElementsByTagName("edition");
for (i=0;i<y.length;i++)
  {
  document.write(y[i].childNodes[0].nodeValue);
  document.write(" - Edition: ");
  document.write(z[i].childNodes[0].nodeValue);
  document.write("<br />");
  }
</script>
</body>
</html>

创建新的属性节点

createAttribute() 用于创建新的属性节点:

xmlDoc=loadXMLDoc("books.xml");

newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";

x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt); 

例子解释:

  1. 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  2. 创建一个新的属性节点 “edition”
  3. 向第一个 <title> 元素添加这个新的属性节点
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";

x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);

document.write("Edition: ");
document.write(x[0].getAttribute("edition"));

</script>
</body>
</html>

遍历所有 <title> 元素,并添加一个新的属性节点:

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

x=xmlDoc.getElementsByTagName("title");

for (i=0;i<x.length;i++)
{
newatt=xmlDoc.createAttribute("edition");
newatt.value="first";
x[i].setAttributeNode(newatt);
}

//Output all "edition" attribute values
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x[i].getAttribute("edition"));
document.write("<br />");
}
</script>
</body>
</html>

注释:如果该属性已存在,则被新属性替代。

通过使用 setAttribute() 来创建属性

由于 setAttribute() 可以在属性不存在的情况下创建新的属性,我们可以使用这个方法来创建新属性。

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");

例子解释:

  1. 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  2. 为第一个 <book> 元素设置(创建)值为 “first” 的属性
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

x=xmlDoc.getElementsByTagName("title");

x[0].setAttribute("edition","first");

document.write("Edition: ");
document.write(x[0].getAttribute("edition"));

</script>
</body>
</html>

遍历所有 <title> 元素并添加一个新属性:

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

x=xmlDoc.getElementsByTagName("title");

//add a new attribute to each title element
for(i=0;i<x.length;i++)
{
x[i].setAttribute("edition","first");
}

//Output title and edition value
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x[i].getAttribute("edition"));
document.write("<br />");
}
</script>
</body>
</html>

创建文本节点

createTextNode() 方法创建新的文本节点:

xmlDoc=loadXMLDoc("books.xml");

newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

例子解释:

  1. 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  2. 创建一个新元素节点 <edition>
  3. 创建一个新的文本节点,其文本是 “first”
  4. 向这个元素节点追加新的文本节点
  5. 向第一个 <book> 元素追加新的元素节点
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

//Output title and edition
document.write(x.getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x.getElementsByTagName("edition")[0].childNodes[0].nodeValue);
</script>
</body>
</html>

向所有 <book> 元素添加一个带有文本节点的元素节点:

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"> 
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

var x=xmlDoc.getElementsByTagName('book');
var newel,newtext

for (i=0;i<x.length;i++)
  {
  newel=xmlDoc.createElement('edition');
  newtext=xmlDoc.createTextNode('First');
  newel.appendChild(newtext);
  x[i].appendChild(newel);
  }

//Output all titles and editions
var y=xmlDoc.getElementsByTagName("title");
var z=xmlDoc.getElementsByTagName("edition");
for (i=0;i<y.length;i++)
  {
  document.write(y[i].childNodes[0].nodeValue);
  document.write(" - Edition: ");
  document.write(z[i].childNodes[0].nodeValue);
  document.write("<br />");
  }
</script>
</body>
</html>

创建一个 CDATA Section 节点

createCDATASection() 方法创建一个新的 CDATA section 节点。

xmlDoc=loadXMLDoc("books.xml");

newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);

例子解释:

  1. 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  2. 创建一个新的 CDATA section 节点
  3. 向第一个 <book> 元素追加这个新的 CDATA section 节点
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);

document.write(x.lastChild.nodeValue);
</script>
</body>
</html>

遍历并向所有 <book> 元素添加一个 CDATA section:

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

x=xmlDoc.getElementsByTagName("book");

newtext="Special Offer & Book Sale";

for (i=0;i<x.length;i++)
{
newCDATA=xmlDoc.createCDATASection(newtext);
x[i].appendChild(newCDATA);
}

for (i=0;i<x.length;i++)
{
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write(" - ");
document.write(x[i].lastChild.nodeValue);
document.write("<br />");
}
</script>
</body>
</html>

创建注释节点

createComment() 方法创建一个新的注释节点。

xmlDoc=loadXMLDoc("books.xml");

newComment=xmlDoc.createComment("Revised March 2008");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);

例子解释:

  1. 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  2. 创建一个新的注释节点
  3. 把这个新的注释节点追加到第一个 <book> 元素
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

newComment=xmlDoc.createComment("Revised March 2008");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);

document.write(x.lastChild.nodeValue);
</script>
</body>
</html>

循环并向所有 <book> 元素添加一个 comment 节点:

<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");

x=xmlDoc.getElementsByTagName('book');

for (i=0;i<x.length;i++)
{
newComment=xmlDoc.createComment("Revised March 2008");
x[i].appendChild(newComment);
}

for (i=0;i<x.length;i++)
{
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write(" - ");
document.write(x[i].lastChild.nodeValue);
document.write("<br />");
}
</script>
</body>
</html>

作者:terry,如若转载,请注明出处:https://www.web176.com/xmldom/10824.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年2月27日
下一篇 2023年2月27日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注