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>");
2

2 Answers 2

0

I think changing following line will fix the issue.

document.getElementById("ulList").innerHTML+=txt;
Sign up to request clarification or add additional context in comments.

Comments

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;  }

2 Comments

Hi, thank you for posting an answer, unfortunately the code still produces only the last item from the xml list ;(
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("<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>");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.