Avatar of hankknight
hankknightFlag for Canada

asked on 

Parse XML Weather Feed

I want to parse XML to find the high and low temperature values for several days.

----------------------------------------
Highs

Today:  69
Fri:  60
Sat: 64
Sun: 64

Lows

Today:  55
Fri:  55
Sat: 53
Sun: 50

<pre>
<?php
 
$p = xml_parser_create();
 
$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>';
 
xml_parse_into_struct($p, $xml, $vals, $index);
 
xml_parser_free($p);
 
echo "Index array\n";
 
print_r($index);
 
echo "\nVals array\n";
 
print_r($vals);
 
?>
</pre>

Open in new window

PHP

Avatar of undefined
Last Comment
hankknight
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany 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 ifp_support
ifp_support
Flag of United Kingdom of Great Britain and Northern Ireland image

Try using simble XML instead, the code below gives you two arrays, highs and lows. It puts the day name as the array key, and the resulting temp as the value for easy usage.


$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>';
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml); 
$s = simplexml_import_dom($doc);
$highs = array();
$lows = array();
foreach($s->weather->forecast_conditions as $day)
{
	$butes = $day->day_of_week->attributes();
	$theday =  (string)$butes['data'][0];
	
	$butes = $day->low->attributes();
	$low =  (string)$butes['data'][0];
	
	$butes = $day->high->attributes();
	$high =  (string)$butes['data'][0];	
 
	$highs[$theday] = $high;
	$lows[$theday] = $low;
}
 
print_r($highs);
print_r($lows);
 
Array
(
    [Today] => 69
    [Fri] => 60
    [Sat] => 64
    [Sun] => 64
)
Array
(
    [Today] => 55
    [Fri] => 55
    [Sat] => 53
    [Sun] => 50
)

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

try:
<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 ['Highs'][$element->childNodes->item(0)->getAttribute("data")]= $element->childNodes->item(2)->getAttribute("data");
	$data ['Lows'][$element->childNodes->item(0)->getAttribute("data")]= $element->childNodes->item(1)->getAttribute("data");
}
var_dump($data);
?>
</pre>

Open in new window

Avatar of hankknight
hankknight
Flag of Canada image

ASKER

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