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를 반환합니다.
통사론
documentObject.childNodes
팁 및 참고 사항
팁: NodeLists의 길이 속성을 사용하여 노드 목록의 노드 수를 결정하십시오. 노드 목록의 길이를 알면 쉽게 루프를 돌고 원하는 값을 추출할 수 있습니다!
❮ 문서 객체