0

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>");
Kugarek
  • 59
  • 9
  • jQuery should help here. http://think2loud.com/224-reading-xml-with-jquery/ – Will Hawker Feb 08 '13 at 08:46
  • You can follow the process described [here](http://stackoverflow.com/questions/8237923/parsing-xml-rss-from-url-using-java-script). Anyway [jQuery](http://stackoverflow.com/questions/14713208/need-help-parsing-xml-with-jquery) also can help a lot. – revoua Feb 08 '13 at 08:49

2 Answers2

0

try this..

     var liOpen=("<li><p>");
      var divOpen=("</p><div class='prod-sq-img'>");  
      var closingTags=("</div></li>");
    var txt;
    var image;
    var title;
var y=xmlDoc.getElementsByTagName("product");
    for (j=0;j<y.length;j++)        {
    var x=xmlDoc.getElementsByTagName("product");
    for (i=0;i<x.length;i++)
      { 
      title=(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
      image=(x[i].getElementsByTagName("imgfile")[0].childNodes[0].nodeValue);
      txt=  liOpen +  title + divOpen + image + closingTags ;
      }

    document.getElementById("ulList").innerHTML=txt;  }
Kingk
  • 995
  • 11
  • 13
  • Hi, thank you for posting an answer, unfortunately the code still produces only the last item from the xml list ;( – Kugarek Feb 08 '13 at 09:11
  • still the same, basically I have other version of the same code which produces the correct output (gets all products from the xml) I need the output to be passed into an div the other code: document.write("
      "); var x=xmlDoc.getElementsByTagName("product"); for (i=0;i

      "); document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue); document.write("

      "); document.write(x[i].getElementsByTagName("imgfile")[0].childNodes[0].nodeValue); } document.write("");
    – Kugarek Feb 08 '13 at 09:24
0

I think changing following line will fix the issue.

document.getElementById("ulList").innerHTML+=txt;
New Developer
  • 3,245
  • 10
  • 41
  • 78