• 코드:
​x
 
1
<!DOCTYPE html>
2
<html lang="ko">
3
​
4
<head>
5
    <meta charset="UTF-8">
6
    <title>XML Xpath</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
                    findResult(xmlHttp);
13
                }
14
            };
15
            xmlHttp.open("GET", "/examples/media/programming_languages.xml", true);
16
            xmlHttp.send();
17
        }
18
​
19
        function findResult(xmlHttp) {
20
            var xmlObj, path, result, nodeList, node;
21
            xmlObj = xmlHttp.responseXML;
22
            path = "//version";
23
            result = "";
24
​
25
            // 익스플로러를 위한 코드
26
            if (window.ActiveXObject !== undefined || xmlHttp.responseType == "msxml-document") {
27
                xmlObj.setProperty("SelectionLanguage", "XPath");
28
                nodeList = xmlObj.selectNodes(path);
29
​
30
                for (i=0; i<nodeList.length; i++) {
31
                    result += nodeList[i].text + "<br>";
32
                }
33
            // 익스플로러를 제외한 브라우저를 위한 코드
34
            } else if (document.implementation && document.implementation.createDocument) {
35
                nodeList = xmlObj.evaluate(path, xmlObj, null, XPathResult.ANY_TYPE, null);
36
                node = nodeList.iterateNext();
37
​
38
                while (node) {
39
                    result += node.firstChild.nodeValue + "<br>";
40
                    node = nodeList.iterateNext();
41
                }
42
            }
43
            
44
            document.getElementById("text").innerHTML = result;
45
        }
46
    </script>
47
</head>
48
​
49
<body>
50
​
51
    <h1></h1>
52
    <button onclick="loadDoc()">경로 표현식 //version 확인!</button>
53
    <p id="text"></p>
54
    
55
</body>
56
​
57
</html>