Skip to content Skip to sidebar Skip to footer

Parsing Xml Data From A Remote Website

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 erro

Solution 1:

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:

<scripttype="text/javascript">var result;
    functionxmlparser() {
        $.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.

Solution 2:

The solution is quite simple (mentioned in Pekka's comment)

1.On your server add a file IAD_proxy.php

2.Put the following code inside it

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.

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.

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

enter image description here

Post a Comment for "Parsing Xml Data From A Remote Website"