• 코드:
​x
 
1
<!DOCTYPE html>
2
<html lang="ko">
3
​
4
<head>
5
    <meta charset="UTF-8">
6
    <title>XML Node</title>
7
    <script>
8
        function loadDoc() {
9
            var xmlHttp = new XMLHttpRequest();
10
            xmlHttp.onreadystatechange = function() {
11
                if(this.status == 200 && this.readyState == this.DONE) {
12
                    searchNode(xmlHttp);
13
                }
14
            };
15
            xmlHttp.open("GET", "/examples/media/programming_languages.xml", true);
16
            xmlHttp.send();
17
        }
18
​
19
        function searchNode(xmlHttp) {
20
            var xmlObj, firstLang, node, result, idx;
21
            xmlObj = xmlHttp.responseXML;   // 요청한 데이터를 XML DOM 객체로 반환함.
22
            firstLang = xmlObj.getElementsByTagName("language")[0]; // 첫 번째 <language>요소를 반환함.
23
            node = firstLang.firstChild;    // 첫 번째 <language>요소의 첫 번째 자식 노드를 반환함.
24
            result = "language 요소의 자식 요소 노드<br>";
25
            for(idx = 0; idx < firstLang.childNodes.length; idx++) {
26
                if(node.nodeType == 1) {    // 요소 노드만을 출력함.
27
                    result += node.nodeName + "<br>";
28
                }
29
                node = node.nextSibling;    // 현재 노드의 바로 다음 형제 노드를 반환함.
30
            }
31
            document.getElementById("text").innerHTML = result;
32
        }
33
    </script>
34
</head>
35
​
36
<body>
37
​
38
    <h1>노드 간의 관계를 이용하여 접근하는 방법</h1>
39
    <button onclick="loadDoc()">노드 접근하기!</button>
40
    <p id="text"></p>
41
    
42
</body>
43
​
44
</html>