无法在 Firefox 中使用 Javascript 输出 xPath 数据
我正面临着巨大的困境,我一生都无法弄清楚我做错了什么: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 之类的 =)
I'm suffering from a huge dilemna and cannot for the life of me, work out what I have done wrong :S
I have written several other code for other projects which do exactly the same thing - Output a table displaying data from an XML file, but for this project it just does not work!
Here is my code:
<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>
The Internet explorer part (for loop towards the end) works flawlessly. I understand that IE code is not dependant on namespace(s)(URL's), but the namespace and namespace URL is perfect in the XML file from which I am extracting them from.
Path is perfect as it works in IE.
Running it in Firefox and observing the developer console, if we try to print out aname2, so document.write(aname2.childNodes[0].nodeValue); It reports that aname2 = null...
Any help would be much appreciated! I am from Australia and will gladly shout you's a drink or a ps3 or something =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的是,我发现你的问题是因为我在 Internet Explorer 上遇到了麻烦 =)
我一直在其他浏览器中这样做:
在这种情况下,我用节点填充一个数组,但你可以做任何你想做的事情请。
Funny, I found your question because I'm having trouble on Internet Explorer =)
I've been doing it in the other browsers like this:
In this case, I'm filling an array with the nodes, but you can do whatever you please.