无法在 Firefox 中使用 Javascript 输出 xPath 数据

发布于 2024-12-08 09:29:15 字数 3566 浏览 0 评论 0原文

我正面临着巨大的困境,我一生都无法弄清楚我做错了什么: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-12-15 09:29:15

有趣的是,我发现你的问题是因为我在 Internet Explorer 上遇到了麻烦 =)

我一直在其他浏览器中这样做:

namespace.prototype.xpath = function(xml, path) {
    var array = [];

    var appendFromNodes = function(object, nodes) {
        for (var index in nodes) {
            var childNode = nodes[index];

            if (childNode.nodeName) {
                object[childNode.nodeName] = childNode.textContent;
            }
        }
    };

    if (xml.evaluate) { // checks if browser supports the W3C way of doing it (no IE)
        var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();

        while (result) {
            var tmp = {};
            appendFromNodes(tmp, result.childNodes);

            for (var index in result.attributes) {
                var attribute = result.attributes[index];

                if (attribute.nodeName) {
                    tmp[attribute.nodeName] = attribute.nodeValue;
                }
            }

            array.push(tmp);
            result = nodes.iterateNext();
        }
    }

    return array;
};

在这种情况下,我用节点填充一个数组,但你可以做任何你想做的事情请。

Funny, I found your question because I'm having trouble on Internet Explorer =)

I've been doing it in the other browsers like this:

namespace.prototype.xpath = function(xml, path) {
    var array = [];

    var appendFromNodes = function(object, nodes) {
        for (var index in nodes) {
            var childNode = nodes[index];

            if (childNode.nodeName) {
                object[childNode.nodeName] = childNode.textContent;
            }
        }
    };

    if (xml.evaluate) { // checks if browser supports the W3C way of doing it (no IE)
        var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();

        while (result) {
            var tmp = {};
            appendFromNodes(tmp, result.childNodes);

            for (var index in result.attributes) {
                var attribute = result.attributes[index];

                if (attribute.nodeName) {
                    tmp[attribute.nodeName] = attribute.nodeValue;
                }
            }

            array.push(tmp);
            result = nodes.iterateNext();
        }
    }

    return array;
};

In this case, I'm filling an array with the nodes, but you can do whatever you please.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文