optimus203
06-23-2012, 03:44 PM
Hey everyone. I've got a XML doc that I'm trying to parse with javascript, but only the first row of XML is being returned. Any ideas why?
<script type="text/javascript">
var xmlDoc = null;
if (window.ActiveXObject)
{
// code for IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{
// code for Mozilla, Firefox, Opera, etc.
xmlDoc = document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc != null)
{
xmlDoc.async = false;
xmlDoc.load("patients.xml");
document.write("<table width=\"100%\"" +
"<tr>" +
"<td style=\"width:15%\">First</td>" +
"<td style=\"width:5%\">MI</td>" +
"<td style=\"width:15%\">Last</td>" +
"<td style=\"width:20%\">SSN</td>" +
"<td style=\"width:25%\">Allergies</td>" +
"<td style=\"width:20%\">Insurance</td>" +
"</tr>");
var x = xmlDoc.getElementsByTagName("patient");
var drug = xmlDoc.getElementsByTagName("drug");
for (var i = 0; i < x.length; i++)
{
document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("first")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("mi")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("last")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("ssn")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("drug")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("insurance")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}
</script>
<script type="text/javascript">
var xmlDoc = null;
if (window.ActiveXObject)
{
// code for IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{
// code for Mozilla, Firefox, Opera, etc.
xmlDoc = document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc != null)
{
xmlDoc.async = false;
xmlDoc.load("patients.xml");
document.write("<table width=\"100%\"" +
"<tr>" +
"<td style=\"width:15%\">First</td>" +
"<td style=\"width:5%\">MI</td>" +
"<td style=\"width:15%\">Last</td>" +
"<td style=\"width:20%\">SSN</td>" +
"<td style=\"width:25%\">Allergies</td>" +
"<td style=\"width:20%\">Insurance</td>" +
"</tr>");
var x = xmlDoc.getElementsByTagName("patient");
var drug = xmlDoc.getElementsByTagName("drug");
for (var i = 0; i < x.length; i++)
{
document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("first")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("mi")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("last")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("ssn")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("drug")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("insurance")[i].childNodes[i].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}
</script>