cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Reading xml from WeatherUnderground

gsee
1-Newbie

Reading xml from WeatherUnderground

Hi,

I want to replace an existing weather service (from yahoo) with a WeatherUndergroup API.

I haven't been able to get my javascript code to actually read any of the current observation data, it just returns an empty result.

var API = me.WeatherUndergroundAPI;

var features = 'forecast';

var queryLocation = '30009';

var format = 'xml';

var url = 'http://api.wunderground.com/api/' + API + '/' + features + '/'  + '/q/' + queryLocation + '.' + format

var params = {

    url: url,

    timeout : 60

};

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);

var params = {

    infoTableName : "InfoTable",

    dataShapeName : "WeatherForecast"

};

me.WeatherHumidity = xmlPage.curren t_observation.relative_humidity;

result = xmlPage.current_observation.relative_humidity;

I'm trying to read from this xml:

<response>

<version>0.1</version>

<termsofService>

http://www.wunderground.com/weather/api/d/terms.html

</termsofService>

<features>

<feature>forecast</feature>

<feature>geolookup</feature>

<feature>conditions</feature>

</features>

<location>...</location>

<current_observation>

<estimated></estimated>

<station_id>KGAALPHA22</station_id>

<observation_time>Last Updated on May 4, 12:39 PM EDT</observation_time>

<observation_time_rfc822>Wed, 04 May 2016 12:39:43 -0400</observation_time_rfc822>

<observation_epoch>1462379983</observation_epoch>

<local_time_rfc822>Wed, 04 May 2016 12:39:55 -0400</local_time_rfc822>

<local_epoch>1462379995</local_epoch>

<local_tz_short>EDT</local_tz_short>

<local_tz_long>America/New_York</local_tz_long>

<local_tz_offset>-0400</local_tz_offset>

<weather>Mostly Cloudy</weather>

<temperature_string>65.7 F (18.7 C)</temperature_string>

<temp_f>65.7</temp_f>

<temp_c>18.7</temp_c>

<relative_humidity>57%</relative_humidity>

<wind_string>From the West at 4.2 MPH Gusting to 6.8 MPH</wind_string>

<wind_dir>West</wind_dir>

<wind_degrees>270</wind_degrees>

<wind_mph>4.2</wind_mph>

<wind_gust_mph>6.8</wind_gust_mph>

<wind_kph>6.8</wind_kph>

<wind_gust_kph>10.9</wind_gust_kph>

<pressure_mb>1009</pressure_mb>

<pressure_in>29.80</pressure_in>

<pressure_trend>0</pressure_trend>

<dewpoint_string>50 F (10 C)</dewpoint_string>

<dewpoint_f>50</dewpoint_f>

<dewpoint_c>10</dewpoint_c>

<heat_index_string>NA</heat_index_string>

<heat_index_f>NA</heat_index_f>

<heat_index_c>NA</heat_index_c>

<windchill_string>NA</windchill_string>

<windchill_f>NA</windchill_f>

<windchill_c>NA</windchill_c>

<feelslike_string>65.7 F (18.7 C)</feelslike_string>

<feelslike_f>65.7</feelslike_f>

<feelslike_c>18.7</feelslike_c>

<visibility_mi>10.0</visibility_mi>

<visibility_km>16.1</visibility_km>

<solarradiation/>

<UV>8</UV>

<precip_1hr_string>0.00 in ( 0 mm)</precip_1hr_string>

<precip_1hr_in>0.00</precip_1hr_in>

<precip_1hr_metric>0</precip_1hr_metric>

<precip_today_string>in ( mm)</precip_today_string>

<precip_today_in/>

<precip_today_metric/>

<icon>mostlycloudy</icon>

<icon_url>http://icons.wxug.com/i/c/k/mostlycloudy.gif</icon_url>

<forecast_url>http://www.wunderground.com/US/GA/Alpharetta.html</forecast_url>

</current_observation>

<forecast>...</forecast>

</response>


					
				
			
			
				
			
			
				
1 ACCEPTED SOLUTION

Accepted Solutions
ttielebein
12-Amethyst
(To:gsee)

Hello, when I try to run this query within my web browser, I receive an error message. Are you sure this XML is returning properly to begin with?

URL: http://api.wunderground.com/api/<MY_KEY>/features/q/30009.xml

Error:

<error>

<type>unknownfeature</type>

</error>

That said, when I use a URL which DOES work for me in the browser in my JS, I do parse the XML correctly. Here is my script:

var params = { 

    url: "http://api.wunderground.com/api/ee5785ebc5a63ea1/conditions/q/42.7,31.xml", 

    timeout : 60 

}; 

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);

var result = xmlPage.current_observation.relative_humidity;

Let me know if this helps at all!

Tori

View solution in original post

4 REPLIES 4
ttielebein
12-Amethyst
(To:gsee)

Hello, when I try to run this query within my web browser, I receive an error message. Are you sure this XML is returning properly to begin with?

URL: http://api.wunderground.com/api/<MY_KEY>/features/q/30009.xml

Error:

<error>

<type>unknownfeature</type>

</error>

That said, when I use a URL which DOES work for me in the browser in my JS, I do parse the XML correctly. Here is my script:

var params = { 

    url: "http://api.wunderground.com/api/ee5785ebc5a63ea1/conditions/q/42.7,31.xml", 

    timeout : 60 

}; 

var xmlPage = Resources["ContentLoaderFunctions"].LoadXML(params);

var result = xmlPage.current_observation.relative_humidity;

Let me know if this helps at all!

Tori

Hi Tori,

Thanks, this helps. However, it's reading everything in as a string, even when I want to assign a numerical value to a property formatted as a number. For numbers, I've been casting them as Number(result), which is working fine. I can't get the dates to import correctly. Any recommendations there?

ttielebein
12-Amethyst
(To:gsee)

Hello again,

Great to be of assistance. Have you seen this documentation on implementing dates in ThingWorx? https://support.ptc.com/appserver/cs/view/solution.jsp?n=CS231457

It is in the ThingWorx KCS database. Have you tried this already?

Thanks!

Tori

Hi Tori,

I missed this response, but thanks for the help!

I can access the page but not the document, unfortunately. I found as long as I parsed the data to a new variable, and then assigned the value to the parameter, it works. I'm not as familiar with javascript, so I'm not sure why the limitation, but I'm glad to be getting output at least. Hopefully there's garbage collection on the new variables I had to create once this scales up to more users.

What I ended up using looks like this:

var d = new Date(Date.parse("" + xmlDoc.current_observation.observation_time_rfc822));

me.newDate = d;

Top Tags