计算节点数不起作用
counting the number of nodes is not working
我有一个 xml 文件:
<?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>
我正在尝试计算美食节点的数量,这是我拥有的代码,我知道它基本上是正确的,但是当我尝试打印出节点列表的长度时,它说它是 0
//starts talking to the 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 the first comboBox
var sel = document.getElementById('CuisineList');
cuisineList = xmlData.getElementsByTagName('cuisine');
document.getElementById("test").innerHTML = cuisineList.length;
两个可能的答案:
A) XML 有错误,您需要</phone>
</city>
。所以你需要解决这个问题。:-)(但我猜这只是问题,而不是你的真实数据,因为如果它在你的真实数据中,你就不会得到0
,我不认为;你会得到一个错误。
B) 检查您的响应标头,可能是您的服务器没有返回具有正确 MIME 类型的 XML。如果没有:
-
最佳答案是更正服务器配置,以便它正确提供 XML 文件。
-
如果出于某种原因无法执行此操作,则可以使用
overrideMimeType
强制XMLHttpRequest
将响应视为 XML:xmlhttp.open("GET","data.xml", false); xmlhttp.overrideMimeType("text/xml"); // <=== The new bit xmlhttp.send(); xmlData = xmlhttp.responseXML;
如果您修复了这些问题,它可以工作:实时工作副本 | 来源
相关文章:
- 节点.js快速删除 req.body 不起作用
- ES2015“导入”在带有--harmony_modules选项的节点v6.0.0中不起作用
- 如果端口范围太宽,节点中的端口扫描不起作用
- “require”关键字在节点红色函数节点中不起作用
- 节点JS require不起作用
- 节点 Webkit 文本到语音 API 不起作用
- 默认条件在节点中不起作用
- 计算节点数不起作用
- 节点邮件不起作用,怎么了
- 节点要求()与ES6导入:为什么这个例子不起作用
- 节点.js正则表达式不起作用
- 节点.js:setInterval 函数不起作用
- 删除类后的添加类在选择同一节点时在剑道树视图中不起作用
- 节点.JS - 简单的 Socket.IO 示例不起作用.获取调试 - 提供静态内容/socket.io.js(Mac
- 使用节点发布回复.js不起作用
- 如果条件在节点 js 中不起作用
- 节点 Http 代理 - 基本反向代理不起作用 (404s)
- 通过猫鼬和节点_id进行简单查询.js不起作用
- DOM 附加文本节点的子节点不起作用
- 从角度到节点的 POST 数据不起作用