tags:

views:

69

answers:

2

Hi, I am required to extract the information at a particular "area" of this large collection of xml. But i'm not familiar with extracting xml. I've looked through the site and tried various ways but all i get back is "Error in my_thread_global_end(): 1 threads didn't exit"

Here's the url of the xml i'm getting my data from: ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10753.xml

I would to retrieve all the 7 forecast-period located only for the Swans Hill "area".

Help please

A: 

You may want to look at this part of the PHP manual http://php.net/manual/en/book.simplexml.php

or try a Google search for "simple xml PHP tutorial"

Steve Robillard
+2  A: 

I agree that using php's simple xml parser is the way to go with this one. You can make your life easy here using the xpath method of extracting data from the xml.

There's an xpath tutorial here: http://www.w3schools.com/xpath/

And php documentation for it here: http://www.php.net/manual/en/simplexmlelement.xpath.php

Try this out

<?php

/*
Get the file with CURL
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10753.xml');
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
$xml_data = curl_exec($curl_handle);
curl_close($curl_handle);
*/

/*
Open the file locally
*/

$xml_data = file_get_contents("weather.xml");
$xml = simplexml_load_string($xml_data);
$result = $xml->xpath("//area[@description='Swan Hill']/forecast-period");

date_default_timezone_set('America/New_York');
foreach ($result as $day) {
    //print_r($day);

    $day_of_the_week = date("l", strtotime($day["start-time-local"])); //start-time-local is an attribute of a result, so use the [] syntax
    $forecast = $day->text; //text is a child node, so use the -> syntax

    printf("%s: %s\n", $day_of_the_week, $forecast);
}
?>

EDIT More illustrative example

Jamie Wong
This is great Jamie! You basically did the job for me after the illustrative example you gave. Thank you very much!
Honshi
I can't figure out how to select the element with the type of 'air_temperature_maximum'I tried using this but it didnt work$max = $day->xpath("element[@type='air_temperature_maximum']");this one works:$min = $day->element[1]; but it doesn't give me the proper control over what to display incase one of the field is missing.
Honshi