Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2005, 10:48 AM
Authorized User
 
Join Date: Jan 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>



 
Old February 14th, 2005, 10:50 AM
Authorized User
 
Join Date: Jan 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old February 14th, 2005, 11:03 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I started to debug your code but's a bit cumbersome. Can you just show what the final table should look like and I'll try to help?

--

Joe (Microsoft MVP - XML)
 
Old February 14th, 2005, 11:08 AM
Authorized User
 
Join Date: Jan 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the header part of the table should look like this...

 ___________________________
|name | age | class|
-------------------------------







Similar Threads
Thread Thread Starter Forum Replies Last Post
XML in JavaScript for Mozilla jwalborn Javascript 2 February 6th, 2008 11:47 AM
XML and JavaScript chuck_schuldiner XML 2 March 14th, 2005 03:04 PM
xml & javascript shine XML 1 December 24th, 2003 12:45 PM
XML DOM and Javascript lilu XML 3 September 25th, 2003 07:55 AM
XML, XML Schema, JavaScript, ASP cyberjames2003 XML 0 June 4th, 2003 04:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.