XML DOM getElementsByTagNameNS () method
Definition and Usage
getElementsByTagNameNS () method returns a NodeList of all elements with the specified name and namespace.
grammar
getElementsByTagNameNS(ns,name)
参数 | 描述 |
---|---|
ns | 字符串,规定要搜索的命名空间名称。值 "*" 匹配所有的标签。 |
name | 字符串,规定要搜索的标签名。值 "*" 匹配所有的标签。 |
Examples
The following code fragment uses loadXMLDoc () to " the Books.xml " into xmlDoc, and add an element node with a namespace to each <book> element:
Examples
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);
}
//Output all titles and editions
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("
");
}
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);
}
//Output all titles and editions
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("
");
}
try it"
Document Object