XML DOM 創(chuàng)建節(jié)點
實例
下面的例子使用 XML 文件 books.xml。
函數(shù) loadXMLDoc(),位于外部 JavaScript 中,用于加載 XML 文件。
- 創(chuàng)建元素節(jié)點
- 本例使用 createElement() 來創(chuàng)建一個新的元素節(jié)點,并使用 appendChild() 把它添加到一個節(jié)點。
- 通過使用 createAttribute 來創(chuàng)建屬性節(jié)點
- 本例使用 createAttribute() 來創(chuàng)建新的屬性節(jié)點,并使用 setAttributeNode() 把該節(jié)點插入一個元素中。
- 通過使用 setAttribute 來創(chuàng)建屬性節(jié)點
- 本例使用 setAttribute() 為一個元素創(chuàng)建一個新的屬性。
- 創(chuàng)建文本節(jié)點
- 本例使用 createTextNode() 來創(chuàng)建新的文本節(jié)點,并使用 appendChild() 把該文本節(jié)點添加到一個元素中。
- 創(chuàng)建一個 CDATA section 節(jié)點
- 本例用 createCDATAsection() 來創(chuàng)建 CDATA section 節(jié)點,并使用 appendChild() 把它添加到元素中。
- 創(chuàng)建注釋節(jié)點
- 本例使用 createComment() 來創(chuàng)建一個 comment 節(jié)點,并使用 appendChild() 把它添加到一個元素中。
創(chuàng)建新的元素節(jié)點
createElement() 方法創(chuàng)建新的元素節(jié)點:
xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel);
例子解釋:
- 通過使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
- 創(chuàng)建一個新的元素節(jié)點 <edition>
- 向第一個 <book> 元素追加這個元素節(jié)點
遍歷并向所有 <book> 元素添加一個元素:TIY
創(chuàng)建新的屬性節(jié)點
createAttribute() 用于創(chuàng)建新的屬性節(jié)點:
xmlDoc=loadXMLDoc("books.xml"); newatt=xmlDoc.createAttribute("edition"); newatt.nodeValue="first"; x=xmlDoc.getElementsByTagName("title"); x[0].setAttributeNode(newatt);
例子解釋:
- 通過使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
- 創(chuàng)建一個新的屬性節(jié)點 "edition"
- 向第一個 <title> 元素添加這個新的屬性節(jié)點
遍歷所有 <title> 元素,并添加一個新的屬性節(jié)點:TIY
注釋:如果該屬性已存在,則被新屬性替代。
通過使用 setAttribute() 來創(chuàng)建屬性
由于 setAttribute() 可以在屬性不存在的情況下創(chuàng)建新的屬性,我們可以使用這個方法來創(chuàng)建新屬性。
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); x[0].setAttribute("edition","first");
例子解釋:
- 通過使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
- 為第一個 <book> 元素設(shè)置(創(chuàng)建)值為 "first" 的屬性
遍歷所有 <title> 元素并添加一個新屬性:TIY
創(chuàng)建文本節(jié)點
createTextNode() 方法創(chuàng)建新的文本節(jié)點:
xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); newtext=xmlDoc.createTextNode("first"); newel.appendChild(newtext); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel);
例子解釋:
- 通過使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
- 創(chuàng)建一個新元素節(jié)點 <edition>
- 創(chuàng)建一個新的文本節(jié)點,其文本是 "first"
- 向這個元素節(jié)點追加新的文本節(jié)點
- 向第一個 <book> 元素追加新的元素節(jié)點
向所有 <book> 元素添加一個帶有文本節(jié)點的元素節(jié)點:TIY
創(chuàng)建一個 CDATA Section 節(jié)點
createCDATASection() 方法創(chuàng)建一個新的 CDATA section 節(jié)點。
xmlDoc=loadXMLDoc("books.xml"); newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newCDATA);
例子解釋:
- 通過使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
- 創(chuàng)建一個新的 CDATA section 節(jié)點
- 向第一個 <book> 元素追加這個新的 CDATA section 節(jié)點
遍歷并向所有 <book> 元素添加一個 CDATA section:TIY
創(chuàng)建注釋節(jié)點
createComment() 方法創(chuàng)建一個新的注釋節(jié)點。
xmlDoc=loadXMLDoc("books.xml"); newComment=xmlDoc.createComment("Revised March 2008"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newComment);
例子解釋:
- 通過使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
- 創(chuàng)建一個新的注釋節(jié)點
- 把這個新的注釋節(jié)點追加到第一個 <book> 元素
循環(huán)并向所有 <book> 元素添加一個 comment 節(jié)點:TIY