XML DOM hasChildNodes() 메서드

정의와 사용법

현재 요소 노드가 자식 노드를 가지고 있다면 hasChildNodes() 메서드는 true를 반환하며, 그렇지 않으면 false를 반환합니다。

문법

elementNode.hasChildNodes()

예제

아래 코드는 "books.xml"를 xmlDoc에 로드하고, 첫 번째 <book> 요소가 어떤 자식 노드를 가지고 있는지 확인합니다:

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('book')[0];
    document.getElementById("demo").innerHTML =
    x.hasChildNodes();
}

직접 테스트해 보세요