pekern1
asked on
PHP, XML <variable/>, and confusion
This sample XML string is read directly from URL, http://www.wrh.noaa.gov/mesowest/getobextXml.php?sid=KTCM&num=1
<?xml version="1.0" encoding="ISO-8859-1"?>
<station id="KTCM" name="Tacoma / McChord Air Force Base" elev="322" lat="47.15" lon="-122.48333" provider="NWS/FAA">
<ob time="09 Dec 9:55 pm" utime="1323496500">
<variable var='T' description='Temp' unit='F' value='25'/>
<variable var='TD' description='Dewp' unit='F' value='25'/>
<variable var='RH' description='Relh' unit='%' value='98'/>
<variable var='DD' description='Direction' unit='deg' value='170'/>
<variable var='DDCARD' description='Wind Card' unit='direction' value='S'/>
<variable var='FF' description='Wind' unit='mph' value='3'/>
<variable var='VV' description='Visibility' unit='miles' value='- 0.25'/>
<variable var='PRESWEA' description='Coded Weather' unit='' value='BR'/>
<variable var='SKY' description='Clouds' unit='' value='CLR'/>
<variable var='SLP' description='SL Pres' unit='mb' value='102.2'/>
<variable var='ALTSE' description='Altimeter' unit='inches' value='30.16'/>
<variable var='P' description='Station Pressure' unit='inches' value='29.820'/>
<variable var='T6MAX' description='6 Hr Max Temp' unit='f' value='36'/>
<variable var='T6MIN' description='6 Hr Min Temp' unit='f' value='25'/>
<variable var='STAQUAL' description='Station Quality' unit='' value='OK'/>
</ob>
</station>
I need to extract the "var=xx", and "value=xx" from <variable/> and am completely lost on how to this in PHP. At this point, I'm retrieving the URL as a string, file_get_contents, and then that's about as far as I can manipulate.
Thanks a lot!
-Pat
<?xml version="1.0" encoding="ISO-8859-1"?>
<station id="KTCM" name="Tacoma / McChord Air Force Base" elev="322" lat="47.15" lon="-122.48333" provider="NWS/FAA">
<ob time="09 Dec 9:55 pm" utime="1323496500">
<variable var='T' description='Temp' unit='F' value='25'/>
<variable var='TD' description='Dewp' unit='F' value='25'/>
<variable var='RH' description='Relh' unit='%' value='98'/>
<variable var='DD' description='Direction' unit='deg' value='170'/>
<variable var='DDCARD' description='Wind Card' unit='direction' value='S'/>
<variable var='FF' description='Wind' unit='mph' value='3'/>
<variable var='VV' description='Visibility' unit='miles' value='- 0.25'/>
<variable var='PRESWEA' description='Coded Weather' unit='' value='BR'/>
<variable var='SKY' description='Clouds' unit='' value='CLR'/>
<variable var='SLP' description='SL Pres' unit='mb' value='102.2'/>
<variable var='ALTSE' description='Altimeter' unit='inches' value='30.16'/>
<variable var='P' description='Station Pressure' unit='inches' value='29.820'/>
<variable var='T6MAX' description='6 Hr Max Temp' unit='f' value='36'/>
<variable var='T6MIN' description='6 Hr Min Temp' unit='f' value='25'/>
<variable var='STAQUAL' description='Station Quality' unit='' value='OK'/>
</ob>
</station>
I need to extract the "var=xx", and "value=xx" from <variable/> and am completely lost on how to this in PHP. At this point, I'm retrieving the URL as a string, file_get_contents, and then that's about as far as I can manipulate.
Thanks a lot!
-Pat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I can definitely run with this!
Now, the magic will be in the foreach() loop.
Thank you very much.
-Pat
Now, the magic will be in the foreach() loop.
Thank you very much.
-Pat
Pat: Here is how I might do it. See http://www.laprbass.com/RAY_temp_pekern1.php
Note the use of (string) to explicitly type-cast the elements of the object. Echo does not care about that, but some other PHP functions might.
Best regards, ~Ray
Note the use of (string) to explicitly type-cast the elements of the object. Echo does not care about that, but some other PHP functions might.
Best regards, ~Ray
<?php // RAY_temp_pekern1.php
error_reporting(E_ALL);
echo "<pre>";
// GET THE DATA INTO AN OBJECT
$url = 'http://www.wrh.noaa.gov/mesowest/getobextXml.php?sid=KTCM&num=1';
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);
// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);
// USE THE OBJECT TO CREATE HTML OUTPUT
$s = (string)$obj["name"];
// USE AN ITERATOR TO ACCESS THE OBSERVATIONS AND VALUES
foreach ($obj->ob as $ob)
{
$t = (string)$ob["time"];
echo PHP_EOL . "On <b>$t</b> at <b>$s</b> ...";
foreach ($ob->variable as $var)
{
$d = (string)$var["description"];
$v = (string)$var["value"];
$u = (string)$var["unit"];
echo PHP_EOL . "$d: $v $u";
}
}
ASKER
Ray,
Thank you. This is a great option. I will defintiely keep this code handly and give credit to you if I use it.
-Pat
Thank you. This is a great option. I will defintiely keep this code handly and give credit to you if I use it.
-Pat
Thanks, no credit needed at all. It's open source when I post it here. Besides, this question reminds me of the English student who sent off $100 for the "How to Write a Novel" kit. When it arrived he found a dictionary and a card that said, "Some assembly required." Glad we could help with the assembly!
;-)
~Ray
;-)
~Ray
ASKER
Yes indeed. The two responses I received on this were definitely a different direction than I was going. :)
When in a hole, quit digging and ask for directions out.
-Pat
When in a hole, quit digging and ask for directions out.
-Pat
Use function given on following link
http://www.bin-co.com/php/scripts/xml2array/
It should help you to solve your problem.
Hope this will help.
Thank You.
Amar Bardoliwala