XML DOM getElementsByTagNameNS() 方法

定義和用法
getElementsByTagNameNS() 方法返回帶有指定名稱和命名空間的所有元素的 NodeList。
語法
getElementsByTagNameNS(ns,name)
參數 | 描述 |
---|---|
ns | 字串,規定要搜索的命名空間名稱。值 "*" 匹配所有的標籤。 |
name | 字串,規定要搜索的標籤名。值 "*" 匹配所有的標籤。 |
實例
下麵的代碼片段使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中,並把帶有命名空間的元素節點添加到每個 <book> 元素:
實例
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
var newel,newtext;
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElementNS("p","edition");
newtext=xmlDoc.createTextNode("First");
newel.appendChild(newtext);
x[i].appendChild(newel);
}
// 輸出所有 title 和 edition
y=xmlDoc.getElementsByTagName("title");
z=xmlDoc.getElementsByTagNameNS("p","edition");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write(" - ");
document.write(z[i].childNodes[0].nodeValue);
document.write(" edition.");
document.write(" Namespace: ");
document.write(z[i].namespaceURI);
document.write("<br>");
}
