xml實例文檔
我們將在下面的例子中使用這個 XML 文檔:
“books.xml” :
<?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <PRice>30.00</price></book><book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book><book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price></book><book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price></book></bookstore>加載 XML 文檔 所有現(xiàn)代瀏覽器都支持使用 xmlhttpRequest 來加載 XML 文檔的方法。 針對大多數(shù)現(xiàn)代瀏覽器的代碼:
var xmlhttp=new XMLHttpRequest()針對古老的微軟瀏覽器(IE 5 和 6)的代碼: var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
選取節(jié)點 不幸的是,Internet Explorer 和其他處理 XPath 的方式不同。 在我們的例子中,包含適用于大多數(shù)主流瀏覽器的代碼。 Internet Explorer 使用 selectNodes() 方法從 XML 文檔中的選取節(jié)點:
xmlDoc.selectNodes(xpath);Firefox、Chrome、Opera 以及 Safari 使用 evaluate() 方法從 XML 文檔中選取節(jié)點:
xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null);選取所有 title 下面的例子選取所有 title 節(jié)點:
/bookstore/book/title代碼:
<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book/title"http:// code for IEif (window.ActiveXObject){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>結(jié)果:
Harry PotterEveryday ItalianLearning XMLXQuery Kick Start選取第一個 book 的 title 下面的例子選取 bookstore 元素下面的第一個 book 節(jié)點的 title:
/bookstore/book[1]/title代碼:
<html><body><script type="text/Javascript">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("/example/xmle/books.xml");path="/bookstore/book[1]/title";// code for IEif (window.ActiveXObject){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>結(jié)果
Harry Potter這里有一個問題。上面的例子在 IE 和其他瀏覽器中輸出不同的結(jié)果。
IE5 以及更高版本將 [0] 視為第一個節(jié)點,而根據(jù) W3C 的標準,應(yīng)該是 [1]。
為了解決 IE5+ 中 [0] 和 [1] 的問題,可以為 XPath 設(shè)置語言選擇(SelectionLanguage)。
下面的例子選取 bookstore 元素下面的第一個 book 節(jié)點的 title:
xml.setProperty("SelectionLanguage","XPath");xml.selectNodes("/bookstore/book[1]/title");代碼
<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book[1]/title";// code for IEif (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>結(jié)果
Harry Potter選取所有價格 下面的例子選取 price 節(jié)點中的所有文本:
/bookstore/book/price/text()代碼
<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book/price/text()"http:// code for IEif (window.ActiveXObject){var nodes=xml.selectNodes(path);for (i=0;i<nodes.length;i++) { document.write(nodes[i].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.nodeValue + "<br />"); result=nodes.iterateNext(); }}</script></body></html>結(jié)果
29.9930.0039.9549.99選取價格高于 35 的 price 節(jié)點 下面的例子選取價格高于 35 的所有 price 節(jié)點:
/bookstore/book[price>35]/price代碼
<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book[price>35]/price";// code for IEif (window.ActiveXObject){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>結(jié)果
39.9549.99選取價格高于 35 的 title 節(jié)點 下面的例子選取價格高于 35 的所有 title 節(jié)點:
/bookstore/book[price>35]/title代碼
<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book[price>35]/title";// code for IEif (window.ActiveXObject){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>結(jié)果
Learning XMLXQuery Kick Start—–下面有個“頂”字,你懂得O(∩_∩)O哈哈~ —–樂于分享,共同進步! —–更多文章請看:http://blog.csdn.net/duruiqi_fx
新聞熱點
疑難解答