Parsing XML with PHP

This post really is one of those that is as much for my benefit than anyone else’s but it may just be some use to other people.

I have seen a lot of tutorials that try to explain XML parsing but none have really hit the spot for me,so I thought another may be useful.

I think what was always missing was the part after getting the xml, there always seems to be some magic wand waving and out pops the needed data.

I have been working on a very simple HTML5 weather App recently that takes a RSS feed processes it and displays just the current weather condition and temperature, told you it was simple.

I managed to get the Parsing of the Weather Feed done by following a article that described exactly how to parse the Yahoo weather feed that I am using. I sort of understood the process,but not fully.  The next stage was to use the HTML5 geolocation API and then send this to the Yahoo Boss placefinder to return a WOEID that I could plug into the weather feed url, to provide the  local weather, really a necessitiy for a mobile App.

By modifying the example found at:

http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html  to work with the placefinder  as shown below.

    <?php  
    require("OAuth.php");  

    $url = "http://yboss.yahooapis.com/geo/placefinder";  

    $cc_key  = "My Consumer Key";  
    $cc_secret = "My Consumer Secret";  

    $args = array();  

    $args["location"] = "51.5082629, -0.1547288";
    $args["gflags"] = "R";

    $consumer = new OAuthConsumer($cc_key, $cc_secret);  
    $request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url,  
    $args);  
    $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);  

    $url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));  
    echo $url;
    $ch = curl_init();  
    $headers = array($request->to_header());  
    curl_setopt($ch,CURLOPT_ENCODING , "gzip");   
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
      
    //print_r("Request Headersn");  
    //print_r($headers);  
      
    $rsp = curl_exec($ch); 
    
    $xml = simplexml_load_string($rsp);
 
    print_r("nHere is the XML response for Placefindern");  
    print_r($rsp);  

//Below is the parsed WOEID value
$woeid = $xml->placefinder[0]->results[0]->result->woeid;
echo $woeid;
?>

This gives the following xml response

<?xml version="1.0" encoding="UTF-8"?>
<bossresponse responsecode="200">
  <placefinder start="0" count="1" request="location=51.5082629%2C%20-0.1547288&amp;gflags=R">
    <results>
      <result>
        <quality>70</quality>
        <latitude>51.508292</latitude>
        <longitude>-0.154723</longitude>
        <offsetlat>51.508292</offsetlat>
        <offsetlon>-0.154723</offsetlon>
        <radius>400</radius>
        <name>51.5082629, -0.1547288</name>
        <line1>51.5082629, -0.1547288</line1>
        <line2>London</line2>
        <line3>W1K 7</line3>
        <line4>United Kingdom</line4><house></house><street></street><xstreet></xstreet><unittype></unittype><unit></unit>
        <postal>W1K 7</postal>
        <neighborhood>W1</neighborhood>
        <city>London</city>
        <county>London</county>
        <state>England</state>
        <country>United Kingdom</country>
        <countrycode>GB</countrycode>
        <statecode>ENG</statecode>
        <countycode>LND</countycode>
        <uzip>W1K 7</uzip><hash></hash>
        <woeid>26352766</woeid>
        <woetype>11</woetype></result></results></placefinder></bossresponse>



The item that we are intersested in is at the very bottom of the output

<woeid>26352766</woeid>

I think one of the problems I have had in understanding how to parse XML is that I was expecting to be able to enter a command a supply the XML  tag as  a parameter and get the data back, well it doesn’t quite work like  that.

I think the best way to think about it is whenever the word parse is used is to swap it for ‘get the data from the xml’, in my head.

So lets crack on.

To Parse the XML we are going to use simpleXML , there are other ways to parse XML in PHP but for files that aren’t hundreds of Megs simpleXML is regarded to be the best, it was introduced in PHP5 to address the need of an XML parser that is easy to use.

So first stage  is to create a simplexml object :

$xml = simplexml_load_string($rsp);

The next step is to create a path to the woeid in the xml object, probably the easiest way to do this is to work backwards.

Ignoring all of the other data and just concentrating on the <woeid> tag we can see the structure of the XML looks something like

<placefinder>

<results>

<result>

<woeid>26346525</woeid>

</result>

</results>

</placefinder>

In the xml file the <bossresponse responsecode=”200”> is the route tag. All xml files have a route tag which must contain all the other contents of the xml file, so we don’t need to refer to it when getting the woeid.

Looking through the xml response. the tag placefinder only contains further levels of tags no any actual data so we need to think of it as an array that contains other arrays hence referring to it as placefinder[0]. The same is trued for results. the result tag contains the woeid tag and the woeid tag contains the actual data we want, so both of those can be referred to directly.

$woeid = $xml->placefinder[0]->results[0]->result->woeid;


And that is all there is to it. If you are struggling to understand hot to parse xml feeds with php I would encourage you to have a go,read a few different articles and look at Stack overflow if you want more information

Comments are closed.