javascript - counting the number of nodes is not working -
i have xml file :
<?xml version="1.0" encoding="iso-8859-1"?> <food> <cuisine type="chinese"> <restaurant name = "panda express"> <location id= "0"></location> <phone id = "1"></phone> <city id="2"></phone> </restaurant> <restaurant name = "mr. chau's"> </restaurant> </cuisine> <cuisine type="indian"> <restaurant name = "shan"> </restaurant> </cuisine> </food>
and trying count number of cuisine nodes code have, know right when try print out length of nodelist says it's 0
//starts talking xml document if(window.xmlhttprequest){ xmlhttp = new xmlhttprequest(); }else{ xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.open("get","data.xml", false); xmlhttp.send(); xmldata = xmlhttp.responsexml; //fills first combobox var sel = document.getelementbyid('cuisinelist'); cuisinelist = xmldata.getelementsbytagname('cuisine'); document.getelementbyid("test").innerhtml = cuisinelist.length;
two possible answers:
a) xml has error, have </phone>
need </city>
. you'll need fix that. :-) (but i'm guessing that's in question, not in real data, because if in real data wouldn't getting 0
, wouldn't think; you'd getting error.)
b) check response headers, may server isn't returning xml correct mime type. if not:
the best answer correct server configuration serves xml files correctly.
-
if whatever reason can't that, can use
overridemimetype
forcexmlhttprequest
treat response xml:xmlhttp.open("get","data.xml", false); xmlhttp.overridemimetype("text/xml"); // <=== new bit xmlhttp.send(); xmldata = xmlhttp.responsexml;
if fix those, works: live working copy | source
Comments
Post a Comment