How extract XML data using PHP
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
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
精彩评论