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
pekern1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amar BardoliwalaCommented:
Hello pekern1,

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
0
StingRaYCommented:
Try this:

<?php
$xml = file_get_contents('http://www.wrh.noaa.gov/mesowest/getobextXml.php?sid=KTCM&num=1');
$x = simplexml_load_string($xml);
$vars = $x->xpath('/station/ob/variable');
foreach ($vars as $var) {
	echo 'var='.(string)$var['var'].' value='.(string)$var['value']."\n";
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pekern1Author Commented:
I can definitely run with this!
Now, the magic will be in the foreach() loop.
Thank you very much.
-Pat
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Ray PaseurCommented:
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
<?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";
    }
}

Open in new window

0
pekern1Author Commented:
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
0
Ray PaseurCommented:
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
0
pekern1Author Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.