我在类里面教给我们一个例子,其中使用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。这是我尝试过的一些例子。 Example1 Example2
最佳答案
看起来该示例仅适用于 Internet Explorer Web 浏览器。您可能想看看这个 Getting Started教程。
关于javascript - 为什么以下 Java 脚本无法加载 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4532957/