javascript and XML
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(tableEle ment);
}
</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>
|