• 코드:
​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
                    removeNode(xmlHttp);
13
                }
14
            };
15
            xmlHttp.open("GET", "/examples/media/programming_languages.xml", true);
16
            xmlHttp.send();
17
        }
18
​
19
        function removeNode(xmlHttp) {
20
            var xmlObj, firstLang, removedNode, node, result, idx;
21
            xmlObj = xmlHttp.responseXML;   // 요청한 데이터를 XML DOM 객체로 반환함.
22
            firstLang = xmlObj.getElementsByTagName("language")[0]; // 첫 번째 <language>요소를 반환함.
23
​
24
            // 변경 전
25
            node = firstLang.firstChild;
26
            result = "변경 전 : <br>";
27
            for(idx = 0; idx < firstLang.childNodes.length; idx++) {
28
                if(node.nodeType == 1) {
29
                    result += node.nodeName + "<br>";
30
                }
31
                node = node.nextSibling;
32
            }
33
            
34
            removedNode = firstLang.removeChild(firstLang.childNodes[3]);   // <category>요소를 제거함.
35
            
36
            // <language>요소의 자식 요소 노드를 모두 출력함.
37
            node = firstLang.firstChild;
38
            result += "<br>변경 후 : <br>";
39
            for(idx = 0; idx < firstLang.childNodes.length; idx++) {
40
                if(node.nodeType == 1) {
41
                    result += node.nodeName + "<br>";
42
                }
43
                node = node.nextSibling;
44
            }
45
            result += "<br>제거된 요소 노드는 " + removedNode.nodeName + "입니다.";
46
            document.getElementById("text").innerHTML = result;
47
        }
48
    </script>
49
</head>
50
​
51
<body>
52
​
53
    <h1>노드의 제거</h1>
54
    <button onclick="loadDoc()">요소 노드 제거!</button>
55
    <p id="text"></p>
56
    
57
</body>
58
​
59
</html>