日期:2014-05-16 浏览次数:20621 次
function listAllStates() { var xmlDoc=xmlHttp.responseXML; var allStates=xmlDoc.getElementsByTagName("state"); outputList("All States in Document",allStates); } function outputList(title,states) { var out=title; var currentState=null; for(var i=0;i<states.length;i++) { currentState=states[i]; out=out+"\n-"+currentState.childNodes[0].nodeValue; }alert(out); }
function listAllStates() { var xmlDoc; var xmlString = '<states><state>wei</state></states>'; // Mozilla and Netscape browsers if (document.implementation.createDocument) { var parser = new DOMParser() xmlDoc = parser.parseFromString(xmlString, "application/xml") // MSIE } else if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.loadXML(xmlString) } var x = xmlDoc.getElementsByTagName('state'); alert (xmlDoc); alert (x.length); } listAllStates();
------解决方案--------------------
你保证你的远程XML读取正确么?
还有是否可以考虑使用XPATH来解决
也就是XDOC.selectNodes("//state").length;
------解决方案--------------------
function handleStateChange() { if(xmlHttp.readyState==4){ //如果你直接拖进浏览器的话正确执行时返回的状态为0,要想返回200状态需要服务器支持 //所以要加上xmlHttp.status==0的判断 if(xmlHttp.status==200||xmlHttp.status==0){ if(requestType=="north"){ listNothStates(); } else if(requestType=="all"){ listAllStates(); } } } }