我試圖解析一個簡單的XML文件,但我無法獲得我需要的值。我收到的當前錯誤消息說節點不是一個對象或爲空。我正在測試I.E. 6,7,FF 2,3在JavaScript中解析XML文件時出錯
這裏的XML文件:
<bookstore>
<appSettings>
<add key="myKey" value="myTargetValue"/>
</appSettings>
</bookstore>
這裏是我想要使用的腳本:
<html>
<head>
<title></title>
</head>
<script type="text/javascript">
if (window.XMLHttpRequest)
{
xhttp = new window.XMLHttpRequest()
}
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP")
}
xhttp.open("GET","test.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
alert(xmlDoc.xml)
var xpath = "/bookstore/appSettings/add[@key='myKey']";
var node = xmlDoc.selectSingleNode(xpath);
alert(node.getAttribute("value"));
</script>
<body>
</body>
</html>
請指教。謝謝。
您的XPath返回: 這是您所期望的嗎? –
dnagirl
2009-10-20 13:44:09