Showing posts with label SimpleXML. Show all posts
Showing posts with label SimpleXML. Show all posts

Mar 9, 2010

Next Step: XML Parsing

I found Dr. Drake's example of parsing to complex as it had that drop down box in it. I need an example with out the code for the drop down to walk through to find out where the code for drop down started and stopped.

I found this website called Kirupa.com and there was a nice walk through of what you needed to parse using SimpleXML. It included a brief description of XML in case you had stumbled on to the page. Then it went in to showing you the code and what each line was and why it was needed.

I also found a decent explanation on the DOM parser at the W3 Schools. It is good example of looping through the xml to pulled it out in to readable text. See code posted from the site below:


$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");

$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "
";

}
?>

If I get time before class tomorrow I will see if I can get my rss feed for Iteration 2 parsed out.

Mar 5, 2010

More on Parsing XML

I have been working quite a bit more on the parsing portion of Iteration 3 for the projects and I have found that using Xpath is both a very simple as well as a effective method of parsing the XML. So far I have been able to find tons of documentation as well of examples of how to search for the tags that you need as well as using that data. I used the weather example in my last post but for this on I will use the CareerBuilder API as the example. Here is the beginning of the PHP script:

$zip = $_REQUEST['zipID']; //Here I get the variables from the Javascript
$job = $_REQUEST['jobID'];

$url = 'http://api.careerbuilder.com/v1/jobsearch?&DeveloperKey=WDhb6SR73QBGH8TTJV4T&Location=';
$url2 = '&Keywords=';
$fullUrl = $url.$zip.$url2.$job; //Here I concatenate the strings into one URL

Here I pass the PHP script two variables from my Javascirpt, a job to search for as well as a zip code to search within a given location. Here is the javascript so it makes a bit more sense. I pass the job variable as a parameter while the zip variable is a global so it can be accessed anywhere in the Javascript.

//*********************************************************************************

function getJob(job) { //Here I'm passing the job variable to the function
jbRequest = jobRequest();
if (jbRequest == null) {
return;
}

url= "scripts/career.php?zipID=" + escape(zip) +"&jobID=" + escape(job); //Passing to PHP
jbRequest.open("GET", url, true);
jbRequest.onreadystatechange = displayJob;
jbRequest.send(null);

}


Once in the PHP script I do the standard request for the data

$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $fullUrl); //Here is the full URL variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch); //Here I put the request return into the data variable
curl_close($ch);


Next I use SimpleXML to get the $data variable into an accessible variable.

$xml = new SimpleXMLElement($data);

Next I start searching for the data. Here is the output from the URL that is passed to the API.

http://api.careerbuilder.com/v1/jobsearch?&DeveloperKey=WDhb6SR73QBGH8TTJV4T&Location=48189&Keywords=Computer%20Programmer




Next I begin the the search. Here I search for all tags=JobSearchResults

$attr = $xml->xpath('//JobSearchResult'); //Search for all tags JobSearchResults and put them in an Array

This data is put into an array called $attr (for attributes) I made this up and it can be anything that you want. Once the data is there I can begin getting the info that I need.

$count = count($attr); //Count the number of results and use that in the for loop

for($i=0; $i<$count; $i++){ echo $attr[$i]->Company; //Access the data in the JobSearchResults/Company tag
echo $attr[$i]->JobTitle /Access the data in the JobSearchResults/JobTitle
echo $attr[$i]->Pay
echo $attr[$i]->Location

}

Well I hope that this information is helpful for someone. Here is the output from the parse.



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: