Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder optimus203's Avatar
    Join Date
    Sep 2008
    Location
    CT
    Posts
    317
    Thanks
    22
    Thanked 16 Times in 15 Posts

    Javascript XML reader only returns first row

    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?

    Code:
    <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>
    Last edited by optimus203; 06-23-2012 at 04:39 PM.
    Always thank those CF Users who help you solve issues...
    Connecticut Web Design

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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>");
    Last edited by DaveyErwin; 06-23-2012 at 04:19 PM.

  • Users who have thanked DaveyErwin for this post:

    optimus203 (06-23-2012)

  • #3
    Regular Coder optimus203's Avatar
    Join Date
    Sep 2008
    Location
    CT
    Posts
    317
    Thanks
    22
    Thanked 16 Times in 15 Posts
    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?
    Always thank those CF Users who help you solve issues...
    Connecticut Web Design

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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>");

  • Users who have thanked DaveyErwin for this post:

    optimus203 (06-23-2012)

  • #5
    Regular Coder optimus203's Avatar
    Join Date
    Sep 2008
    Location
    CT
    Posts
    317
    Thanks
    22
    Thanked 16 Times in 15 Posts
    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.
    Always thank those CF Users who help you solve issues...
    Connecticut Web Design


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •