我想从在线xml文件中获取数据,例如http://data.one.gov.hk/others/td/speedmap.xml。 它在我从本地调用javascript的javascript(没有servlet)时起作用。 但是,当我通过jsp和java servlet调用它时,它不起作用。 是否有另一种方法可以从联机xml文件中获取数据?java/javascript在线阅读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>
它在哪里失败?读取XML或解析它?将日志添加到浏览器控制台以更好地查看原因。 – asgs
究竟是行不通的?也许有'js_workable.js'的例外情况?你介意只发布提供的HTML页面,而不是jsp代码?顺便说一句,'LANGUAGE =“JavaScript”'已弃用。 – Bergi
我现在不确定原因。我会检查这一点。顺便说一句,谢谢你的建议! – user1976340