从远程网站解析xml数据 [英] Parsing xml data from a remote website

查看:93
本文介绍了从远程网站解析xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pre code><!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< title>航空< / title>
< script type =text / javascriptsrc =Scripts / jquery-1.7.2.min.js>< / script>
< script type =text / javascript>
var result;
函数xmlparser(){
$ .ajax({
类型: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 =buttonname =clickmevalue =点击此处显示城市名称onclick = xmlparser()/>
< input type =textname =result1readonly =true/>
< / form>
< / body>
< / html>

因为我打印了错误,所以我只在警告框中得到错误'o Error'信息。任何人请帮助解析来自远程网站的XML数据。
注意:我也有'City'而不是'city',但它不起作用...
在此先感谢...

解决方案

我不相信这将工作,因为该服务仍然返回XML。 jsonp期望将一个n对象字面量作为参数传递给回调函数。我相信如果你在本地运行,你会发现在你的成功中没有任何数据可以消耗。试试这个

  $。ajax({
类型: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);
}
});

以下是使用asp.net mvc 3创建代理的示例。返回映射到字符串的ContentResult,但我将内容类型定义为text / xml。这只是对服务进行web请求,并将流读入到字符串中,以便在响应中发回。

  [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();
}
}

返回Content(xml,text / xml);

您的xmlParser函数如下所示:

 < script type =text / javascript> 
var result;
函数xmlparser(){
$ .ajax({
类型:GET,
url:XmlExample,
dataType:xml,
成功:函数(xml){
结果= $(xml).find(City).text();
document.myform.result1.value = result;
} ,
error:function(xml){
alert(xml.status +''+ xml.statusText);
}
});
}
< / script>

jQuery ajax通过在内部使用$ .parseXML来转换数据,这使得我们甚至不需要调用这个在成功块。此时,您有一个jQuery对象,您可以使用它的默认DOM功能来查找城市节点。



请确保使用它映射到的URL根据您的控制器来替换 XmlExample


I would like to parse the xml data from a remote website http://services.faa.gov/airport/status/IAD?format=xml...But I was not able to parse the xml data and I am only getting error. But I was able to parse the JSON data from the same remote website http://services.faa.gov/airport/status/IAD?format=json. The code I have used to parse the xml data is:

<!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>

I was only getting the error as 'o Error' in the alert box since I have printed the error message. Anybody please helpout to parse the xml data from the remote website. Note: I have also 'City' instead of 'city' but its not working... Thanks in advance...

解决方案

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.

这篇关于从远程网站解析xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