源代码 :
点击运行
<html><!DOCTYPE html> <html> <body> <script> function loadXMLDoc(dname) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",dname,false); xhttp.send(""); return xhttp.responseXML; } xml=loadXMLDoc("books.xml"); path="/bookstore/book[1]/title"; // code for IE if (window.ActiveXObject) { xml.setProperty("SelectionLanguage","XPath"); var nodes=xml.selectNodes(path); for (i=0;i<nodes.length;i++) { document.write(nodes[i].childNodes[0].nodeValue); document.write("<br>"); } } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE,null); var result=nodes.iterateNext(); while (result) { document.write(result.childNodes[0].nodeValue); document.write("<br>"); result=nodes.iterateNext(); } } </script> </body> </html>
运行结果