I hope that someone will be able to help me, I am new to javaScript and XML, at the moment I am trying to process an xml list of products into an ul list in my HTML page. I would like to fetch all the products from xml and display them as the li elements on the page. I have managed to write code to go through the xml but it only returns the last product from the xml list. How could I get all the products? any suggestions welcomed. my code so far:
<script>
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","product.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("product");
for (i=0;i<x.length;i++)
{
liOpen=("<li><p>");
title=(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
divOpen=("</p><div class='prod-sq-img'>");
image=(x[i].getElementsByTagName("imgfile")[0].childNodes[0].nodeValue);
closingTags=("</div></li>");
txt= liOpen + title + divOpen + image + closingTags ;
document.getElementById("ulList").innerHTML=txt;
}
</script>
Thank you for any help
My other code works fine and it gets all the products from the list but I need to pass it into an DIV, any ideas?
document.write("<ul class='prod-sq'>");
var x=xmlDoc.getElementsByTagName("product");
for (i=0;i<x.length;i++)
{
document.write("<li><p>");
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("</p><div class='prod-sq-img'>");
document.write(x[i].getElementsByTagName("imgfile")[0].childNodes[0].nodeValue);
}
document.write("</ul>");