XML DOM nodeType 속성
定义和用法
nodeType
属性返回所选节点的节点类型。
语法
elementNode.nodeType
节点编号: | 节点名称: |
---|---|
1 | Element |
2 | Attribute |
3 | Text |
4 | CDATA Section |
5 | entity reference |
6 | entity |
7 | processing instruction |
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; }