javascript 解析来自远程网站的 xml 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11778074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing xml data from a remote website
提问by prabu R
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:
我想解析来自远程网站的 xml 数据http://services.faa.gov/airport/status/IAD?format=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>
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...
因为我打印了错误消息,所以我只在警告框中收到错误为“o 错误”。任何人都请帮助解析来自远程网站的 xml 数据。 注意:我也有“城市”而不是“城市”,但它不起作用......提前致谢......
采纳答案by JustinMichaels
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
我不相信这会起作用,因为该服务仍在返回 xml。jsonp 期望将对象文字作为参数传递给回调。我相信,如果您在本地运行它,您会意识到在您的成功中没有可消耗的数据。试试这个
$.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.
这是使用 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();
}
}
return Content(xml, "text/xml");
}
Your xmlParser function will look like this:
您的 xmlParser 函数将如下所示:
<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.
jQuery ajax 通过在内部使用 $.parseXML 来转换数据,这消除了我们甚至在成功块中调用它的要求。此时,您有一个 jQuery 对象,您可以使用它的默认 DOM 函数来查找城市节点。
Make sure to replace the XmlExamplewith the url that it maps to based on your controller.
确保将XmlExample替换为它基于您的控制器映射到的 url。
回答by Adi
The solution is quite simple (mentioned in Pekka's comment)
解决方案很简单(在Pekka的评论中提到)
1.On your server add a file IAD_proxy.php
1.在你的服务器上添加一个文件 IAD_proxy.php
2.Put the following code inside it
2.在里面放入以下代码
header("Content-type: text/xml; charset=utf-8");
echo file_get_contents('http://services.faa.gov/airport/status/IAD?format=xml');
3.Change the url
in your Ajax request to IAD_proxy.php
.
3.url
将 Ajax 请求中的 更改为IAD_proxy.php
。
In case you're using any other server-side language, try to implement the same idea.
如果您使用任何其他服务器端语言,请尝试实现相同的想法。
Edit:Please read about Parsing XML With jQuery, here's what I've tried and it's working.
编辑:请阅读有关Parsing XML With jQuery 的内容,这是我尝试过的并且正在运行。
Javscript:
文字:
$.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);
}
});
Here I tried it with document.write($(xml).find('City').text());
在这里我试过了 document.write($(xml).find('City').text());