Test.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>
Test.html: <script type= "Text/javascript" >/* @desc load xhr file @author lee [<[email protected]>] @param file path @ Param Async Synchronous or asynchronous true asynchronous Flase synchronization @return xmldoc loaded content */function Loaddoc (file,async=true) {if (window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest (); }else{//code for IE6, IE5 xmlhttp=new activexobject ("Microsoft.XMLHTTP"); if (async = = = True) {Xmlhttp.onreadystatechange = function () {if (Xmlhttp.readystate < 4) { Load in}else if (xmlhttp.readystate = = 4 && xmlhttp.status = = 200) {//success Xmldoc=xmlhttp.responsexml; return xmldoc}else{//Failed}}} xmlhttp.open ("GET", File,async); Xmlhttp.send (); if (async = = = False) {xmldoc=xmlhttp.responsexml; return xmldoc}}var xmldoc = Loaddoc (' Test.xml ', false) var path= "/bookstore/book/titLe "//Code for Ieif (window. ActiveXObject) {var nodes=xmldoc.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=xmldoc.evaluate (path, xmldoc, NULL, xpathresult.any_type, NULL); var result=nodes.iteratenext (); while (result) {document.write (result.childnodes[0].nodevalue); document.write ("<br/>"); Result=nodes.iteratenext (); }}</script>
Output:Harry PotterEveryday ItalianLearning XMLXQuery Kick Start
HTML parsing XML using XPath