title: Reading XML document with Javascript
body: <p>I am reading an XML data that is hosted on a server, then display it in an HTML document, for it I do the following:</p>
  
  <pre><code>var a1 = xml.getElementsByTagName('a1')[0].childNodes[0];
  var A1 = document.getElementById("A1");
  A1.innerHTML = a1.nodeValue;
  </code></pre>
  
  <p>where a1 is the id of the XML stored on the server. This procedure I have it to do many times, where the only thing that changes is the id of the field, ie, I have this:</p>
  
  <pre><code>var a1 = xml.getElementsByTagName('a1')[0].childNodes[0];
  var a2 = xml.getElementsByTagName('a2')[0].childNodes[0];
  var a3 = xml.getElementsByTagName('a3')[0].childNodes[0];
  var a4 = xml.getElementsByTagName('a4')[0].childNodes[0];
  var a5 = xml.getElementsByTagName('a5')[0].childNodes[0];
  
  var A1 = document.getElementById("A1");
  var A2 = document.getElementById("A2");
  var A3 = document.getElementById("A3");
  var A4 = document.getElementById("A4");
  var A5 = document.getElementById("A5");
  
  A1.innerHTML = a1.nodeValue;
  A2.innerHTML = a2.nodeValue;
  A3.innerHTML = a3.nodeValue;
  A4.innerHTML = a4.nodeValue;
  A5.innerHTML = a5.nodeValue;
  </code></pre>
  
  <p>Everything works fine and brings the data as expected, but my question is this, no way to optimize the code to not use as many lines as necessary, ie use something like</p>
  
  <pre><code>for (i=1;i&lt;=5;i++){
      var a[i] = xml.getElementsByTagName('a[i]')[0].childNodes[0];
  }
  </code></pre>
  
  <p>I do not know, is something that occurs to me, I'm new to this and have been generated that doubt, I appreciate any info, Greetings!</p>