/    Sign up×
Community /Pin to ProfileBookmark

XML to JS Objects to HTML Table

I appreciate any help that can be provided. I’m trying to teach myself how to load an XML document to a JavaScript Array and then present the data in an HTML table. I have pieced together the following code from a JavaScript book that I am using to teach myself.

There is an onload event to call the loadxml function which calls the XML2JS function to load the matchData array, then it will call the drawTable function to display the table.

I do not get any errors but the data is not displaying. I am testing in IE. I know I don’t have the Mozilla code correct yet – but just trying to figure it out in IE for now. I don’t know which part of the code may be the problem. THANKS!


********************************************

<html>
<head>
<title>jsobjects</title>
<style type=”text/css”>
table {table-collapse:collapse; border-spacing:0}
td {border:2px groove black; padding:7px; background-color:ccffcc}
th {border:2px groove black; padding:7px; background-color:ccffcc}
.ctr {text-align:center}
</style>

<script language=”JavaScript” type=”text/JavaScript”>

var xmlDoc;

function loadXML()
{
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async=false;
xmlDoc.load(“worldcup.xml”);
var matchData = XML2JS(xmlDoc, “worldcup”);
drawTable(matchData);
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument(“”,””,null);
xmlDoc.load(“worldcup.xml”);
xmlDoc.onload=drawTable(matchData);
}
else
{
alert(‘Your browser cannot handle this script’);
}
}

//convert XML data in to JavaScript array of JavaScript objects
function XML2JS(xmlDoc, containerTag) {
var output = new Array();
var rawData = xmlDoc.getElementsByTagName(containerTag)[0];
var i, j, OneRecord, OneObject;
for (i=0; i < rawData.childNodes.length; i++) {
if (rawData.childNodes[i].nodeType == 1) {
oneRecord = rawData.childNodes[i];
oneObject = output[output.length] = new Object();
for (j = 0; j < oneRecord.childNodes.length; j++) {
if (oneRecord.childNodes[j].nodeType == 1) {
oneObject[oneRecord.childNodes[j].tagName] =
oneRecord.childNodes[j].firstChild.nodeValue;
}
}
}
}
return output;
}

//draw table from ‘jsData’ array of objects
function drawTable(tbody) {
var tr, td;
tbody = document.getElementById(tbody);
//loop through data source
for (var i = 0; i < matchData.length; i++) {
tr = tbody.insertRow(tbody.rows.length);
td = tr.insertCell(tr.cells.length);
td.setAttribute(“align”, “center”);
td.innerHTML = matchData[i].year;
td = tr.insertCell(tr.cells.length);
td.innerHTML = matchData[i].location;
td = tr.insertCell(tr.cells.length);
td.innerHTML = matchData[i].winner;
td = tr.insertCell(tr.cells.length);
td.innerHTML = matchData[i].loser;
td = tr.insertCell(tr.cells.length);
td.setAttribute(“align”, “center”);
td = tr.insertCell(tr.cells.length);
td.innerHTML = matchData[i].winscore + ” – ” + matchData[i].losscore;
}
}

</script>
</head>

<body onload=”loadXML()”>
<table id=”cupFinals”>
<thead>
<tr>
<th>Year</th>
<th>Host Country</th>
<th>Winner</th>
<th>Loser</th>
<th>Score (Win – Lose)</th>
</tr>
</thead>
<tbody id=”matchData”></tbody>
</table>

</body>
</html>

Here’s the XML document:
<xml id=”myData” style=”display:none”>
<worldcup>
<final>
<location>Uruguay</location>
<year>1930</year>
<winner>Uruguay</winner>
<winscore>4</winscore>
<loser>Argentina</loser>
<losscore>2</losscore>
</final>
<final>
<location>Italy</location>
<year>1934</year>
<winner>Italy</winner>
<winscore>2</winscore>
<loser>Czechoslovakia</loser>
<losscore>1</losscore>
</final>
</worldcup>
</xml>

