I've got an XML list of radio stations that I want to display, but I think it involves some nested FOR loops that I can't quite figure out.
Here's a sample of my XML:
After browsing through w3schools.com's XML DOM section, I think I found a script that is close to what I need (http://www.w3schools.com/dom/tryit.a...y_dom_nodetype), and here's my version that isn't quite working:
What I'm trying to accomplish is to have it display the state name once and then each city's name & frequency below its state's name, like this:
California
City 1, 88.1 FM
City 2, 88.7 FM
City 3, 89.3 FM
City 4, 100.3 FM
Oregon
City 1, 91.1 FM
City 2, 90.1 FM
Washington
City 1, 95.7 FM
City 2, 93.1 FM
City 3, 90.7 FM
Any help you have would be greatly appreciated. Thanks in advance!
Here's a sample of my XML:
Code:
<stations> <state id="California"> <station city="City 1" freq="88.1 FM" /> <station city="City 2" freq="88.7 FM" /> <station city="City 3" freq="89.3 FM" /> <station city="City 4" freq="100.3 FM" /> </state> <state id="Oregon"> <station city="City 1" freq="91.1 FM" /> <station city="City 2" freq="90.1 FM" /> </state> <state id="Washington"> <station city="City 1" freq="95.7 FM" /> <station city="City 2" freq="93.1 FM" /> <station city="City 3" freq="90.7 FM" /> </state> </stations>
Code:
<script type="text/javascript">
xmlDoc = loadXMLDoc("xml/stations.xml");
x = xmlDoc.getElementsByTagName('state');
for (i=0; i<x.length; i++) {
y = x.childNodes;
document.write("<b>" + x[i].getAttribute('id') + "</b><br />");
for (z=0; z<y[i].length; z++) {
document.write(y[i].childNodes[z].getAttribute('city'));
document.write(", ");
document.write(y[i].childNodes[z].getAttribute('freq'));
document.write("<br />");
}
document.write("<br /><br />");
}
</script>
California
City 1, 88.1 FM
City 2, 88.7 FM
City 3, 89.3 FM
City 4, 100.3 FM
Oregon
City 1, 91.1 FM
City 2, 90.1 FM
Washington
City 1, 95.7 FM
City 2, 93.1 FM
City 3, 90.7 FM
Any help you have would be greatly appreciated. Thanks in advance!
Comment