Avatar of hankknight
hankknightFlag for Canada

asked on 

Parse XML Weather Forecast by Day

Hello,

I use the following code to get weather information from XML.
https://www.experts-exchange.com/questions/23573410/Parse-XML-Weather-Feed.html

I want to display information by each day.  Now information is stored by type (high or low).

At the bottom of my code is how I want it the be displayed.
Thanks!
<?php
echo "<pre>";
$xml = '<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0"><forecast_information><city data="Schriesheim, BW"/><postal_code data="Schriesheim,Germany"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2008-07-17"/><current_date_time data="2008-07-17 17:16:29 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data=""/><temp_f data="64"/><temp_c data="18"/><humidity data="Humidity: 76%"/><icon data=""/><wind_condition data="Wind: SE at 1 mph"/></current_conditions><forecast_conditions><day_of_week data="Today"/><low data="55"/><high data="69"/><icon data="/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions><forecast_conditions><day_of_week data="Fri"/><low data="55"/><high data="60"/><icon data="/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions><forecast_conditions><day_of_week data="Sat"/><low data="53"/><high data="64"/><icon data="/images/weather/rain.gif"/><condition data="Rain"/></forecast_conditions><forecast_conditions><day_of_week data="Sun"/><low data="50"/><high data="64"/><icon data="/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions></weather></xml_api_reply>';
 
$s = simplexml_load_string($xml);
 
foreach($s->weather->forecast_conditions as $day) {
 
    $result['high'][(string)$day->day_of_week[0]{'data'}] = (string)$day->high[0]{'data'};
    $result['low'][(string)$day->day_of_week[0]{'data'}] = (string)$day->low[0]{'data'};
    $result['condition'][(string)$day->day_of_week[0]{'data'}] = (string)$day->condition[0]{'data'};
    $result['icon'][(string)$day->day_of_week[0]{'data'}] = (string)$day->icon[0]{'data'};
}
 
var_dump($result);
echo "</pre>";
 
echo "<h1>Today</h1>\n";
echo "High: 69 <br />\n";
echo "Low: 55 <br />\n";
echo "Condition: Chance of Rain <br />\n";
 
echo "<h1>Fri</h1>\n";
echo "High: 77 <br />\n";
echo "Low: 55 <br />\n";
echo "Condition: Chance of Rain <br />\n";
 
 
echo "<h1>Sat</h1>\n";
echo "High: 64 <br />\n";
echo "Low: 53 <br />\n";
echo "Condition: Rain<br />\n";
 
echo "<h1>Sun</h1>\n";
echo "High: 64 <br />\n";
echo "Low: 50 <br />\n";
echo "Condition: Chance of Rain<hr />\n";
 
?>

Open in new window

PHP

Avatar of undefined
Last Comment
hielo
ASKER CERTIFIED SOLUTION
Avatar of ifp_support
ifp_support
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Try this:
<pre>
<?php
$xml = '<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0"><forecast_information><city data="Schriesheim, BW"/><postal_code data="Schriesheim,Germany"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2008-07-17"/><current_date_time data="2008-07-17 14:01:59 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data=""/><temp_f data="66"/><temp_c data="19"/><humidity data="Humidity: 71%"/><icon data=""/><wind_condition data="Wind: SW at 3 mph"/></current_conditions><forecast_conditions><day_of_week data="Today"/><low data="55"/><high data="69"/><icon data="/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions><forecast_conditions><day_of_week data="Fri"/><low data="55"/><high data="60"/><icon data="/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions><forecast_conditions><day_of_week data="Sat"/><low data="53"/><high data="64"/><icon data="/images/weather/rain.gif"/><condition data="Rain"/></forecast_conditions><forecast_conditions><day_of_week data="Sun"/><low data="50"/><high data="64"/><icon data="/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions></weather></xml_api_reply>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$node = $dom->getElementsByTagName("forecast_conditions");
$data = array(); 

foreach( $node as $element )
{
	$data [$element->childNodes->item(0)->getAttribute("data")]= array('High'=>$element->childNodes->item(1)->getAttribute("data"), 'Low'=>$element->childNodes->item(2)->getAttribute("data"), 'Condition'=>$element->childNodes->item(4)->getAttribute("data"));
} 
foreach($data as $day => $data )
{
	echo "<h1>$day</h1>";
	foreach($data as $info => $value )
		echo "<div>$info: $value</div>";
} 
?>
</pre>

Open in new window

PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo