PDA

View Full Version : Children & Attributes from XML


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.

mr e
06-09-2008, 06:43 AM
If you have PHP5 (or can upgrade to it) I would recommend SimpleXML

http://www.php.net/simplexml

simplexml_load_file() (http://www.php.net/manual/en/function.simplexml-load-file.php)

Fou-Lu
06-09-2008, 06:54 AM
I agree, so long as you have version 5+ and it hasn't been disabled.
Personally, I tend to use the DOM, but the simpleXML is great for two reasons: its simple, and can be converted to DOM if you need more complex manipulations.
Both support XPath which is perfect for extracting data.
So long as the xml file is publicly available, you won't need to curl it either which is great.


Another option may be available. More and more places are creating webservices - you may be more comfortable with creating a soap client if you have the service available.

Redneck
06-11-2008, 04:18 AM
Unfortunately i have to use curl because of my web host but i was able to upgrade to php 5 and learn simple xml in the last couple of days and figure out a way to incorporate curl into it. I have exactly what i was looking for now. You are right about simple xml, it is a lot easier to use and manipulate. Thanks for your help.

Fou-Lu
06-11-2008, 04:25 AM
Glad to hear you got it working mate! Too bad on the CURL, I hate using it >.<