extract data from xml files

Topics related to the iOS and/or Apple Watch app.
Post Reply
Dieter
Posts: 10
Joined: Thu Jan 21, 2016 3:20 pm

extract data from xml files

Post by Dieter » Tue Nov 14, 2017 12:23 am

Hi Dave,

I would like to add a weather forecast panel to my remote. Source is the data I get from weather underground ("wunderground api").
The data comes in a xml file. How can I access the data in this file?

Thanks in advance

Dieter

User avatar
Dave
Site Admin
Posts: 83
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: extract data from xml files

Post by Dave » Wed Nov 15, 2017 4:05 am

Hi Dieter. If you are retrieving the XML file from a remote location, I would suggest using AJAX (XML HTTPRequest) to load the XML, and use the XML DOM Parser to process it. You can get lots of info on both of those online. My go-to is usually w3schools:

XML HTTPRequest: https://www.w3schools.com/xml/xml_http.asp
XML Parser: https://www.w3schools.com/xml/xml_parser.asp

TouchControl has a ready-to-use xml parser object that you can use by referencing _xmlParser in any TouchControl script. So, for example, the following would load some XML into an XML DOM document using the TC parser:

xmlDoc = _xmlParser.parseFromString(myXml, "text/xml"); // where "myXml" is some xml text that you retrieved from an XML HTTPRequest call

If you would like a more in-depth example, provide a little more info on where you are getting the XML and what you'd like to do with it and I'll see what I can come up with.

Thanks!

Dieter
Posts: 10
Joined: Thu Jan 21, 2016 3:20 pm

Re: extract data from xml files

Post by Dieter » Wed Nov 15, 2017 2:26 pm

Hi Dave,

here are the infos:

1) url where I got the xml-file: http://api.wunderground.com/api/<my key>/conditions/lang:DL/q/Viersen.xml

2) The resulting xml-file:

<response>
<version>0.1</version>
<termsofService>http://www.wunderground.com/weather/api ... sofService>
<features>
<feature>conditions</feature>
</features>
<current_observation>
<image>
<url>http://icons.wxug.com/graphics/wu2/logo_130x80.png</url>
<title>Weather Underground</title>
<link>http://www.wunderground.com</link>
</image>
<display_location>
<full>Viersen, Germany</full>
<city>Viersen</city>
<state>DF</state>
<state_name>Germany</state_name>
<country>DL</country>
<country_iso3166>DE</country_iso3166>
<zip>00000</zip>
<magic>229</magic>
<wmo>10403</wmo>
<latitude>51.25999832</latitude>
<longitude>6.40000010</longitude>
<elevation>38.1</elevation>
</display_location>
<observation_location>
<full>Viersen, NRW</full>
<city>Viersen</city>
<state>NRW</state>
<country>DE</country>
<country_iso3166>DE</country_iso3166>
<latitude>51.284824</latitude>
<longitude>6.371577</longitude>
<elevation>115 ft</elevation>
</observation_location>
<estimated>
</estimated>
<station_id>INRWVIER2</station_id>
<observation_time>Last Updated on November 15, 15:01 CET</observation_time>
<observation_time_rfc822>Wed, 15 Nov 2017 15:01:03 +0100</observation_time_rfc822>
<observation_epoch>1510754463</observation_epoch>
<local_time_rfc822>Wed, 15 Nov 2017 15:10:01 +0100</local_time_rfc822>
<local_epoch>1510755001</local_epoch>
<local_tz_short>CET</local_tz_short>
<local_tz_long>Europe/Berlin</local_tz_long>
<local_tz_offset>+0100</local_tz_offset>
<weather>Nebel</weather>
<temperature_string>49.6 F (9.8 C)</temperature_string>
<temp_f>49.6</temp_f>
<temp_c>9.8</temp_c>
<relative_humidity>96%</relative_humidity>
<wind_string>Calm</wind_string>
<wind_dir>Süd</wind_dir>
<wind_degrees>180</wind_degrees>
<wind_mph>0.0</wind_mph>
<wind_gust_mph>2.0</wind_gust_mph>
<wind_kph>0.0</wind_kph>
<wind_gust_kph>3.2</wind_gust_kph>
<pressure_mb>1025</pressure_mb>
<pressure_in>30.27</pressure_in>
<pressure_trend>0</pressure_trend>

<dewpoint_string>48 F (9 C)</dewpoint_string>
<dewpoint_f>48</dewpoint_f>
<dewpoint_c>9</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>50 F (10 C)</windchill_string>
<windchill_f>50</windchill_f>
<windchill_c>10</windchill_c>

<feelslike_string>50 F (10 C)</feelslike_string>
<feelslike_f>50</feelslike_f>
<feelslike_c>10</feelslike_c>
<visibility_mi>0.7</visibility_mi>
<visibility_km>1.2</visibility_km>
<solarradiation>93</solarradiation>
<UV>0.0</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>0.00 in (0 mm)</precip_today_string>
<precip_today_in>0.00</precip_today_in>
<precip_today_metric>0</precip_today_metric>



<icon>fog</icon>
<icon_url>http://icons.wxug.com/i/c/k/fog.gif</icon_url>
<forecast_url>http://www.wunderground.com/global/stat ... recast_url>

<history_url>http://www.wunderground.com/weatherstat ... istory_url>

