Método XML DOM getNamedItem()
❮ Objeto NamedNodeMap
Exemplo
O fragmento de código a seguir carrega " books.xml " em xmlDoc, percorre os elementos <book> e imprime os valores dos atributos de categoria:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, att, xmlDoc,
txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for (i = 0; i < x.length; i++) {
att = x.item(i).attributes.getNamedItem("category");
txt += att.value + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
A saída do código acima será:
cooking
children
web
web
Definição e uso
O método getNamedItem() retorna o nó com o nome específico (de um namedNodeMap).
Suporte ao navegador
O método getNamedItem() é suportado em todos os principais navegadores.
Observação: o Internet Explorer 8 e versões anteriores não oferecem suporte a esse método.
Sintaxe
namedNodeMap.getNamedItem(nodeName)
Parâmetros
Parameter | Type | Description |
---|---|---|
nodeName | String | Required. The name of the node to return |
Valor de retorno
Type | Description |
---|---|
Node object | The node with the specified name, , or null if it does not identify any node in the map |
Detalhes técnicos
Versão DOM | Nível básico 1 |
---|
Demonstrações de teste você mesmo
❮ Objeto NamedNodeMap