~Chair~ Member
Posts : 13 Join date : 2011-04-18
| Subject: Please help - Dom Parsing XML file to HTML? Wed May 11, 2011 10:19 pm | |
| Please help am stumped - DOM Parsing XML file (am new to using DOM and parsing files) Hi I am practicing with example files I found online and would really appreciate some help relating to the following. I am trying to use DOM to parse and XML file and then display the info retrieved from the XML file in HTML using Javascript The files are working well and validate, but nothing is displayed when I open the file up in a browser only the style sheet background color I am using There are 3 JS functions, one loads the XML, the second gets the info and the third displays it Here is the code, minus the Style sheet I have been trying to figure this out for days now, is it something little that I have missed? please help thanks HTML Code - Code:
-
<?xml version = "1.0" encoding = "utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <head> <title>Bookstore</title>
<script type = "text/javascript"> function loadXMLDoc(books.xml) { var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET",books.xml,false); xmlhttp.send(); return xmlhttp.responseXML; } else if (ActiveXObject("Microsoft.XMLDOM")) { xmlhttp = CreateObject( "Microsoft.XMLDOM" ) xmlhttp.async="false" xmlhttp.load(books.xml) return xmlhttp.responseXML } alert("Error loading document"); return null; }
xmlDoc = loadXMLDoc("books.xml"); var x=xmlDoc.getElementsByTagName("bookstore"); var i=0;
function info(i) { title=(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue); author=(x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue); year=(x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue);
txt="title"<br />"author"<br />"year"<br />"; document.getElementById("showBooks").innerHTML=txt; }
function display() { document.write("<table border='1'; >"); for (var i=0;i<x.length;i++) { document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue); document.write("</td><td>"); document.write(x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue); document.write("</td><td>"); document.write(x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue ); document.write("</td><td>"); } } </script> </head> <body onload ="display()"> <div id='showRecipe'></div> </body> </html>
Here is the XML File Data - Code:
-
<?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> </bookstore>
|
|