...

View Full Version : Javascript to parse XML File



natie769
01-27-2009, 05:37 PM
I have this Javascript running for parsing an XML file, but it only displays the first item in the XML file....I'm sure its something simple I'm missing.


<script type="text/javascript">
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("equipment.xml");

document.write("<table cellpadding='10' width='756px'>");
document.write("<tr>");
document.write("<td>");
document.write("Manufacturer");
document.write("</td>");

document.write("<td>");
document.write("Model");
document.write("</td>");

document.write("<td>");
document.write("Year");
document.write("</td>");

document.write("<td>");
document.write("Equipment");
document.write("</td>");

document.write("<td>");
document.write("Price");
document.write("</td>");

document.write("<td>");
document.write("Options");
document.write("</td>");

document.write("<td>");
document.write("Photos");
document.write("</td>");
document.write("</tr>");


var x=xmlDoc.getElementsByTagName("Activities");
for (i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("Manufacturer")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(
x[i].getElementsByTagName("Model")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(
x[i].getElementsByTagName("Year")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(
x[i].getElementsByTagName("Equipment")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(
x[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(
x[i].getElementsByTagName("Options")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(
x[i].getElementsByTagName("Photos")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}

</script>





This is my XML schema:

<Activities>
<Activity></Activity>
<Activity></Activity>
<Activity></Activity>
</Activities>

angst
01-27-2009, 05:49 PM
I've never parsed xml with javascript before,
but shouldn't these values be dynamic?

x[i].getElementsByTagName("Manufacturer")[i].childNodes[i].nodeValue);

I replaced the 0's with i's

natie769
01-27-2009, 07:20 PM
That code is from the W3C site, so it should be proper..I was just wondering why its only posting the first item.

angst
01-27-2009, 07:24 PM
can you send me a copy to the page where u found this?

also please note for future posts you need to WRAP your code


code........

natie769
01-27-2009, 09:43 PM
http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest
http://www.w3schools.com/xml/xml_parser.asp

starie78
03-17-2009, 02:56 PM
I think angst is correct in saying that the values need to be dynamic. I had a vaguely similar situation a while back.

Have a look at http://userscripts.org/scripts/review/38466

I had to adapt it slightly to fit my code, but it was a big help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum