【发布时间】:2011-10-05 06:08:30
【问题描述】:
我正处于一个巨大的两难境地,无法终生解决我做错了什么:S 我已经为其他项目编写了其他几个代码,它们做同样的事情 - 输出一个显示 XML 文件中数据的表,但对于这个项目,它只是不起作用!
这是我的代码:
<html>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
xml=loadXMLDoc("AH_vic.xml");
var aname="/AirportHeliport/timeSlice/AirportHeliportTimeSlice/name";
var acoord="/AirportHeliport/timeSlice/AirportHeliportTimeSlice/ARP/ElevatedPoint/gml:coordinates";
if (typeof xml.evaluate !== 'undefined')
{
var result = xml.evaluate(
aname,
xml,
function (prefix) {
if (prefix === 'gml') {
return 'http://www.opengis.net/gml/3.2';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null
);
var result2 = xml.evaluate(
acoord,
xml,
function (prefix) {
if (prefix === 'gml') {
return 'http://www.opengis.net/gml/3.2';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null
);
// now use the code here you already have in your sample for evaluate
var nodes=xml.evaluate(
aname,
xml,
function (prefix) {
if (prefix === 'gml') {
return 'http://www.opengis.net/gml/3.2';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null);
var nodes2=xml.evaluate(
acoord,
xml,
function (prefix) {
if (prefix === 'gml') {
return 'http://www.opengis.net/gml/3.2';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null);
var aname2=nodes.iterateNext();
var acoord2=nodes2.iterateNext();
//document.write(aname2.childNodes[0].nodeValue());
document.write("<table border=2><tr><td><b>Name</b></td><td><b>Coordinates</b></td></tr>");
while (aname2)
{
document.write("<tr><td>");
document.write(aname2.childNodes[0].nodeValue);
document.write("<br /><td>");
document.write(acoord2.childNodes[0].nodeValue);
document.write("<br /><td>");
aname2=nodes.iterateNext();
acoord2=nodes2.iterateNext();
}
document.write("</td></tr></table>");
}
else if (typeof xml.selectNodes !== 'undefined' && typeof xml.setProperty != 'undefined')
{
//xml.setProperty('SelectionLanguage', 'XPath');
//xml.setProperty('SelectionNamespaces', 'xmlns:gml="http://www.opengis.net/gml/3.2"');
var nodes = xml.selectNodes(aname);
var nodes2 = xml.selectNodes(acoord);
// now use the code you already have for selectNodes
document.write("<table border=2><tr><td><b>Name</b></td><td><b>Coordinates</b></td></tr>");
for (i=0;i<nodes.length;i++)
{
document.write("<tr><td>");
document.write(nodes[i].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(nodes2[i].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
}
</script>
</body>
</html>
Internet Explorer 部分(最后的 for 循环)运行良好。我知道 IE 代码不依赖于命名空间(URL),但是命名空间和命名空间 URL 在我从中提取它们的 XML 文件中是完美的。 Path 是完美的,因为它可以在 IE 中运行。 在 Firefox 中运行它并观察开发者控制台,如果我们尝试打印出 aname2,那么 document.write(aname2.childNodes[0].nodeValue);它报告 aname2 = null...
任何帮助将不胜感激!我来自澳大利亚,会很高兴地喊你是饮料或 ps3 什么的 =)
【问题讨论】:
标签: javascript html xml parsing firefox