4

我想从远程网站http://services.faa.gov/airport/status/IAD?format=xml解析 xml 数据...但我无法解析 xml 数据,我只得到错误. 但我能够解析来自同一个远程网站http://services.faa.gov/airport/status/IAD?format=json的 JSON 数据。我用来解析 xml 数据的代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Aviation</title>
<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    var result;
    function xmlparser() {
        $.ajax({
            type: "GET",
            url: "http://services.faa.gov/airport/status/IAD?format=xml",
            dataType: "xml",
            success: function (xml) { 
                result = xml.city;
                document.myform.result1.value = result;
            },
            error: function (xml) {
                alert(xml.status + ' ' + xml.statusText);
            }
        });             
    }        
</script>    
</head>
<body>
<p id="details"></p>
<form name="myform">
    <input type="button" name="clickme" value="Click here to show the city name" onclick=xmlparser() />
    <input type="text" name="result1" readonly="true"/>        
</form>
</body>
</html>

自从我打印了错误消息以来,我只在警报框中收到错误消息“o Error”。请任何人帮忙解析来自远程网站的 xml 数据。 注意:我也有“城市”而不是“城市”,但它不起作用......在此先感谢......

4

2 回答 2

3

解决方案很简单(在Pekka的评论中提到)

1.在您的服务器上添加一个文件IAD_proxy.php

2.将以下代码放入其中

header("Content-type: text/xml; charset=utf-8");
echo file_get_contents('http://services.faa.gov/airport/status/IAD?format=xml');

3.将url您的 Ajax 请求中的 更改为IAD_proxy.php.

如果您使用任何其他服务器端语言,请尝试实现相同的想法。

编辑:请阅读Parsing XML With jQuery,这是我尝试过的,它正在工作。

Javascript:

$.ajax({
        type: "GET",
        url: "IAD_proxy.php",
        dataType: "xml",
        success: function (xml) { 
            alert($(xml).find('City').text());
        },
        error: function (xml) {
            alert(xml.status + ' ' + xml.statusText);
        }
    });

在这里我试过了document.write($(xml).find('City').text());

在此处输入图像描述

于 2012-08-02T13:56:15.037 回答
3

I don't believe that will work since the service is still returning xml. jsonp is expecting a n object literal as an argument to pass to the callback. I believe if you run this locally you'll realize there's no data being consumable in your success. Try this

$.ajax({
    type: "GET",
    url: "http://services.faa.gov/airport/status/IAD?format=json",
    dataType: "jsonp",
    success: function (data) {
        document.myform.result1.value = data.city;
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(errorThrown);
    }
});

Here is the example for creating a proxy with asp.net mvc 3. I just created an action that returns a ContentResult which maps to a string but I define the content type as text/xml. This simply just makes a webrequest to the service and reads the stream in to a string to send back in the response.

[HttpGet]
public ContentResult XmlExample()
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://services.faa.gov/airport/status/IAD?format=xml");
    string xml = null;
    using (WebResponse response = request.GetResponse())
    {
        using (var xmlStream = new StreamReader(response.GetResponseStream()))
        {
            xml = xmlStream.ReadToEnd();
        } 
    }

    return Content(xml, "text/xml");
}

Your xmlParser function will look like this:

<script type="text/javascript">
    var result;
    function xmlparser() {
        $.ajax({
            type: "GET",
            url: "XmlExample",
            dataType: "xml",
            success: function (xml) {
                result = $(xml).find("City").text();
                document.myform.result1.value = result;
            },
            error: function (xml) {
                alert(xml.status + ' ' + xml.statusText);
            }
        });             
    }        
</script> 

jQuery ajax's converts the data by using $.parseXML internally which removes the requirement for us to even call this in the success block. At that point you have a jQuery object that you can use it's default DOM functions to find the City Node.

Make sure to replace the XmlExample with the url that it maps to based on your controller.

于 2012-08-02T13:37:50.077 回答