Parsing xml data from a remote website

I would like to parse the XML data from the remote site http://services.faa.gov/airport/status/IAD?format=xml ... But I was not able to parse the xml data and I only get the error. But I was able to analyze JSON data from the same remote site http://services.faa.gov/airport/status/IAD?format=json . The code I used to parse the xml data:

<!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 an error like "o Error" in the warning field since I printed an error message. Someone please help parsing the xml data from the remote website. Note I also have a "City", not a "city", but it doesn’t work ... Thanks in advance ...

+4
source share
2 answers

I do not believe this will work, as the service still returns xml. jsonp expects an n-object callback to be passed as an argument. I believe that if you run this locally, you will realize that there is no data in your success. try it

 $.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 an example of creating a proxy with asp.net mvc 3. I just created an action that returns a ContentResult that maps to a string, but I define the content type as text / xml. It just makes a web request for the service and reads the stream into a string to send back in 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 converts the data using $ .parseXML inside, which eliminates the requirement for us to even call this in a success block. At this point, you have a jQuery object that you can use by default for DOM functions to find the City Node.

Be sure to replace XmlExample with the URL that it maps based on your controller.

+3
source

The solution is quite simple (see Pekka's comment)

1. On your server, add the file IAD_proxy.php

2. Paste the following code into 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 .

If you use any other server language, try to implement the same idea.

Edit: Please read XML parsing with jQuery , here is what I tried and it works.

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

+3
source

All Articles