为什么以下Java脚本无法加载XML?
我已经在课堂上向我们讲了一个例子,其中一个javascript用于从XML中检索数据,但它不起作用。请帮助
我还在下面添加了XML文件。
<html>
<head>
<title>Customer Info</title>
<script language="javascript">
var xmlDoc = 0;
var xmlObj = 0;
function loadCustomers(){
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.onreadystatechange = displayCustomers;
xmlDoc.load("customers.xml");
}
function displayCustomers(){
if(xmlDoc.readyState == 4){
xmlObj = xmlDoc.documentElement;
var len = xmlObj.childNodes.length;
for(i = 0; i < len; i++){
var nodeElement = xmlObj.childNodes[i];
document.write(nodeElement.attributes[0].value);
for(j = 0; j < nodeElement.childNodes.length; j++){
document.write(" " + nodeElement.childNodes[j].firstChild.nodeValue);
}
document.write("<br/>");
}
}
}
</script>
</head>
<body>
<form>
<input type="button" value="Load XML" onClick="loadCustomers()">
</form>
</body>
</html>
XML(customers.xml)
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer custid="CU101">
<pwd>PW101</pwd>
<email>jack@rediff.com</email>
</customer>
<customer custid="CU102">
<pwd>PW102</pwd>
<email>jill@rediff.com</email>
</customer>
<customer custid="CU103">
<pwd>PW103</pwd>
<email>john@rediff.com</email>
</customer>
<customer custid="CU104">
<pwd>PW104</pwd>
<email>jeff@rediff.com</email>
</customer>
</customers>
编辑1
我正在尝试W3Schools.com的一些例子,但他们没有工作。我正在使用chrome和IE。这是我尝试过的一些例子。
例1
例题
没有找到相关结果
已邀请:
1 个回复
坛沤疲撑拆