0

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.