XML DOM attribute nodeType
Ορισμός και χρήση
nodeType
Η ατριβή επιστρέφει τον τύπο του κόμβου που επιλέχθηκε.
Γλωσσή
elementNode.nodeType
Αριθμός κόμβου: | Όνομα κόμβου: |
---|---|
1 | Στοιχείο |
2 | Ατριβή |
3 | Τекст |
4 | CDATA Σektion |
5 | Entity Reference |
6 | Entity |
7 | Processing Instrucion |
8 | Comment |
9 | Document |
10 | Document Type |
11 | Document Fragment |
12 | Notation |
Εξάγωγή
Παράδειγμα 1
Το παρακάτω κώδικας φορτώνει το "books.xml" στο xmlDoc και παίρνει τον τύπο κόμβου από το πρώτο στοιχείο <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; }
Παράδειγμα 2
Παράβλεψη κενών κόμβων κειμένου:
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 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) { // Επεξεργασία μόνο των κόμβων στοιχείων txt += firstNode.childNodes[i].nodeName +""} " = " + firstNode.childNodes[i].childNodes[0].nodeValue + "<br>"; } } document.getElementById("demo").innerHTML = txt; }