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 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript and the XML DOM

    what i am trying to do is by using javascript i am trying to create a table using the information in a xml file ... i have truncated the javascript code to show only the part that i would use to create the header for the table...

    the html code....

    <html>

    <head>
    <Script type="text/javascript">

    var IE=(navigator.appName.indexOf("Explorer")>-1);

    if(IE)
    {

    var xmlDoc=new ActiveXObject("MICROSOFT.XMLDOM");
    xmlDoc.async=false;
    }
    else
    {
    var xmlDoc=document.implementation.createDocument("","doc",null);
    }

    xmlDoc.load("xml1.xml");

    function xmlTable()
    {

    var papaNode=xmlDoc.getElementsByTagName("child");

    //The Table

    var tableElement=document.createElement("TABLE");
    var tableRow=document.createElement("TR");

    //Table Header

    for(i=0;i<papaNode[0].childNodes.length;i++)
    {

    if(papaNode[0].childNodes[i].nodeType!=1)continue;

    var tableCell=document.createElement("TH");
    var theText=document.createTextNode(papaNode[0].childNodes[i].nodeName);
    tableCell.appendChild(theText);
    tableRow.appendChild(tableCell);

    }

    tableElement.appendChild(tableRow);
    document.getElementById("p1").appendChild(tableElement);


    }

    </script>
    </head>

    <body>

    <a href="javascript:xmlTable()">xml table</a>
    <br>
    <p id="p1"></p>

    </body>
    </html>

    and the XML file(xml1.xml) is...

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <group>
    <child>
    <name>Glen Benton</name>
    <age>13</age>
    <class>1B</class>
    </child>
    <child>
    <name>Sean Reinhardt</name>
    <age>12</age>
    <class>1B</class>
    </child>
    <child>
    <name>Angel Ripper</name>
    <age>14</age>
    <class>2C</class>
    </child>
    </group>


    the problem with the above code is that ... when i click on the link "xml table" no data is displayed in the page ... where could i have gone wrong
    the header part of the table should look like this...

    +--------------------------+
    | name | age | class |
    +--------------------------+

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I guess that you must have insert the row's append method inside the loop, not outside


    ...
    var tableElement=document.createElement("TABLE");
    for(i=0;i<papaNode[0].childNodes.length;i++)
    {
    if(papaNode[0].childNodes[i].nodeType!=1)continue;
    var tableRow=document.createElement("TR");
    var tableCell=document.createElement("TH");
    var theText=document.createTextNode(papaNode[0].childNodes[i].nodeName);
    tableCell.appendChild(theText);
    tableRow.appendChild(tableCell);
    tableElement.appendChild(tableRow);
    }
    document.getElementById("p1").appendChild(tableElement);
    ...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    eer, I have notice that TBODY element is missing also....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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