dd

XML DOM 创建节点


在线实例

下面的实例使用 XML 文件 books.xml。
函数 loadXMLDoc(),位于外部 JavaScript 中,用于加载 XML 文件。

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

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("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() 把它插入一个元素中。

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("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() 为一个元素创建一个新的属性。

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("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() 把它添加到一个元素中。

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("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() 把它添加到一个元素中。

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("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() 来创建一个注释节点,并使用 appendChild() 把它添加到一个元素中。

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("books.xml");

newComment=xmlDoc.createComment("Revised April 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> 元素追加这个元素节点

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

尝试一下

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book");

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
y=xmlDoc.getElementsByTagName("title");
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. 设置属性节点的值为 "first"
  4. 向第一个 <title> 元素添加这个新的属性节点

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

尝试一下

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("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>

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

使用 setAttribute() 创建属性

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

实例

xmlDoc=loadXMLDoc("books.xml");

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

实例解释:

  1. 使用 loadXMLDoc() 把 "books.xml" 载入 xmlDoc 中
  2. 为第一个 <book> 元素设置(创建)值为 "first" 的 "edition" 属性

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

尝试一下

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book");

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
y=xmlDoc.getElementsByTagName("title");
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>

创建文本节点

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> 元素追加新的元素节点

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

创建 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 节点

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

尝试一下

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("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> 元素

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

尝试一下

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>

<script>
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');

for (i=0;i<x.length;i++)
{
newComment=xmlDoc.createComment("Revised April 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>