嗨,我在桌面上的文件夹中有一个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>

最佳答案

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

10-05 21:24