• 코드:
​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
                    createNode(xmlHttp);
13
                }
14
            };
15
            xmlHttp.open("GET", "/examples/media/programming_languages.xml", true);
16
            xmlHttp.send();
17
        }
18
​
19
        function createNode(xmlHttp) {
20
            var xmlObj, versionElement, newNode, newAttrNode;
21
            xmlObj = xmlHttp.responseXML;   // 요청한 데이터를 XML DOM 객체로 반환함.
22
            versionElement = xmlObj.getElementsByTagName("version")[3]; // 네 번째 <version>요소를 반환함.
23
            newAttrNode = xmlObj.createAttribute("anotherVersion"); // 새로운 anotherVersion 속성 노드를 생성함.
24
            newAttrNode.nodeValue = "2.7.12";                       // 새로운 속성 노드에 속성값을 설정함.
25
            versionElement.setAttributeNode(newAttrNode);   // 네 번째 <version>요소에 새로운 속성 노드를 추가함.
26
            document.getElementById("text").innerHTML =
27
                "another version : " + versionElement.getAttribute("anotherVersion");
28
        }
29
    </script>
30
</head>
31
​
32
<body>
33
​
34
    <h1>노드의 생성</h1>
35
    <button onclick="loadDoc()">속성 노드 생성!</button>
36
    <p id="text"></p>
37
    
38
</body>
39
​
40
</html>