Skip to main content
Inspiring
June 1, 2015
Answered

How To Read NOAA XML "weather-summary" Attribute?

  • June 1, 2015
  • 1 reply
  • 1776 views

Can someone help with Flash CS6 ActionScript 3 E4X syntax for reading National Weather Service’s XML feed (National Weather Service)? 

I’ve been able to address most of the needed fields; but, XML attribute "weather-summary" is driving me to drink!  Looping, I can locate it, but results always come back blank.  Any help is appreciated.  I’ve tried a number of approaches including:

for each(var elemParamters:XML in rssXML.data.(@type == "current observations").elements("parameters")) {
       :

       :

     

for each (var elemWeather:XML in elemParamters.weather.elements("weather-conditions") ) {
       if ( elemWeather.hasOwnProperty("@weather-summary") ) {
        currentConditions = elemWeather.("@weather-summary");
        currentConditions = elemWeather.("@weather-summary[0]");
        trace("\n elemWeather = {" + elemWeather +"}");
        }
       }
      trace("\n currentConditions? = {" + currentConditions +"}");

       :

       :

Sample XML:

<dwml>
<data>
:
:
:
</data>
<data type="current observations">
<location>
  <location-key>point1</location-key>
  <point latitude="39.11" longitude="-84.42"/>
  <area-description>Cincinnati, Cincinnati Municipal Airport Lunken Field, OH</area-description>
  <height datum="mean sea level" height-units="feet">482</height>
</location>
<moreWeatherInformation applicable-location="point1">http://www.nws.noaa.gov/data/obhistory/KLUK.html</moreWeatherInformation>
<time-layout time-coordinate="local">
  <layout-key>k-p1h-n1-1</layout-key>
  <start-valid-time period-name="current">2015-04-29T13:53:00-04:00</start-valid-time>
</time-layout>
<parameters applicable-location="point1">
  <temperature type="apparent" units="Fahrenheit" time-layout="k-p1h-n1-1">
   <value>65</value>
  </temperature>
  <temperature type="dew point" units="Fahrenheit" time-layout="k-p1h-n1-1">
   <value>43</value>
  </temperature>
  <humidity type="relative" time-layout="k-p1h-n1-1">
   <value>45</value>
  </humidity>
  <weather time-layout="k-p1h-n1-1">
   <name>Weather Type, Coverage, Intensity</name>
   <weather-conditions weather-summary="Partly Cloudy"/>
   <weather-conditions>
    <value>
     <visibility units="statute miles">10.00</visibility>
    </value>
   </weather-conditions>
  </weather>
  <conditions-icon type="forecast-NWS" time-layout="k-p1h-n1-1">
   <name>Conditions Icon</name>
   <icon-link>http://forecast.weather.govnewimages/medium/sct.png</icon-link>
  </conditions-icon>
  <direction type="wind" units="degrees true" time-layout="k-p1h-n1-1">
   <value>999</value>
  </direction>
  <wind-speed type="gust" units="knots" time-layout="k-p1h-n1-1">
   <value>15</value>
  </wind-speed>
  <wind-speed type="sustained" units="knots" time-layout="k-p1h-n1-1">
   <value>5</value>
  </wind-speed>
  <pressure type="barometer" units="inches of mercury" time-layout="k-p1h-n1-1">
   <value>29.84</value>
  </pressure>
</parameters>
</data>
</dwml>

This topic has been closed for replies.
Correct answer GuyMcMickle

you're welcome.

p.s when using the adobe forums, please mark helpful/correct responses, if there are any.


Technique for loop for(var i:int=0;i<outputxml..weather.length();i++){ could be something like:

var iCondition:Number = -1;
for(var i:int=0; i<rssXML...weather.length(); i++){
    ++ iCondition;
    if ( iCondition == 1 ) { currentConditions = rssXML..weather['weather-conditions'].@['weather-summary']; };
  };

Solution for question as originally asked turns out to be method toXMLString().  XML data structure was filtered down to working with only the qualified XML region.  Here's the AS3 E4X code:


     for each(var elemParamters:XML in rssXML.data.(@type == "current observations").elements("parameters")) {
      for each (var elemWeather:XML in elemParamters.weather.elements("weather-conditions") ) {
       if ( elemWeather.hasOwnProperty("@weather-summary") ) {
                 currentConditions = elemWeather.attribute("weather-summary").toXMLString();

trace("\n currentConditions? = {" + currentConditions +"}");

// trace output:     currentConditions? = {Partly Cloudy}

}

       }

It's not clear why to get needed results XML method toString() is wrong and toXMLString() is correct, when objective is to extract XML to a string?

kglad; Thanks again for working with me on my question. 

Guy McMickle

1 reply

kglad
Community Expert
Community Expert
June 2, 2015

use bracket notation to access hyphenated nodes/attributes.  eg, if you assign sampleXML to samplexml = XML(e.target.data) in your complete listener:

for(var i:int=0;i<samplexml..weather.length();i++){

samplexml..weather['weather-conditions'].@['weather-summary']);

}

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

Inspiring
June 2, 2015

For me, it’s not that simple.  I does not work.

I previously tried unsuccessfully using brackets.  It still returns an empty result (instead of ”Partly Cloudy”).  Weather XML element/attribute names are a hodge/podge mix of program language reserved words/mathematical operators.  It makes stepping through XML structure a land mine. 

 

Encapsulating “for each” loop had targeted down to just the “weather-condition” elements.  The 1st weather-conditions element (of 2) is (currently) the only one containing attribute “weather-summary” and it has no element value.  That’s why loop first matches hasOwnProperty("@weather-summary").  Loop works. It just doesn’t pass the attribute value:

      for each (var elemWeather:XML in elemParamters.weather.elements("weather-conditions") ) {
        if ( elemWeather.hasOwnProperty("@weather-summary") ) {
          trace("\n elemWeather = {" + elemWeather +"}");
         }
       }
       trace("\n currentConditions? = {" + currentConditions +"}"); 

// trace output: currentConditions? = {}

I tried your index method, but it also fails. Using only square brackets returns zero XML collection length.  Elements() and atrubute() finds data.  But with work arounds for XMLs funky mix of name/attribute , index syntax (on element “weather-condition”) does not “fit”.

   var lenCond:int = -1;

     var currentConditions2:String;

     lenCond = elemParamters.weather.elements("weather-conditions").length();

     trace("\n elemParamters.weather.['weather-conditions'].length() = {" + lenCond +"}");

/ trace output: elemParamters.weather.['weather-conditions'].length() = {2}

     for (var i:int=0; i<lenCond; i++){

           // fails:

           currentConditions2 = elemParamters.weather.element("weather-conditions").attribute("weather-summary");

           trace("\n currentConditions2? = {" + currentConditions2 +"}");

     }   

I'm hoping for any working syntax... 

kglad
Community Expert
Community Expert
June 2, 2015

it works.

show your load method and load complete listener.