Wednesday, 20 January 2016

XML : Javascript XML parsing of NOAA weather data - problems extracting data from child nodes

I'd love some help with Javascript code I'm writing to extract weather data from NOAA xml files (downloaded from here: http://graphical.weather.gov/xml/SOAP_server/ndfdXML.htm ). For now I've just pasted in the relevant part of the XML as a string:

  var xmlDoc = $.parseXML("<data>\  <weather time-layout=\"k-p3h-n40-2\">\  <name>Weather Type, Coverage, and Intensity</name>\  <weather-conditions/>\  <weather-conditions/>\  <weather-conditions/>\  <weather-conditions>\  <value coverage=\"areas\" intensity=\"none\" weather-type=\"sun\" qualifier=\"none\">\  <visibility xsi:nil=\"true\"/>\  </value>\  </weather-conditions>\  <weather-conditions>\  <value coverage=\"areas\" intensity=\"none\" weather-type=\"rain\" qualifier=\"none\">\  <visibility xsi:nil=\"true\"/>\  </value>\  </weather-conditions>\  <weather-conditions>\  <value coverage=\"areas\" intensity=\"none\" weather-type=\"fog\" qualifier=\"none\">\  <visibility xsi:nil=\"true\"/>\  </value>\  </weather-conditions>\  </data>")    

I'm trying to extract all the 'weather-type' attributes using the following code:

  var count = 0  var test_weather = new Array()  $(xmlDoc).find('weather').each(function(){    $(this).find('weather-conditions').each(function(){      $(this).find('value').each(function(){        test_weather[count] = $(this).attr('weather-type')        count=count+1      })    })  })    

But this only finds the first weather type value, and I can't work out why! Any suggestions for what I'm doing wrong, or suggestions for how to improve my code, would be greatly appreciated!

No comments:

Post a Comment