javascript - getElementsByTag 不起作用

标签 javascript xml xml-parsing

您好,我桌面上的文件夹中有一个 XML 文档,我正在尝试读出标签名称为“cuisine”的所有元素。

这是我的代码:

if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
}else{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

xmlhttp.open("GET","data.xml", false);
xmlhttp.send()
xmlData = xmlhttp.responseXML;

cuisineList = xmlData.getElementsByTagName("cuisine");
document.getElementById("test").innerHTML = cuisineList.length;

当我打印出 cuisineList 的长度时,它显示为零。

这是我的 XML 文档:

<?xml version="1.0" encoding"ISO-8859-1"?>
<food>
    <cuisine type="Chinese">
    </cuisine>
    <cuisine type="Indian">

    </cuisine>
</food>

最佳答案

我认为您的文档中有错误(编码属性中缺少“=”)。

关于javascript - getElementsByTag 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11336132/

相关文章:

javascript - JQuery +thumbs_up gem 渲染投票数?

java - 如何设置 TableColumnValue JavaFX TableView

javascript - 根据解析的 XML 纬度和经度值将 Google map 居中

powershell - 带#sign 的成员(member)

javascript - event.preventDefault 阻止社交媒体图标工作

php - 删除位于选定列表之后的列表

javascript - Javascript 新窗口 URL 中的回车/换行

xml - 在xml文件中查找并替换域名地址

php - 将特殊字符 '&' 、 '>' 和 '<' 转换为其各自的 XML 字符引用

xml - 使用 LibXML 和 XPath 查找带冒号的节点(本地命名空间)