XML DOM 添加節(jié)點(diǎn)


tryitimg.gif嘗試一下 - 實(shí)例


下面的實(shí)例使用 XML 文件 books.xml。
函數(shù) loadXMLDoc(),位于外部 JavaScript 中,用于加載 XML 文件。

在最后一個(gè)子節(jié)點(diǎn)之后添加一個(gè)節(jié)點(diǎn)
本例使用 appendChild() 方法向一個(gè)已有的節(jié)點(diǎn)添加一個(gè)子節(jié)點(diǎn)。

在指定的子節(jié)點(diǎn)之前添加一個(gè)節(jié)點(diǎn)
本例使用 insertBefore() 方法在一個(gè)指定的子節(jié)點(diǎn)之前插入一個(gè)節(jié)點(diǎn)。

添加一個(gè)新屬性
本例使用 setAttribute() 方法添加一個(gè)新的屬性。

向文本節(jié)點(diǎn)添加數(shù)據(jù)
本例使用 insertData() 把數(shù)據(jù)插入一個(gè)已有的文本節(jié)點(diǎn)中。


添加節(jié)點(diǎn) - appendChild()

appendChild() 方法向一個(gè)已有的節(jié)點(diǎn)添加一個(gè)子節(jié)點(diǎn)。

新節(jié)點(diǎn)會(huì)添加(追加)到任何已有的子節(jié)點(diǎn)之后。

注意:如果節(jié)點(diǎn)的位置很重要,請(qǐng)使用 insertBefore() 方法。

下面的代碼片段創(chuàng)建一個(gè)元素(<edition>),并把它添加到第一個(gè) <book> 元素的最后一個(gè)子節(jié)點(diǎn)后面:

實(shí)例

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

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

newel=xmlDoc.createElement("edition");

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

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

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例解釋:

  1. 使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中

  2. 創(chuàng)建一個(gè)新節(jié)點(diǎn) <edition>

  3. 把這個(gè)節(jié)點(diǎn)追加到第一個(gè) <book> 元素

遍歷并向所有 <book> 元素追加一個(gè)元素:嘗試一下


插入節(jié)點(diǎn) - insertBefore()

insertBefore()方法用于在指定的子節(jié)點(diǎn)之前插入節(jié)點(diǎn)。

在被添加的節(jié)點(diǎn)的位置很重要時(shí),此方法很有用:

實(shí)例

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

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

newNode=xmlDoc.createElement("book");

x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book");

document.write("Book elements before: " + y.length);
document.write("<br>");
x.insertBefore(newNode,y[3]);

y=xmlDoc.getElementsByTagName("book");
document.write("Book elements after: " + y.length);
</script>
</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例解釋:

  1. 使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中

  2. 創(chuàng)建一個(gè)新的元素節(jié)點(diǎn) <book>

  3. 把這個(gè)新節(jié)點(diǎn)插到最后一個(gè) <book> 元素節(jié)點(diǎn)之前

如果 insertBefore() 的第二個(gè)參數(shù)是 null,新節(jié)點(diǎn)將被添加到最后一個(gè)已有的子節(jié)點(diǎn)之后。

x.insertBefore(newNode,null)x.appendChild(newNode) 都可以向 x 追加一個(gè)新的子節(jié)點(diǎn)。


添加新屬性

addAtribute() 這個(gè)方法是不存在的。

如果屬性不存在,則 setAttribute() 可創(chuàng)建一個(gè)新的屬性:

實(shí)例

<!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>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例解釋:

  1. 使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中

  2. 把第一個(gè) <book> 元素的 "edition" 屬性的值設(shè)置(創(chuàng)建)為 "first"

注意:如果屬性已存在,setAttribute() 方法將覆蓋已有的值。


向文本節(jié)點(diǎn)添加文本 - insertData()

insertData() 方法將數(shù)據(jù)插入已有的文本節(jié)點(diǎn)中。

insertData() 方法有兩個(gè)參數(shù):

  • offset - 在何處開(kāi)始插入字符(以 0 開(kāi)始)

  • string - 要插入的字符串

下面的代碼片段將把 "Easy" 添加到已加載的 XML 的第一個(gè) <title> 元素的文本節(jié)點(diǎn):

實(shí)例

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

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

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
document.write(x.nodeValue);
x.insertData(0,"Easy ");
document.write("<br>");
document.write(x.nodeValue);

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

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例