Click here to Skip to main content
14,487,386 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to fetch and print raw_text from the xml API using Jquery.


I have successfully run the ajax call to trigger the API .
<pre><response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XML-Schema-instance" version="1.2" xsi:noNamespaceSchemaLocation="http://aviationweather.gov/adds/schema/metar1_2.xsd">
<request_index>84211629</request_index>
<data_source name="metars"/>
<request type="retrieve"/>
<errors/>
<warnings/>
<time_taken_ms>5</time_taken_ms>
<data num_results="6">
<METAR>
<raw_text>
KDEN 141753Z 36022G34KT 10SM FEW080 SCT130 BKN180 M02/M08 A3011 RMK AO2 PK WND 35036/1703 SLP217 60000 T10221083 11022 21039 51023
</raw_text>
<station_id>KDEN</station_id>
<observation_time>2019-03-14T17:53:00Z</observation_time>
<latitude>39.85</latitude>
<longitude>-104.65</longitude>
<temp_c>-2.2</temp_c>
<dewpoint_c>-8.3</dewpoint_c>
<wind_dir_degrees>360</wind_dir_degrees>
<wind_speed_kt>22</wind_speed_kt>
<wind_gust_kt>34</wind_gust_kt>
<visibility_statute_mi>10.0</visibility_statute_mi>
<altim_in_hg>30.109253</altim_in_hg>
<sea_level_pressure_mb>1021.7</sea_level_pressure_mb>
<quality_control_flags>
<auto_station>TRUE</auto_station>
</quality_control_flags>
<sky_condition sky_cover="FEW" cloud_base_ft_agl="8000"/>
<sky_condition sky_cover="SCT" cloud_base_ft_agl="13000"/>
<sky_condition sky_cover="BKN" cloud_base_ft_agl="18000"/>
<flight_category>VFR</flight_category>
<three_hr_pressure_tendency_mb>2.3</three_hr_pressure_tendency_mb>
<maxT_c>-2.2</maxT_c>
<minT_c>-3.9</minT_c>
<pcp6hr_in>0.005</pcp6hr_in>
<metar_type>METAR</metar_type>
<elevation_m>1640.0</elevation_m>
</METAR>


What I have tried:

ow i am trying to get a value of raw_text. 

<pre lang="Javascript">$.ajax({
			type: "GET",
			url: "https://www.aviationweather.gov/adds/dataserver_current/httpparam",
			dataType: "xml",
			data: { 
			dataSource:dataSource,
			requestType:requestType,
			format:format,
			stationString:stationString,
			hoursBeforeNow:hoursBeforeNow
			
  },
			success: function(data) {
			//remainder of the code
			console.log(data);
                       console.log(data.METAR.raw_text) // not able to get this value

				}
			});
	  
     });
Posted
Updated 14-Mar-19 18:25pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Assuming the XML posted here is correct, you can use the .find() selector to get the element value like below. Again, I have to assume that the data return is XML type object.

console.log($(data).find("raw_text").text());


if not, here an example on how to parse a string into XML object
Cp_xml find node - JSFiddle[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100