...

View Full Version : Attempting to use JS to parse info from XML into HTML



harbinger
05-14-2009, 08:55 PM
<!--
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("test.xml");

document.write("<table width='74' border='0'>");

var x=xmlDoc.getElementsByTagName("ENTRY");
for (i=0; i<x.length; i++) {
document.write("<tr>");
document.write("<td colspan='3'>");
document.write("<a href='");
document.write(x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue);
document.write("'>");
document.write("<img src='");
document.write(x[i].getElementsByTagName("IMG")[0].childNodes[0].nodeValue);
document.write("' alt='");
docuemnt.write(x[i].getElementsByTagName("MFG")[0].childNodes[0].nodeValue);
docuemnt.write("' />");
document.write("</a>");
document.write("</td>");
document.write("</tr>");

/*document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("MFG")[0].childNodes[0].nodeValue);
document.write("</td>");
//document.write("</tr>");
//document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue);
document.write("</td>");
//document.write("</tr>");
//document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("IMG")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");*/
}
document.write("</table>");
}
//-->

There may be an easier way to do this but I am unaware of it.

Also you will notice it is actually different scripts.

One is an attempt at the final product, to get a image to load with a link and alt description.

The other works fine and loads the 3 sections of infomation into a table so I can see that it is querying the XML file correctly. I have attached the .js file and the .xml file.

I appreciate any assistance that can be rendered

Old Pedant
05-14-2009, 09:16 PM
So what's the question??

harbinger
05-15-2009, 08:52 PM
My question is:

The script works correctly when it is only doing with text,



for (i=0; i<x.length; i++) {
document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("MFG")[0].childNodes[0].nodeValue);
document.write("</td>");
//document.write("</tr>");
//document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue);
document.write("</td>");
//document.write("</tr>");
//document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("IMG")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}


but when I attempt to implement with images,



for (i=0; i<x.length; i++) {
document.write("<tr>");
document.write("<td>");
document.write("<a href='");
document.write(x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue);
document.write("'>");
document.write("<img src='");
document.write(x[i].getElementsByTagName("IMG")[0].childNodes[0].nodeValue);
document.write("' alt='");
docuemnt.write(x[i].getElementsByTagName("MFG")[0].childNodes[0].nodeValue);
docuemnt.write("' />");
document.write("</a>");
document.write("</td>");
document.write("</tr>");
}


All i get is the first image. Are there any sugestions on how I may have made an error that might be causing this? Or perhaps there is a better way to do the same thing?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum