用Javascript解析XML不需要';行不通
非常简单的一段代码:用Javascript解析XML不需要';行不通,javascript,html,xml,Javascript,Html,Xml,非常简单的一段代码: <!DOCTYPE html> <html> <head> </head> <body> <script type="text/javascript"> function loadXMLDoc(dname) { if (window.XMLHttpRequest)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}
xmlDoc = loadXMLDoc("feedback.xml");
x=xmlDoc.getElementsByTagName("quote");
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("<br>");
}
</script>
</body>
</html>
函数loadXMLDoc(dname)
{
if(window.XMLHttpRequest)
{
xhttp=newXMLHttpRequest();
}
其他的
{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”、dname、false);
xhttp.send();
返回xhttp.responseXML;
}
xmlDoc=loadXMLDoc(“feedback.xml”);
x=xmlDoc.getElementsByTagName(“报价”);
对于(i=0;i将
添加到xml顶部是我最初的建议。但是,我已经在Ubuntu中的Firefox上进行了检查,您的代码正在运行。我尝试更改编码,可能这就是问题所在,但事实并非如此
起初我认为没有标题的XML文档不会被Javascript识别为同一个对象。但是,在我的Firefox Ubuntu上也没有区别,文档是XMLDocument,列表是HTMLCollection
所以我真的很困惑问题是什么。你检查过XML是否真的返回了吗?alert(xmlDoc)给我一个[object XMLDocument]可能是你的反馈。XML
不在正确的位置。它在同一个文件夹中,就在这个html文件的旁边。尝试添加到你的XML中。
<feedback>
<quote id="1">"This is a quote."</quote>
<quote id="2">"This is another quote."</quote>
</feedback>