我想从在线 xml 文件中获取数据,例如http://data.one.gov.hk/others/td/speedmap.xml。当我在本地(没有 servlet)从 HTML 调用 javascript 时,它可以工作。但是,当我通过 jsp 和 java servlet 调用它时,它不起作用。是否有另一种方法可以让我从在线 xml 文件中获取数据?
contentXML.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<p>CONTENT_XML</p>
<%@ include file="source_file/js_workable.js" %>
<%@ include file="source_file/data_retrieve.js" %>
</body>
</html>
data_retrieve.js
<script LANGUAGE="JavaScript">
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
document.write(xmlhttp.status);
document.write(xmlhttp.readyState);
document.write(xmlhttp.statusText);
xmlhttp.open("GET","http://data.one.gov.hk/others/td/speedmap.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("jtis_speedmap");
for (i=0;i<x.length;i++){
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("LINK_ID")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("ROAD_TYPE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
.html 文件:
<html>
<body>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://data.one.gov.hk/others/td/speedmap.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("jtis_speedmap");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("LINK_ID")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("ROAD_TYPE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
</body>
</html>