0

I'm trying to output data from an XML file.

Here's an excerpt from the XML file:

<TAF>
<raw_text>
TAF AMD EGBB 242124Z 2421/2518 11006KT 6000 BKN020 TEMPO 2421/2424 4000 +SHRA TSRA BECMG 2421/2424 BKN009 PROB30 2500/2506 4000 BR BKN004 BECMG 2506/2508 9999 SCT020 TEMPO 2508/2518 7000 SHRA PROB30 TEMPO 2508/2518 33015G25KT 4000 +TSRA BECMG 2515/2518 35010KT
</raw_text>
<station_id>EGBB</station_id>
<issue_time>2012-08-24T21:24:00Z</issue_time>
<valid_time_from>2012-08-24T21:00:00Z</valid_time_from>
<valid_time_to>2012-08-25T18:00:00Z</valid_time_to>

<forecast>
    <fcst_time_from>2012-08-24T21:00:00Z</fcst_time_from>
    <fcst_time_to>2012-08-25T18:00:00Z</fcst_time_to>
    <wind_dir_degrees>110</wind_dir_degrees>
    <wind_speed_kt>6</wind_speed_kt>
    <visibility_statute_mi>3.73</visibility_statute_mi>
    <sky_condition sky_cover="BKN" cloud_base_ft_agl="2000"/>
</forecast>
<forecast>
    <fcst_time_from>2012-08-24T21:00:00Z</fcst_time_from>
    <fcst_time_to>2012-08-25T00:00:00Z</fcst_time_to>
    <change_indicator>TEMPO</change_indicator>
    <visibility_statute_mi>2.49</visibility_statute_mi>
    <wx_string>+SHRA TSRA</wx_string>
</forecast>
</TAF>

Maybe there's a few more <forecast>. My question is how do I fill them out into a foreach loop to output data for each <forecast> element under <TAF>?

I am currently using:

$xml_taf->data->TAF[0]->forecast

to access the data inside the XML with SimpleXML.

2
  • 1
    As in... foreach($xml_taf->data->TAF[0]->forecast as $forecast)? Commented Aug 24, 2012 at 23:59
  • 1
    You showed us what you're currently using, but does it work? If not, please show the whole XML structure (i.e. why you tried $xml_tag->data->…) as the problem isn't in the fragment that you gave above. Commented Aug 25, 2012 at 0:05

1 Answer 1

1

Am not sure what you are doing but this works

$xml = '<TAF>
<raw_text>
TAF AMD EGBB 242124Z 2421/2518 11006KT 6000 BKN020 TEMPO 2421/2424 4000 +SHRA TSRA BECMG 2421/2424 BKN009 PROB30 2500/2506 4000 BR BKN004 BECMG 2506/2508 9999 SCT020 TEMPO 2508/2518 7000 SHRA PROB30 TEMPO 2508/2518 33015G25KT 4000 +TSRA BECMG 2515/2518 35010KT
</raw_text>
<station_id>EGBB</station_id>
<issue_time>2012-08-24T21:24:00Z</issue_time>
<valid_time_from>2012-08-24T21:00:00Z</valid_time_from>
<valid_time_to>2012-08-25T18:00:00Z</valid_time_to>

<forecast>
    <fcst_time_from>2012-08-24T21:00:00Z</fcst_time_from>
    <fcst_time_to>2012-08-25T18:00:00Z</fcst_time_to>
    <wind_dir_degrees>110</wind_dir_degrees>
    <wind_speed_kt>6</wind_speed_kt>
    <visibility_statute_mi>3.73</visibility_statute_mi>
    <sky_condition sky_cover="BKN" cloud_base_ft_agl="2000"/>
</forecast>
<forecast>
    <fcst_time_from>2012-08-24T21:00:00Z</fcst_time_from>
    <fcst_time_to>2012-08-25T00:00:00Z</fcst_time_to>
    <change_indicator>TEMPO</change_indicator>
    <visibility_statute_mi>2.49</visibility_statute_mi>
    <wx_string>+SHRA TSRA</wx_string>
</forecast>
</TAF>';

echo "<pre>";
$xml = new SimpleXMLElement($xml);
foreach ( $xml->forecast as $forecast ) {
    print( $forecast->fcst_time_from . ","  . $forecast->fcst_time_to . "," . $forecast->visibility_statute_mi . PHP_EOL);
}

Output

2012-08-24T21:00:00Z,2012-08-25T18:00:00Z,3.73
2012-08-24T21:00:00Z,2012-08-25T00:00:00Z,2.49

See Live Demo

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

Comments

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.