how to parse child nodes
i am very, very very new to all of this.. seem to have got what i wanted for the most part.. i am putting a little weather widget on our business website... and have everthing working fine except sunset, sunrise...
i think because it has subcatagories..
how do i pull these??
see below:
<sunset>
<hour>20</hour>
<minute>48</minute>
</sunset>
−
<sunrise>
<hour>5</hour>
<minute>06</minute>
</sunrise>
im trying to call it using: $sun = "Dawn: $sunrise - Dusk: $sunset";
but it comes up just blank
if i just use $hour:$minute it defaults to the sunrise hour minute (must read bottom to top?)
im sure this is super simple. i am way over my head.. but hey.. so far it looks great!
thanks again!
pj
10 Months Ago
Last Updated
pjrey
Junior Poster in Training
67 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
man, been at this all day.. and getting no where...
$file = "http://api.wunderground.com/api/conditions/forecast/astronomy/q/ZIP.xml";
$xml_parser = xml_parser_create();
if (file_exists($file)){
$data = implode('', file($file));
} else {
$fp = fopen($file,'r');
while(!feof($fp)){
$data = $data . fread($fp, 1024);
}
fclose($fp);
}
xml_parse_into_struct( $xml_parser, $data, $vals, $index );
xml_parser_free( $xml_parser );
$t = true;
foreach ($vals as $key => $i) {
if ($vals[$key]['value'] != false) {
if ($vals[$key]['tag'] != 'T') {
$temp_f = strtolower($vals[$key]['tag']);
$$temp_f = $vals[$key]['value'];
} elseif ($t == true) {
if ($vals[$key]['level'] == 3) $conditions = $vals[$key]['value'];
else { $wind_mph = $vals[$key]['value']; $t = false; break; }
}
}
}
i am able to call elements like this, $wind, $temp_f etc...
but there is sunrise
<sunrise>
<hour>
<minute>
and <sunset>
<hour>
<minute>
i dont know how to call sunrise hour:minute
i dont want to put them in an array.. i fear they will change order... wheras now i can call them by the names they are given in the xml file
does this make any sense..
thanks
p
pjrey
Junior Poster in Training
67 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
<response>
<version>0.1</version>
−
<termsofService>
http://www.wunderground.com/weather/api/d/terms.html
</termsofService>
−
<features>
<feature>conditions</feature>
<feature>astronomy</feature>
</features>
−
<current_observation>
−
<image>
−
<url>
http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png
</url>
<title>Weather Underground</title>
<link>http://www.wunderground.com</link>
</image>
<station_id>MTS546</station_id>
<observation_time>Last Updated on May 31, 6:44 PM CDT</observation_time>
<observation_time_rfc822>Thu, 31 May 2012 18:44:00 -0500</observation_time_rfc822>
<observation_epoch>1338507840</observation_epoch>
<local_time_rfc822>Thu, 31 May 2012 19:11:13 -0500</local_time_rfc822>
<local_epoch>1338509473</local_epoch>
<local_tz_short>CDT</local_tz_short>
<local_tz_long>America/Chicago</local_tz_long>
<local_tz_offset>-0500</local_tz_offset>
<weather>Clear</weather>
<temperature_string>57 F (13.9 C)</temperature_string>
<temp_f>57</temp_f>
<temp_c>13.9</temp_c>
<relative_humidity>33%</relative_humidity>
<wind_string>From the SW at 6 MPH Gusting to 13.0 MPH</wind_string>
<wind_dir>SW</wind_dir>
<wind_degrees>228</wind_degrees>
<wind_mph>6</wind_mph>
<wind_gust_mph>13.0</wind_gust_mph>
<wind_kph>9.7</wind_kph>
<wind_gust_kph>20.9</wind_gust_kph>
<pressure_mb>1017</pressure_mb>
<pressure_in>30.03</pressure_in>
<pressure_trend>0</pressure_trend>
<dewpoint_string>28 F (-2 C)</dewpoint_string>
<dewpoint_f>28</dewpoint_f>
<dewpoint_c>-2</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>57 F (13.9 C)</feelslike_string>
<feelslike_f>57</feelslike_f>
<feelslike_c>13.9</feelslike_c>
<visibility_mi>10.0</visibility_mi>
<visibility_km>16.1</visibility_km>
<solarradiation>342</solarradiation>
<UV>2</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>clear</icon>
<icon_url>http://icons-ak.wxug.com/i/c/k/clear.gif</icon_url>
<forecast_url>http://www.wunderground.com/US/MN/Hovland.html</forecast_url>
−
<history_url>
http://www.wunderground.com/history/airport/MTS546/2012/5/31/DailyHistory.html
</history_url>
−
<ob_url>
http://www.wunderground.com/cgi-bin/findweather/getForecast?query=47.950298,-89.779999
</ob_url>
</current_observation>
−
<moon_phase>
<percentIlluminated>83</percentIlluminated>
<ageOfMoon>11</ageOfMoon>
−
<current_time>
<hour>19</hour>
<minute>11</minute>
</current_time>
−
<sunset>
<hour>20</hour>
<minute>50</minute>
</sunset>
−
<sunrise>
<hour>5</hour>
<minute>05</minute>
</sunrise>
</moon_phase>
</response>
is the one i am using now.. but there is another one that i will be using with forecasts.. but i will run into the same problem... because the child tags are the same name for all the days it is the parent that is unique...
i have it cron-jobed every ten minuts.. and written to a text file in html, and i insert the txt file into my page...
you can see it in action here: http://naniboujou.com (bottom center)
i want to add sunrise and sunset, and then i will have it when you click it, it will give out a three day forecast
thank you for responding!
pj
pjrey
Junior Poster in Training
67 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
my code is a messsss.. it is working, but i dont know what i am really doing... what i want, is a script that will read the xml file and write it to my txt file (i want it to write the current weather and condition to weather.txt), and the forecasted weather to weather_forecast.txt) i have a cron job set for every 10 mins.
so parse the data, write it to two files
for someone who knows what they are doing i imaging this would be very easy... i know close to nothing with php (i can cobble things together is about the extent of my php abilities.. if it isnt already clear_)
thanks!
pj
pjrey
Junior Poster in Training
67 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
anybody?? i need some helppp
pjrey
Junior Poster in Training
67 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
can anyone point me to a forum where i may get some help with this question?
pjrey
Junior Poster in Training
67 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0