<ob_url>http://www.wunderground.com/cgi-bin/fin ... 77</ob_url>
</current_observation>


</response>



3) I need the following data:

<city>Viersen</city>
<weather>Nebel</weather>
<temp_c>9.8</temp_c>
<relative_humidity>96%</relative_humidity>
<wind_dir>Süd</wind_dir>
<wind_kph>0.0</wind_kph>
<pressure_mb>1025</pressure_mb>



and another example: (its for my DENON receiver)
http://192.168.1.6/goform/formNetAudio_StatusXml.xml

the xml-file:

<item><chFlag><value>0</value><value>8</value><value>0</value><value>0</value><value>0</value><value>0</value><value>0</value><value>4</value><value>0</value><value>0</value></chFlag><szLine><value>Internet Radio</value><value>Deutschland</value><value>Sender suchen</value><value>Podcast suchen</value><value>Empfohlene Sender</value><value>radiodenon.com</value><value>Kürzlich wiedergegeben</value><value>Nach Stichwort suchen</value><value> [ 1/7 ]</value><value/></szLine><NetworkNewTop><value>0</value></NetworkNewTop><iPodRemoteDirect><value>0</value></iPodRemoteDirect><Art><value>0</value></Art><Slideshow><value/></Slideshow><SlideshowLists><value index="OFF" table="OFF" param=""/><value index="05" table="05" param=""/><value index="10" table="10" param=""/><value index="15" table="15" param=""/><value index="20" table="20" param=""/><value index="30" table="30" param=""/><value index="60" table="60" param=""/></SlideshowLists><iRadio><value>0</value></iRadio><iRadioSearch><value>0</value></iRadioSearch><Flickr><value>0</value></Flickr><FlickrSearch><value>0</value></FlickrSearch><AddFlickrContact><value>0</value></AddFlickrContact><HomeBtnClck><value>0</value></HomeBtnClck><GetFavoriteList><value>0</value></GetFavoriteList><FavoriteListPage><value>0</value></FavoriteListPage><ModelId><value>20</value></ModelId><SalesArea><value>1</value></SalesArea><InputFuncSelect><value/></InputFuncSelect><NetFuncSelect><value>IRADIO</value></NetFuncSelect><selectSurround><value/></selectSurround><VolumeDisplay><value>Relative</value></VolumeDisplay><MasterVolume><value>-72.0</value></MasterVolume><Mute><value>off</value></Mute><RemoteMaintenance><value/></RemoteMaintenance><SubwooferDisplay><value>FALSE</value></SubwooferDisplay><Zone2VolDisp><value>TRUE</value></Zone2VolDisp><LastfmDisplay><value>TRUE</value></LastfmDisplay></item>

the data I need:

the first "szLine" paragraph beginning with "Internet Radio"



thanks in advance
Dieter

sfatula
Posts: 9
Joined: Wed Nov 01, 2017 10:17 pm

Re: extract data from xml files

Post by sfatula » Tue Nov 21, 2017 7:01 pm

One could always keep it simple and not worry about xml. You can extract the data directly with potentially simpler regex if you are familiar with it. So, a pattern like this <weather>(.*)<\/weather> will provide the result.

var weather = XMLSourceString.match('/<weather>(.*)<\/weather>/g');

Provides the result in weather[1]; Same applies to your Denon.

User avatar
Dave
Site Admin
Posts: 83
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: extract data from xml files

Post by Dave » Thu Nov 30, 2017 3:57 am

Hi Dieter. Really sorry for the delay getting this posted. I agree with sfatula that using regex would be easy, but using the xml parser isn't very hard either. Below is the script that you can put in a script button to extract the desired data from the XML. I just pasted your XML into a script variable named "weatherXml", but you'll need to retrieve it from wunderground.com. I would have done that, but looks like I need an API key which I don't have. But you should be able to easily retrieve it using the XMLHttpRequest. The script below simply adds the retrieved data from the XML to a Text button on the layout. The result looks like this:

Image

=====================

xmlDoc = _xmlParser.parseFromString(weatherXml, "text/xml");
city = xmlDoc.getElementsByTagName('city')[0].childNodes[0].nodeValue;
weather = xmlDoc.getElementsByTagName('weather')[0].childNodes[0].nodeValue;
temp_c = xmlDoc.getElementsByTagName('temp_c')[0].childNodes[0].nodeValue;
relative_humidity = xmlDoc.getElementsByTagName('relative_humidity')[0].childNodes[0].nodeValue;
wind_dir = xmlDoc.getElementsByTagName('wind_dir')[0].childNodes[0].nodeValue;
wind_kph = xmlDoc.getElementsByTagName('wind_kph')[0].childNodes[0].nodeValue;
pressure_mb = xmlDoc.getElementsByTagName('pressure_mb')[0].childNodes[0].nodeValue;

_appendLine('Text','Current weather for: '+city);
_appendLine('Text','Conditions: '+weather);
_appendLine('Text','Temperature: '+temp_c+'º');
_appendLine('Text','Humidity: '+relative_humidity);
_appendLine('Text','Wind: '+wind_dir);
_appendLine('Text','Pressure: '+pressure_mb+'mb');

=========================
Note that although I'm sure you can do it with with some more advanced regex, extracting data from XML that has multiple tags with the same name (like your Denon example) would be much easier, I would think, using the XML parser.

Post Reply