XML nested nodes in javascript

Pauleena

New Member
In this xml demo, I am able to loop through the root node and the top level nodes, I want to get the definition for each node. The different ways that I tried to get that with childnodes only get the top nodes.Here is the HTML\[code\]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"><head><script type="text/javascript">if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("GET","communities.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML; y=xmlDoc.getElementsByTagName("COMMUNITIES")[0].childNodes;n=y.length;document.write("n "+n+"<br/>");for (i=0;i<n;i++){ document.write(y.nodeName); att=y.item(i).attributes.getNamedItem("ID"); document.write(att.value + "<br />"); x=y.item(i).childNodes; document.write(x.length); document.write("<br />");}</script></head><body><div id='show'></div></body></html>\[/code\]Here is the xml.\[code\]<?xml version="1.0" encoding="ISO-8859-1" ?> <COMMUNITIES><COMMUNITY ID="c001"> <NAME>Town Services</NAME> <TOP>50</TOP> <LEFT>50</LEFT> <WIDTH>200</WIDTH> <HEIGHT>300</HEIGHT> <URLS> <URL ID="U001"> <NAME>Google.com</NAME> <URL>http://www.google.com</URL> </URL> <URL ID="U002"> <NAME>Bing.com</NAME> <URL>http://www.bing.com</URL> </URL> <URL ID="U003"> <NAME>Yahoo.com</NAME> <URL>http://www.yahoo.com</URL> </URL> <URL ID="U004"> <NAME>Aol.com</NAME> <URL>http://www.aol.com</URL> </URL> </URLS> </COMMUNITY><COMMUNITY ID="c002"> <NAME>Local Stores</NAME> <TOP>50</TOP> <LEFT>260</LEFT> <WIDTH>200</WIDTH> <HEIGHT>150</HEIGHT> <URLS> <URL ID="U001"> <NAME>Walgreens</NAME> <URL>http://www.walgreens.com</URL> </URL> <URL ID="U002"> <NAME>Bing.com</NAME> <URL>http://www.bing.com</URL> </URL> <URL ID="U003"> <NAME>Yahoo.com</NAME> <URL>http://www.yahoo.com</URL> </URL> </URLS> </COMMUNITY><COMMUNITY ID="c003"> <NAME>Attractions</NAME> <TOP>50</TOP> <LEFT>470</LEFT> <WIDTH>200</WIDTH> <HEIGHT>300</HEIGHT> <URLS> <URL ID="U001"> <NAME>Museum</NAME> <URL>http://www.mfa.org</URL> </URL> <URL ID="U002"> <NAME>Park</NAME> <URL>http://www.bing.com</URL> </URL> </URLS> </COMMUNITY><COMMUNITY ID="c004"> <NAME>Online Stores</NAME> <TOP>370</TOP> <LEFT>50</LEFT> <WIDTH>200</WIDTH> <HEIGHT>150</HEIGHT> <URLS> <URL ID="U001"> <NAME>Amazon.com</NAME> <URL>http://www.amazon.com</URL> </URL> <URL ID="U002"> <NAME>Target.com</NAME> <URL>http://www.target.com</URL> </URL> </URLS> </COMMUNITY><COMMUNITY ID="c005"> <NAME>Online Forums</NAME> <TOP>370</TOP> <LEFT>300</LEFT> <WIDTH>200</WIDTH> <HEIGHT>200</HEIGHT> <URLS> <URL ID="U001"> <NAME>Technet</NAME> <URL>http://www.Microsoft.com</URL> </URL> <URL ID="U002"> <NAME>MSDN</NAME> <URL>http://www.Microsoft.com</URL> </URL> </URLS> </COMMUNITY><COMMUNITY ID="c006"> <NAME>Travel</NAME> <TOP>370</TOP> <LEFT>480</LEFT> <WIDTH>200</WIDTH> <HEIGHT>200</HEIGHT> <URLS> <URL ID="U001"> <NAME>Southwest</NAME> <URL>http://www.mfa.org</URL> </URL> <URL ID="U002"> <NAME>Northwest</NAME> <URL>http://www.bing.com</URL> </URL> </URLS> </COMMUNITY></COMMUNITIES>\[/code\]My goal is to read how many communities are in that xml, and for each community retrieve ID, name, then find out how many urls, and real id, name, and url for each.
 
Back
Top