【问题标题】:Can't output xPath data using Javascript in Firefox无法在 Firefox 中使用 Javascript 输出 xPath 数据
【发布时间】: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


    【解决方案1】:

    有趣的是,我找到了你的问题,因为我在使用 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;
    };
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多