Parsing XML data using Javascript -
I have moved the basic information to XML and now I am trying to parse XML data using Javascript. I have taken help from w3schools & amp; The following code is written but not parsing and an empty page is displayed. Please help ...
The XML file being used is books.xml :
& lt; Bookstore & gt; & Lt; Book class = "cooking" & gt; & Lt; Title lang = "en" & gt; Daily Italian & lt; / Title & gt; & Lt; Author & gt; Giada de Laurentiis & lt; / Author & gt; & Lt; Years & gt; 2005 & lt; / Year & gt; & Lt; Price & gt; 30.00 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; Book category = "kids" & gt; & Lt; Title lang = "en" & gt; Harry Potter & lt; / Title & gt; & Lt; Author & gt; JK's rolling & lt; / Author & gt; & Lt; Years & gt; 2005 & lt; / Year & gt; & Lt; Price & gt; 29.99 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; / Bookstore & gt;
HTML is the code:
& lt ;! DOCTYPE html & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Script & gt; Function Load XMLDoc (filename) {if (window.XMLHttpRequest) {xhttp = new XMLHttpRequest (); } Xhttp.open ("GET", filename, wrong); Xhttp.send (); Return xhttp.responseXML; } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Script & gt; Var xmlDoc = loadXMLDoc ("C: /Users/A/Desktop/books.xml"); Document.write (xmlDoc.getElementsByTagName ("title") [0]. ChildNode [0] .nodeValue + "& lt; br & gt;"); Document.write (xmlDoc.getElementsByTagName ("author") [0]. ChildNode [0] .nodeValue + "
"); Document.write (xmlDoc.getElementsByTagName ("year") [0] .childNodes [0] .nodeValue); & Lt; / Script & gt; & Lt; / Body & gt; & Lt; / Html & gt;
I've tried your code, it works (I'm Mozilla Firefox 28 I'm using Mac OSX).
Remember that the directory separator from the Windows system is a backslash and there is no slash / try switching it to your XML path. You may see an empty page because you can not get your AJAX request to that XML because you did not give a fallback. If you are using firebug then you should see error "404 not found" while trying to get that XML.
Comments
Post a Comment