0

私はjavascriptを学んでいますが、xmlデータをjavascriptsオブジェクトに保存する際に問題がありました。それが何らかの構文データなのかどうかはよくわかりませんが、機能させることはできません:

<!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")[i].childNodes[0].nodeValue;
        year=xmlDoc.getElementsByTagName("year")[i].childNodes[0].nodeValue;
        author=xmlDoc.getElementsByTagName("author")[i].childNodes[0].nodeValue;
        price=xmlDoc.getElementsByTagName("price")[i].childNodes[0].nodeValue;
        booki = new book(title,year,author,price);
        booksArray[i]=booki;
}

for(i=0;i<30;i++){
title= booksArray[i].getTitle;
year= booksArray[i].getYear;
author= booksArray[i].getAuthor;
price= booksArray[i].getPrice;
document.write(title);
document.write(author);
document.write(year);
document.write(price);

}


</script>
</body>
</html>

さて、私がここでやろうとしているのは、xml データをオブジェクト内に格納してから、配列内に格納することです。その後、オブジェクトのメソッドを使用してデータを表示します。データが適切に読み込まれていることはわかっていますが、念のため、xml ファイルを次に示します。

<?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>

私が間違っていることについて何か考えはありますか?ありがとう!

4

0 に答える 0