简体   繁体   English

以解析字符串访问XML节点

[英]Access XML nodes in parse string

I'm receiving XML from a server in a different domain, by performing an Ajax call through a PHP proxy (to work around the cross-origin "issue"). 我正在通过PHP代理执行Ajax调用来从其他域中的服务器接收XML(以解决跨域“问题”)。 I don't get the value from the XML nodes into my html tag. 我没有将XML节点中的值放入html标记中。 No errors in my browser console. 我的浏览器控制台中没有错误。 In Firebug, I can see the XML data just fine in both the Response and XML column. 在Firebug中,我可以在Response和XML列中看到XML数据。 Complete code: 完整的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<h2>Flightinfo</h2>

<button type="button" onclick="loadXMLDoc()">
Get Flight ID</button>

<p id="fid"></p>

<script>
function loadXMLDoc() {
jQuery(document).ready(function($) { 
  $.ajax({
    url: "http://d2f.no/flightinfo/proxy.php",
    type: "POST",
    data: {
        address: "http://flydata.avinor.no/XmlFeed.asp?TimeFrom=1&TimeTo=7&airport=OSL&direction=D&lastUpdate=2016-03-10T15:03:00Z"
    },

    success: function myFunction(xml) {

  var x, i, xmlDoc, txt, parser;
  parser = new DOMParser(); // new Parser
  xmlDoc = parser.parseFromString(xmlDoc,"text/xml"); // Parse string
  txt = "";
  x = xmlDoc.getElementsByTagName("flight_id");
  for (i = 0; i< x.length; i++) {
  txt += x[i].childNodes[0].nodeValue + "<br>";
  }

   document.getElementById("fid").innerHTML = txt;

}

  }); 
});  
}
</script>
</body>
</html>

Parts of the XML data received: 接收到的部分XML数据:

    <airport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://flydata.avinor.no/XmlFeed.xsd" name="OSL">
<flights lastUpdate="2016-04-07T00:37:16Z">
<flight uniqueID="5760860">
<airline>OSL</airline>
<flight_id>OSL424</flight_id>
<dom_int>D</dom_int>
<schedule_time>2016-03-10T10:15:00Z</schedule_time>
<arr_dep>D</arr_dep>
<airport>BDU</airport>
<gate>PAD</gate>
</flight>
<flight uniqueID="5758058">
<airline>AC</airline>
<flight_id>AC6377</flight_id>
<dom_int>S</dom_int>
<schedule_time>2016-03-11T05:30:00Z</schedule_time>
<arr_dep>D</arr_dep>
<airport>BRU</airport>
<check_in>10</check_in>
</flight>

Link to XML feed 链接到XML feed

What am I missing here? 我在这里想念什么?

Here is a streamlined approach, more the jQuery way: 这是一种简化的方法,更多是jQuery的方法:

<button type="button" id="load_btn">Get Flight IDs</button>

<p id="fid"></p>

<script>
$( document ).ready(function() {
    $('#load_btn').click(function() {
        loadXMLDoc();
    });
});  

function loadXMLDoc() {

    $.ajax({
        url: "http://d2f.no/flightinfo/proxy.php",
        type: "POST",
        dataType: "xml",
        data: {
            address: "http://flydata.avinor.no/XmlFeed.asp?TimeFrom=1&TimeTo=7&airport=OSL&direction=D&lastUpdate=2016-03-10T15:03:00Z"
        },

    })

    .done(function (xml) {

        var ids = $(xml).find('flight_id');

        var txt = "";
        $.each(ids, function(index) {
            txt += ids[index].textContent + "<br>";
        }); 
        $('#fid').html(txt);
    });
}
</script>

One problem in your code was the line: 您的代码中的一个问题是该行:

  xmlDoc = parser.parseFromString(xmlDoc,"text/xml"); // Parse string

which uses xmlDoc twice. 两次使用xmlDoc。 I believe you meant it to be: 我相信您的意思是:

  xmlDoc = parser.parseFromString(xml,"text/xml"); // Parse string

Also, as of jQuery 1.8, success has been deprecated in favor of .done. 另外,从jQuery 1.8开始, 不赞成使用 .done。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM