I am new to XML coding and I am working on a site right now to help me improve on the skill. I think i got all the syntax right for the page but it seems not to be working. Here’s the code for the index page and also the xml file I am trying to make use of.
<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>
and the XML i got off a sample best buy request
<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>
You will have to put the code that executes after the data is loaded in the
onreadystatechangeevent of thexmlhttpobject: http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.aspEdit:
Looking at your code now I realized that you are using
var x = xmlDoc.getElementsByTagName("name");. This will select EVERY tagname, including the inner one. Try usingstoreto get each one instead of the name and then look forproduct.Also, you are using a hard coded limit in the for loop. Use
x.lengthinstead:for (var i=0; i<x.length;i++)