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:

  1. the best answer correct server configuration serves xml files correctly.

  2. if whatever reason can't that, can use overridemimetype force xmlhttprequest 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

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -