Here's the code that does all the work (sorry for the lack of
commenting, I haven't gotten there yet):
function fillContent(url, obj) {
var http_request = false;
if( window.XMLHttpRequest ) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if( http_request.overrideMimeType ) {
http_request.overrideMimeType('text/xml');
}
} else if( window.ActiveXObject ) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch( e ) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch( e ) {}
}
}
if( !http_request ) {
alert('Giving up: (Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function() {
alertContents(http_request, obj); };
http_request.open('GET', url, true);
http_request.send(null);
return false;
}
function alertContents(http_request, obj) {
if( http_request.readyState == 4 )
{
if( http_request.status == 200 )
{
var xmldoc = http_request.responseXML;
var root = xmldoc.getElementsByTagName('root');
if( root[0].getAttribute("id") == "Product List" )
{
showProdList(root, obj);
}
} else {
alert('There was a problem with the request.');
alert(http_request.status);
}
}
}
function showProdList(root, obj)
{
var prods = root[0].childNodes;
obj.innerHTML = "";
for( var i=0; i<prods.length; i++ )
{
drawProduct( prods, 0, obj );
}
}
function drawProduct(ele, level, obj)
{
var space = "";
for( var sp=0; sp<level; sp++ )
{
space += " ";
}
var dispName = new String( ele.getAttribute("id") );
var id = new String();
var imgSrc = new String( "images/folder.gif" );
for( var sl=0; sl<dispName.length; sl++ )
{
if( dispName.charAt(sl) != " " && dispName.charAt(sl) != "." )
{
id += dispName.charAt(sl);
}
}
var ch = ele.getElementsByTagName("product");
if( ch.length < 1 )
{
imgSrc = "images/text.gif";
}
obj.innerHTML += " <a id=\"x" + id + "\"
href=\"javascript:Toggle('" + id + "');\">";
obj.innerHTML += " <img src='" + imgSrc + "' width='16'
height='16' hspace='0' vspace='0' border='0'>";
obj.innerHTML += " </a>";
obj.innerHTML += " <b>" + dispName + "</b><br/>";
obj.innerHTML += "<div id='" + id + "' class=\"subitem\">";
var children = ele.childNodes;
for( var i=0; i<children.length; i++ )
{
if( children.getAttribute("parent") == ele.getAttribute("id")
)
{
drawProduct( children, level+1, obj );
}
}
obj.innerHTML += "</div>";
}