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.



2 comments:

  1. Parsing is the part that really has me scared. I think I am just making it harder than it is. We only have 7 weeks left of class, I hope we get it figured out.

    ReplyDelete
  2. I was most worried about parsing as well. I haven't tried to do it yet, but I'm glad you posted this. It'll definitely be useful to me, thanks.

    ReplyDelete