loading data from XML file and storing it in javascript objects

aLoxesn

New Member
i'm learning javascript and i had some problems saving xml data in javascripts objects. i don't really know if it's some syntax data or what, but i cant make it work:\[code\]<!DOCTYPE html><html><body><script type="text/javascript">function book(titl,yea,autho,pric){ this.title=titl; this.year=yea; this.author=autho; this.price=pric;this.getTitle = getTit;this.getYear = getYe;this.getAuthor = getAuth;this.getPrice = getPri;}function getTit(){ return this.title;}function getYe(){ return this.year;}function getAuth(){ return this.author;}function getPri(){ return this.price;}var i;var booki;var booksArray;var title;var year;var author;var price;if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("GET","books.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML;booksArray = new Array(); for(i=0;i<30;i++){ title=xmlDoc.getElementsByTagName("title").childNodes[0].nodeValue; year=xmlDoc.getElementsByTagName("year").childNodes[0].nodeValue; author=xmlDoc.getElementsByTagName("author").childNodes[0].nodeValue; price=xmlDoc.getElementsByTagName("price").childNodes[0].nodeValue; booki = new book(title,year,author,price); booksArray=booki;}for(i=0;i<30;i++){title= booksArray.getTitle;year= booksArray.getYear;author= booksArray.getAuthor;price= booksArray.getPrice;document.write(title);document.write(author);document.write(year);document.write(price);}</script></body></html>\[/code\]well, what i'm trying to do here is storing xml data within an object and then within an array. After that I'll show the data using object's methods. I know the data is loading well, but just in case here is the xml file:\[code\]<?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy? --><bookstore><book category="COOKING"><title lang="en">lorem ipsum</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>\[/code\]any idea of what i'm doing wrong? thanks!
 
Back
Top