我是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>
您必须将加载数据后执行的代码放在xmlhttp
对象的onreadystatechange
事件中: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++)