Atributo nodeType del DOM XML

Definición y uso

nodeType La propiedad nodeType devuelve el tipo de nodo seleccionado.

Sintaxis

elementNode.nodeType
Número del nodo: Nombre del nodo:
1 Elemento
2 Atributo
3 Texto
4 Sección de datos CDATA
5 Referencia de entidad
6 Entidad
7 Instrucción de procesamiento
8 Comentario
9 Documento
10 Tipo de documento
11 Fragmento de documento
12 Notación

Ejemplo

Ejemplo 1

El siguiente código carga "books.xml" en xmlDoc y obtiene el tipo de nodo del primer elemento <title>:

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 xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("title")[0];
    document.getElementById("demo").innerHTML =
    x.nodeType;
}

Prueba personalmente

Ejemplo 2

Saltar nodos de texto en blanco:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
//Verificar si el primer nodo es un nodo de elemento
function get_firstchild(n) {
    var x = n.firstChild;
    while (x.nodeType != 1) {
        x = x.nextSibling;
    }
    return x;
}
function myFunction(xml) {
    var x, i, txt, xmlDoc, firstNode, xmlDoc;
    xmlDoc = xml.responseXML;
    x = xmlDoc.documentElement;
    txt = "";
    firstNode = get_firstchild(x);
    for (i = 0; i < firstNode.childNodes.length; i++) { 
        if (firstNode.childNodes[i].nodeType == 1) {
            //Procesar solo nodos de elemento
            txt += firstNode.childNodes[i].nodeName +"}}" 
            " = " + 
            firstNode.childNodes[i].childNodes[0].nodeValue + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt; 
}

Prueba personalmente