javaScript to access XML question...

Associate
Joined
10 Jun 2005
Posts
326
Location
Kent
this is my current file:

Code:
<html>
<head>

<script type="text/javascript">

function loadXML() {
	var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	xmlDoc.load("pizza.xml");

	nodes=xmlDoc.documentElement.childNodes;
	l1left.innerText = nodes(0).attributes(0).text;
	l1right.innerText = nodes(0).childNodes.length;
}

</script>

<title>PizzaList</title>
</head>

<body onload="loadXML()" bgcolor=#a0a000>

<h1>Pizza Companies in Portsmouth</h1>

<table width="800px" bgcolor=#a0a0a0 border="2px">
<tr>
<td><b>Company Name</b></td> <td><b>Number of Branches</b></td>
</tr>

<tr>
<td><span id="l1left"></span></td>
<td><span id="l1right"></span></td>
</tr>

</body>
</html>

and is based on an XML file in this format:

Code:
<pizzaList>
  <company name="Company 1">
    <branch>
      .....
    </branch>
  </company>
  <company name="Company 2">
    <branch>
      .....
    </branch>
    <branch>
      .....
    </branch>
  </company>
</pizzaList>

The question i have is how to get it to do this for each "Company" element in my XML file. So that i basically have a table with
Code:
Company Name       Number of Branches 
Company 1             3
Company 2             6
Company 3             1
etc...

do i have to have the table made from within the script part of the html file?
its basically got to stand up to me adding a new "Company" element in the .xml and then that gets outputted too.
note that it has to be in javaScript as thats what part of the coursework is on.

thanks in advance!!
 
I think the table has to be generated by the javascript.

something like:

nodes=xmlDoc.documentElement.childNodes;
tablestring = '<table>';
for(x = 0; x < nodes.length; x++)
{
tablestring += '<tr><td>'+nodes(x).attributes(0).text+'</td>';
tablestring += '<td>'+nodes(x).childNodes.length+'</td></tr>';
}
tablestring += '</table>';
document.write(tablestring);

would do I guess, though you could probably do better.
 
Back
Top Bottom