weather file - accessing xml data
am 24.09.2007 04:27:23 von z1
hi -
i downloaded some data from a weather service that is in an xml file.
["parameters"]=> object(SimpleXMLElement)#9 (4) { ["temperature"]=> array(2)
{ [0]=> object(SimpleXMLElement)#11 (2) { ["name"]=> string(25) "Daily
Maximum Temperature" ["value"]=> array(6) { [0]=>
object(SimpleXMLElement)#16 (0) { } [1]=> string(2) "84" [2]=> string(2)
"89" [3]=> string(2) "87" [4]=> string(2) "82" [5]=> string(2) "78" } }
[1]=> object(SimpleXMLElement)#12 (2) { ["name"]=> string(25) "Daily Minimum
Temperature" ["value"]=> array(5) { [0]=> string(2) "48" [1]=> string(2)
"54" [2]=> string(2) "61" [3]=> string(2) "60" [4]=> string(2) "61" } } }
i need to get the temperatures out of that with simplexml methods.
i just cant come up with the syntax to access the array indexes of each
element of a given object.
any help is very cool.
thank you,
jim
Re: weather file - accessing xml data
am 24.09.2007 18:15:19 von Steve
"z1" wrote in message
news:46f73d2c$0$19652$4c368faf@roadrunner.com...
> hi -
>
> i am going to try to use regular expressions to get my values.
> i am frustrated that the formart of the file doesnt seem to be
> able to be parsed by simplexml.
>
> thanks anyways,
listen jim, don't try to access the arrays by index. just use a foreach.
however, if you just want to use regex for simplicity and you don't need to
know attributes and such, try these:
==========
hint: segment is a node
would be a 'segment'...a 'single segment'
would be one of many 'segments', or 'multiple segments'
$records = getSingleSegment('records', $xml);
$records = getMultipleSegments('record', $records);
foreach ($records as $record)
{
// you get the idea
}
==========
function getMultipleSegments($segment, $xml)
{
$pattern = "/<(" . $segment . ")>(.*?)<\/\\1>/si";
preg_match_all($pattern, $xml, $matches);
foreach ($matches[2] as $content)
{
$output[] = $content;
}
return $output;
}
function getMultipleSegmentNames($xml)
{
$output = array();
$pattern = "/<([^>]+)>.*?<\/\\1>/si";
preg_match_all($pattern, $xml, $matches);
foreach ($matches[1] as $content)
{
$output[] = $content;
}
return $output;
}
function getSegmentValue($xml, $emptyDefault = '')
{
$pattern = "/<([^>]+)>(.*?)<\/\\1>/si";
preg_match($pattern, $xml, $match);
$value = $match[2];
if ($value == ''){ $value = $emptyDefault; }
return htmlDecode($value);
}
function getSingleSegment($segment, $xml)
{
$output = '';
$pattern = "/<(" . $segment . ")>(.*?)<\/\\1>/si";
preg_match_all($pattern, $xml, $matches);
foreach ($matches[0] as $content)
{
$output .= $content;
}
return $output;
}
function getSingleSegmentName($xml)
{
$output = '';
$pattern = "/<([^>]+)>.*?<\/\\1>/si";
preg_match($pattern, $xml, $match);
return $match[1];
}