Mar 5, 2010

SimpleXML Paring the Data

In my last post I should you how I was able to get the data from the xml dump to be displayed in the browser. The problem with that was that it was just data. It was unorganized and in no logical format that the user could discern, well all that is about to change. While it took a little time for me to understand how to parse the data, once I figured it out, things moved along fairly quickly. I chose to parse the data using SimpleXML and Xpath in my PHP proxy function. I found that was the easiest way to get the data formatted for proper display. More can be found on the Internet about SimpleXML and Xpath.

The most important part of this process is to get the data into a variable so it can be parsed. If you look at my last post you will see the process of creating the curl session. Once the session is complete you need to take that $data variable and use it to create the $xml variable.

$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $fullUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($data);

This simple line takes the $data returned from the curl session and puts it into a SimpleXML format so that we can parse it. THe next step is to look through your xml by taking the url in your request and putting that into the firefox browser. This will show you the tags and their attributes that are returned so you can figure out what you need. For this instance I used:

http://a6357896562.api.wxbug.net/getLiveWeatherRSS.aspx?ACode=A6357896562&OutputType=1&zipCode=48189

I ended up with the following output:




From here I can start to figure out the tags I want to grab. I do this by using a object type structure. I created variables in the PHP script to hold the resutls of the query. In this one $zip = $xml->xpath('//aws:city-state/@zipcode'); I search for the aws:city-state tag and then I grab the attribute "zipcode".

$timezone = $xml->xpath('//aws:time-zone/@text');
$temphigh = $xml->xpath('/aws:weather/aws:ob/aws:temp-high/text()');
$templow = $xml->xpath('/aws:weather/aws:ob/aws:temp-low/text()');
$feelslike = $xml->xpath('/aws:weather/aws:ob/aws:feels-like/text()');
$city = $xml->xpath('/aws:weather/aws:ob/aws:city-state/text()');
$zip = $xml->xpath('//aws:city-state/@zipcode');
$current_temp = $xml->xpath('/aws:weather/aws:ob/aws:temp /text()');
$elevation = $xml->xpath('/aws:weather/aws:ob/aws:elevation/text()');
$wind = $xml->xpath('/aws:weather/aws:ob/aws:wind-speed/text()');
$lat = $xml->xpath('/aws:weather/aws:ob/aws:latitude/text()');
$long = $xml->xpath('/aws:weather/aws:ob/aws:longitude/text()');
$icon = $xml->xpath('//aws:current-condition/@icon');

Once I had all the data that I need I then format the data into a nice presentable form by using a series of HTML Table tags. This allows me to creat the format and then just hand the JAvascript function something that it needs to spit out. Unfortunately the blog is freaking out and wont let me upload a picture so I put it on my emich site instead Here...Good Luck

Here is the finished Product:

1 comment:

  1. You did great job... I really appreciate your explanation of how You did it. Initially It was very hard for me to understand how we parse the xml data...it was nothing but a impossible puzzle for me..thanks for all your help

    ReplyDelete