...

View Full Version : Resolved Javascript XML reader only returns first row



optimus203
06-23-2012, 04: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>

DaveyErwin
06-23-2012, 05:16 PM
it should look like this …

document.write("<td>");
document.write(x[i].getElementsByTagName("first")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(x[i].getElementsByTagName("mi")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(x[i].getElementsByTagName("last")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(x[i].getElementsByTagName("ssn")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(x[i].getElementsByTagName("drug")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(x[i].getElementsByTagName("insurance")[0].childNodes[0].nodeValue);
document.write("</td>");

optimus203
06-23-2012, 05:24 PM
Thanks Davey. I caught that one after posting.

Issue I'm having now is that, if an XML attribute is not present, the script stops running at that point. The way I have it now, in order for the script to run properly, all attributes in XML must be explicitly defined.

Any ideas on how to revise - say if someone doesn't have a "mi" attribute in the xml doc?

DaveyErwin
06-23-2012, 05:31 PM
here is one possibility

document.write("<td>");
if(x[i].getElementsByTagName("mi")[0])
document.write(x[i].getElementsByTagName("mi")[0].childNodes[0].nodeValue);
document.write("</td>");

optimus203
06-23-2012, 05:39 PM
Nice. I was doing something like

(x[i].getElementsByTagName("mi")[0] == null)

But wasn't working. Your method seems like the way to go. I appreciate the efforts and your time. Take care.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum