Redneck
06-09-2008, 05:51 AM
Hello,
I have been using the same code for years now to access my weather's xml feed and parse it into my php pages. But it is not working now as i would like for it to. Below is a sample of the XML i am pulling in to be parsed.
<weather ver="2.0">
<dayf>
<day d="0" t="Tuesday" dt="May 6">
<part p="d">
<icon>11</icon>
<t>Showers</t>
<wind>
<s>14</s>
<t>S</t>
</wind>
</part>
</day>
</dayf>
</weather>
And below is some of the php code that i am using to retrieve the xml and display it on my website. The full version of my code is very large and so i clipped the main code down to this.
$fileforcast = "dayf.xml";
$curl_handle4 = curl_init();
curl_setopt ($curl_handle4, CURLOPT_URL, $fileforcast);
curl_setopt ($curl_handle4, CURLOPT_RETURNTRANSFER, 1);
$dataforcast = curl_exec($curl_handle4);
curl_close($curl_handle4);
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $dataforcast, $values, $indexes);
xml_parser_free($parser);
$currenttemperature = $values[$indexes["t"][0]]["value"];
$currenticon = $values[$indexes["icon"][0]]["value"];
$currentwinddirection = $values[$indexes["t"][1]]["value"];
$currentwindspeed = $values[$indexes["s"][0]]["value"];
echo 'Data is displayed here: ' . $currenttemperature . ' It keeps going and that is it!';
The problem is that if another t is inserted in between the 2 that are already there then the numbering is thrown off. I'd like to find a new way to parse the xml in that i can say i want the current temperature inside the day indexes. It would be nice if they would just re-word their xml feed to be easier to parse but i am sure that's out of the question.
I've tried things like this...
foreach ($indexes['day'] as $day) { if ($values[$day][attributes]['d'] == "2") {
echo '' . $values[$day][children]['d'] . '';
...but it doesn't read the children as a proper function as it does the attributes. Any kind of help is appreciated. Thanks.
I have been using the same code for years now to access my weather's xml feed and parse it into my php pages. But it is not working now as i would like for it to. Below is a sample of the XML i am pulling in to be parsed.
<weather ver="2.0">
<dayf>
<day d="0" t="Tuesday" dt="May 6">
<part p="d">
<icon>11</icon>
<t>Showers</t>
<wind>
<s>14</s>
<t>S</t>
</wind>
</part>
</day>
</dayf>
</weather>
And below is some of the php code that i am using to retrieve the xml and display it on my website. The full version of my code is very large and so i clipped the main code down to this.
$fileforcast = "dayf.xml";
$curl_handle4 = curl_init();
curl_setopt ($curl_handle4, CURLOPT_URL, $fileforcast);
curl_setopt ($curl_handle4, CURLOPT_RETURNTRANSFER, 1);
$dataforcast = curl_exec($curl_handle4);
curl_close($curl_handle4);
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $dataforcast, $values, $indexes);
xml_parser_free($parser);
$currenttemperature = $values[$indexes["t"][0]]["value"];
$currenticon = $values[$indexes["icon"][0]]["value"];
$currentwinddirection = $values[$indexes["t"][1]]["value"];
$currentwindspeed = $values[$indexes["s"][0]]["value"];
echo 'Data is displayed here: ' . $currenttemperature . ' It keeps going and that is it!';
The problem is that if another t is inserted in between the 2 that are already there then the numbering is thrown off. I'd like to find a new way to parse the xml in that i can say i want the current temperature inside the day indexes. It would be nice if they would just re-word their xml feed to be easier to parse but i am sure that's out of the question.
I've tried things like this...
foreach ($indexes['day'] as $day) { if ($values[$day][attributes]['d'] == "2") {
echo '' . $values[$day][children]['d'] . '';
...but it doesn't read the children as a proper function as it does the attributes. Any kind of help is appreciated. Thanks.