非常に簡単なコード:
<!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>
次のような XML ファイル「feedback.xml」をロードしています。
<feedback>
<quote id="1">"This is a quote."</quote>
<quote id="2">"This is another quote."</quote>
</feedback>
http://www.w3schools.com/dom/tryit.asp?filename=try_dom_list_loopからコードを取得しましたが、実際には何も起こっていません。中身を確認しようとするとalert(x)
、「[object HTMLCollection]」が表示されます。ただし、確認したい場合はalert(x[0])
「未定義」になります。これが機能しない理由はありますか?XML ファイルはこの html ファイルと同じフォルダーにあり、Firebug はエラーをスローしません。