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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript to parse XML File

    I have this Javascript running for parsing an XML file, but it only displays the first item in the XML file....I'm sure its something simple I'm missing.
    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("equipment.xml");
     
    document.write("<table cellpadding='10' width='756px'>");
    document.write("<tr>");
    document.write("<td>");
    document.write("Manufacturer");
    document.write("</td>");
     
    document.write("<td>");
    document.write("Model");
    document.write("</td>");
     
    document.write("<td>");
    document.write("Year");
    document.write("</td>");
     
    document.write("<td>");
    document.write("Equipment");
    document.write("</td>");
     
    document.write("<td>");
    document.write("Price");
    document.write("</td>");
     
    document.write("<td>");
    document.write("Options");
    document.write("</td>");
     
    document.write("<td>");
    document.write("Photos");
    document.write("</td>");
    document.write("</tr>");
     
     
    var x=xmlDoc.getElementsByTagName("Activities");
    for (i=0;i<x.length;i++)
    { 
    document.write("<tr>");
    document.write("<td>");
    document.write(
    x[i].getElementsByTagName("Manufacturer")[0].childNodes[0].nodeValue);
    document.write("</td>");
     
    document.write("<td>");
    document.write(
    x[i].getElementsByTagName("Model")[0].childNodes[0].nodeValue);
    document.write("</td>");
     
    document.write("<td>");
    document.write(
    x[i].getElementsByTagName("Year")[0].childNodes[0].nodeValue);
    document.write("</td>");
     
    document.write("<td>");
    document.write(
    x[i].getElementsByTagName("Equipment")[0].childNodes[0].nodeValue);
    document.write("</td>");
     
    document.write("<td>");
    document.write(
    x[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue);
    document.write("</td>");
     
    document.write("<td>");
    document.write(
    x[i].getElementsByTagName("Options")[0].childNodes[0].nodeValue);
    document.write("</td>");
     
    document.write("<td>");
    document.write(
    x[i].getElementsByTagName("Photos")[0].childNodes[0].nodeValue);
    document.write("</td>");
    document.write("</tr>");
    }
    document.write("</table>");
    }
      
    </script>



    This is my XML schema:

    <Activities>
    <Activity></Activity>
    <Activity></Activity>
    <Activity></Activity>
    </Activities>
    Last edited by Alex Vincent; 01-28-2009 at 07:05 AM. Reason: adding code tags

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    I've never parsed xml with javascript before,
    but shouldn't these values be dynamic?

    x[i].getElementsByTagName("Manufacturer")[i].childNodes[i].nodeValue);

    I replaced the 0's with i's

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That code is from the W3C site, so it should be proper..I was just wondering why its only posting the first item.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    can you send me a copy to the page where u found this?

    also please note for future posts you need to WRAP your code

    Code:
     code........

  • #5
    New Coder
    Join Date
    Jan 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

  • #6
    New to the CF scene
    Join Date
    Mar 2009
    Location
    UK
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Example that might help

    I think angst is correct in saying that the values need to be dynamic. I had a vaguely similar situation a while back.

    Have a look at http://userscripts.org/scripts/review/38466

    I had to adapt it slightly to fit my code, but it was a big help.


  •  

    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
    •