2012-07-04 194 views
2

我是XML編碼新手,現在我正在網站上工作,以幫助我提高技能。我認爲我爲頁面提供了所有的語法,但它似乎沒有工作。以下是索引頁面的代碼以及我正在嘗試使用的xml文件。XML問題 - 加載空白頁面

<html> 
<body> 

<script type="text/javascript"> 
var link= "http://api.remix.bestbuy.com/v1/stores(area(55423,10))+products(name=playstation*)?show=storeId,name,products.sku,products.name&page=1&apiKey=ydpyq9h9cmpmzaakbawv9mzk"; 
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","test.xml",false); 
xmlhttp.send(); 
xmlDoc=xmlhttp.responseXML; 
document.write("<table border='1'>"); 
var x = xmlDoc.getElementsByTagName("name"); 
for (i=0;i<5;i++) 
    { 
    document.write("<tr><td>"); 
    document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue); 
    document.write("Step 2"); 
    document.write("</td><td>"); 
    document.write(x[i].getElementsByTagName("distance")[0].childNodes[0].nodeValue); 
    document.write("</td></tr>"); 
    } 
document.write("</table>"); 
</script> 

</body> 
</html> 

,我下車的樣本百思買請求XML

<stores warnings="Distances are in terms of miles (default). Your product criteria matches too many records. That exceeds number of records that we allow on the product side of a store-product query. We've automatically truncated the products down to the first 100. These results are not complete. Avoid this by narrowing the number of products in your query." currentPage="1" totalPages="1" from="1" to="10" total="10" queryTime="0.015" totalTime="0.153" canonicalUrl="/v1/stores(area("55423",10))+products(name="playstation*")?show=storeId,name,products.sku,products.name&apiKey=ydpyq9h9cmpmzaakbawv9mzk" partial="false"> 
    <store> 
    <storeId>281</storeId> 
    <name>Richfield</name> 
    <products> 
     <product> 
     <sku>9984133</sku> 
     <name>Assassin's Creed Brotherhood - PlayStation 3</name> 
     </product> 
     <product> 
     <sku>7917141</sku> 
     <name>Assassin's Creed Greatest Hits - PlayStation 3</name> 
     </product> 
     <product> 
     <sku>2613621</sku> 
     <name>Assassin's Creed: Revelations - PlayStation 3</name> 
     </product> 
     <product> 
     <sku>3109269</sku> 
     <name>Assassin's Creed: Revelations Collector's Edition (Game Guide) - Xbox 360, PlayStation 3, Windows</name> 
     </product> 
     <product> 
     <sku>3109065</sku> 
     <name>Assassin's Creed: Revelations The Complete Official Guide (Game Guide) - Xbox 360, PlayStation 3, Windows</name> 
     </product> 
     <product> 
     <sku>9927337</sku> 
     <name>Batman: Arkham Asylum Game of the Year Edition - PlayStation 3</name> 
     </product> 
     <product> 
     <sku>3116162</sku> 
     <name>Batman: Arkham City (Signature Series Game Guide) - Windows, PlayStation 3, Xbox 360</name> 
     </product> 
     <product> 
     <sku>5260722</sku> 
     <name>Batman: Arkham City - Game of the Year Edition - PlayStation 3</name> 
     </product> 
     <product> 
     <sku>2173056</sku> 
     <name>Batman: Arkham City - PlayStation 3</name> 
     </product> 
     <product> 
     <sku>3550073</sku> 
     <name>Battlefield 3 (Game Guide) - Xbox 360, PlayStation 3, Windows</name> 
     </product> 
</products> 
    </store> 
</stores> 

回答

1

你將不得不把該數據在onreadystatechange事件xmlhttp對象的裝載後執行代碼:http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

xmlhttp.onreadystatechange = function() { 
    if (httprequest.readyState == 4) { 
     // Response finished 
     if (httprequest.status = 200) { 
      // everything OK 
      // Parse the data 
     } else { 
      // Something gone wrong with the request 
     } 
    } 
} 

編輯:
羅在你的代碼現在,我意識到你正在使用var x = xmlDoc.getElementsByTagName("name");。這將選擇每個標籤name,包括內部。嘗試使用store獲取每個名稱而不是名稱,然後查找product

另外,您在for循環中使用硬編碼限制。使用x.length代替:for (var i=0; i<x.length;i++)

+0

這是我收到的時候,當我通過鍍鉻 調試錯誤「遺漏的類型錯誤:無法調用空的‘的getElementsByTagName’」 –

+0

是否已進行了更改,我說的嗎? – rcdmk

+0

我試圖但不確定我是否做正確的事情。我將其餘的代碼置於狀態200狀態?如果您可以請您發佈編輯的代碼,它會有所幫助。 –