코드:
결과보기 »
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title>XML Xpath</title> <script> function loadDoc() { var xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() { if(this.status == 200 && this.readyState == this.DONE) { findResult(xmlHttp); } }; xmlHttp.open("GET", "/examples/media/programming_languages.xml", true); xmlHttp.send(); } function findResult(xmlHttp) { var xmlObj, path, result, nodeList, node; xmlObj = xmlHttp.responseXML; path = "descendant::*"; result = ""; // 익스플로러를 위한 코드 if (window.ActiveXObject !== undefined || xmlHttp.responseType == "msxml-document") { xmlObj.setProperty("SelectionLanguage", "XPath"); nodeList = xmlObj.selectNodes(path); for (i=0; i<nodeList.length; i++) { result += nodeList[i].text + "<br>"; } // 익스플로러를 제외한 브라우저를 위한 코드 } else if (document.implementation && document.implementation.createDocument) { nodeList = xmlObj.evaluate(path, xmlObj, null, XPathResult.ANY_TYPE, null); node = nodeList.iterateNext(); while (node) { result += node.firstChild.nodeValue + "<br>"; node = nodeList.iterateNext(); } } document.getElementById("text").innerHTML = result; } </script> </head> <body> <h1></h1> <button onclick="loadDoc()">경로 표현식 descendant::* 확인!</button> <p id="text"></p> </body> </html>