*************************’
Thanks again very much for any help!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@RobDavidFeb 06.2007 — for some reason, IE doesn't display tables data that is dynamically created?

Try using something like

span = document.getElementById('container');

span.innerHTML = "<table>" + dynamicTableBody.innerHTML + "</span>";




..html

<span id='container'></span>


that should work
Copy linkTweet thisAlerts:
@konithomimoFeb 07.2007 — for some reason, IE doesn't display tables data that is dynamically created?

Try using something like

span = document.getElementById('container');

span.innerHTML = "<table>" + dynamicTableBody.innerHTML + "</span>";




..html

<span id='container'></span>


that should work[/QUOTE]

IE does not have a problem with displaying dynamically created data into a table. The problem is that XML does not allow data to be bound to a table cell. In order to have the data displayed in a cell you must first insert it into another object such as a span or div. That means instead of having:

<td>data</td>

you would have

<td><span>data</span></td>
Copy linkTweet thisAlerts:
@RobDavidFeb 07.2007 — This does not display anything in IE (Works in Mozilla though)
[CODE]
var table = document.createElement('table');
var tr = document.createElement('tr');
var td = document.createElement('td');

td.innerHTML = "test table";
tr.appendChild(td);
table.appendChild(tr);
document.body.appendChild(table);
[/CODE]


I didn't mean to say that IE can't create dynamically created tables. Try alerting your table body. If it's all there, than this is your problem?
Copy linkTweet thisAlerts:
@konithomimoFeb 07.2007 — IE requires the tbody:

var tables = document.createElement('table');
var trs = document.createElement('tr');
var tds = document.createElement('td');
var tb = document.createElement('tbody');

tds.appendChild(document.createTextNode('test table'));
trs.appendChild(tds);
tb.appendChild(trs);
tables.appendChild(tb);
document.appendChild(tables);


If you ever look at a table DOMtree in IE it will show the TBODY, in FF it wont.
Copy linkTweet thisAlerts:
@konithomimoFeb 07.2007 — As the OP's code shows, they are using TBODY. The problem with the OP's code is that the script tries to bind data directly to a <td> element, which does not support it, thus the need to put a div/span, or some other element in the cell, and then bind the data to that element.
Copy linkTweet thisAlerts:
@konithomimoFeb 07.2007 — And yes, using the tbody is supported in both browsers. Since tbody is a supported element of a standard table.
Copy linkTweet thisAlerts:
@lsealsauthorFeb 07.2007 — Thanks a lot! I really appreciate the feedback. I'm not sure I understand 100% what the suggestions mean (I am very much a newbie at this!) but they are definitely helping to steer me in the right direction. And, for that, I am most grateful! =)

I did make the following changes to my code above and it worked in IE:

1. declared var=matchData; at the top of the script section

2. in the function loadXML(), i replaced:

var matchData = XML2JS(xmlDoc, "worldcup");

drawTable(matchData);

with

matchData = XML2JS(xmlDoc, "worldcup");
drawTable('matchData');


Thanks again!
Copy linkTweet thisAlerts:
@lsealsauthorFeb 13.2007 — Working from this recommendation, I'm not sure how to proceed. Do I abandon the use of the array completely and load the XML document and access it directly using DOM node referencing (which I am in process of figuring out!)? What is in the 'test table' quotes in your code? Where/how is the xml document data being accessed here? Thanks again!

IE requires the tbody:

var tables = document.createElement('table');
var trs = document.createElement('tr');
var tds = document.createElement('td');
var tb = document.createElement('tbody');

tds.appendChild(document.createTextNode('test table'));
trs.appendChild(tds);
tb.appendChild(trs);
tables.appendChild(tb);
document.appendChild(tables);


If you ever look at a table DOMtree in IE it will show the TBODY, in FF it wont.[/QUOTE]
×

Success!

Help @lseals spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...