Analyze xml data from a remote Web site

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.