XML DOM childNodes 속성
❮ 노드 객체
예시
다음 코드 조각은 " books.xml "을 xmlDoc에 로드하고 XML 문서의 자식 노드를 표시합니다.
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 x, i, xmlDoc,
txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.childNodes;
for (i = 0; i < x.length; i++) {
txt += "Nodename: " + x[i].nodeName
+
" (nodetype: " + x[i].nodeType
+ ")";
}
document.getElementById("demo").innerHTML
= txt;
}
위 코드의 출력은 다음과 같습니다.
Nodename: bookstore (nodetype: 1)
출력 IE9 이하:
Nodename: xml (nodetype: 7)
Nodename: bookstore (nodetype: 1)
정의 및 사용
childNodes 속성은 지정된 노드에 대한 자식 노드의 NodeList를 반환합니다.
팁: length 속성을 사용하여 자식 노드의 수를 결정한 다음 모든 자식 노드를 반복하고 원하는 정보를 추출할 수 있습니다.
브라우저 지원
childNodes 속성은 모든 주요 브라우저에서 지원됩니다.
통사론
nodeObject.childNodes
기술적 세부 사항
반환 값: | 노드 컬렉션을 나타내는 NodeList 객체 |
---|---|
DOM 버전 | 코어 레벨 1 |
직접 체험해볼 수 있는 데모
❮ 노드 객체