下面是根据XPath教程的一次查询尝试!

  <script type="text/javascript">
        function loadXMLDoc(fileName) {
            if (window.XMLHttpRequest) {
                xhttp = new XMLHttpRequest();
            }
            else {
                //IE5 
                xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhttp.open("GET", fileName, false);
            xhttp.send("");
            return xhttp.responseXML;
        }
        xml = loadXMLDoc("Book.xml");
        var path = "/bookstore/book/title";
        if (window.ActiveXObject) {
            var nodes = xml.selectNodes(path);
            for (i = 0; i < nodes.length; i++) {
                document.write(nodes[i].childNodes[0].nodeValue);
                document.write("<br/>");
            }
        }//Firefox之类浏览器
        else if (document.implementation && document.implementation.createDocument) {
            var nodes = xml.evaluate(path, xml, null, XPathReslt.ANY_TYPE, null);
            var result = nodes.iterateNext();
            while (result) {
                document.write(result.childNodes[0].nodeValue);
                document.write("<br/>");
                result = nodes.iterateNext();
            }
        }
    </script>

 

 

 

 

posted on 2012-05-04 10:28  gzh4455  阅读(278)  评论(0)    收藏  举报