XML DOM nodeType गुण

वर्णन और उपयोग

nodeType प्रतिनिधित्व करने वाले नोड के नोड टाइप को वापस करता है।

व्याकरण

elementNode.nodeType
नोड संख्या: नोड नाम:
1 एलीमेंट
2 अट्रिब्यूट
3 लेख
4 CDATA खंड
5 एंटिटी रेफरेंस
6 एंटिटी
7 प्रोसेसिंग इन्स्ट्रुक्शन
8 कमेंट
9 डॉक्युमेंट
10 डॉक्युमेंट टाइप
11 डॉक्युमेंट फ्रैग्मेंट
12 नोटेशन

इंस्टांस

उदाहरण 1

नीचे कोड "books.xml" को xmlDoc में लोड करता है और पहले <title> एलेमेंट से नोड क़िस्म प्राप्त करता है:

वार xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   यदि (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
फ़ंक्शन myFunction(xml) {
    वार xmlDoc = xml.responseXML;
    वार x = xmlDoc.getElementsByTagName("title")[0];
    document.getElementById("demo").innerHTML =
    x.nodeType;
}

स्वयं प्रयोग कीजिए

उदाहरण 2

स्किप खाली टेक्स्ट नोड:

वार xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    यदि (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
// Check if the first node is an element node
फ़ंक्शन get_firstchild(n) {
    वार x = n.firstChild;
    जब तक (x.nodeType != 1) {
        x = x.nextSibling;
    }
    वापस x;
}
फ़ंक्शन myFunction(xml) {
    वार x, i, txt, xmlDoc, पहला नोड, xmlDoc;
    xmlDoc = xml.responseXML;
    x = xmlDoc.documentElement;
    txt = "";
    पहला नोड = get_firstchild(x);
    फ़ोर (i = 0; i < पहला नोड के childNodes.length; i++) { 
        यदि (पहला नोड के childNodes[i].nodeType == 1) {
            //Process only element nodes
            txt += firstNode.childNodes[i].nodeName +"}}" 
            " = " + 
            firstNode.childNodes[i].childNodes[0].nodeValue + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt; 
}

स्वयं प्रयोग कीजिए