Extracting data from particular attribute using simplexml

Hi,

I have the following xml feed:

<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff </tripDetail>
<tripDetail type="Whats not included">Not much </tripDetail>
</tripDetails>

I need a way to be able to save each of the tripDetails as a seperate value. The “types” are always constant and i have a list of them.

I therefore want to be able to say $startfinish = ‘ex Lima’, $whatsincluded = ‘Lots of stuff’ etc.

The code i have so far is:

[php]

<?php if(!$xml=simplexml_load_file('xmlfeed.xml')){ trigger_error('Error reading XML file',E_USER_ERROR); } foreach ($xml->tourName as $tourname) foreach ($xml->dossierCode as $agentcode) ?>

[/php]
which obviously get’s me the first two instances of the XML. When i move onto the tripDetail section though, i tried to use:

[php]
foreach ($xml->tripDetails->tripDetail[0] as $startfinish)[/php]
this didn’t produce anything. If i remove the [ 0 ], it just displays the last result in the array.

I have been told how to get all the data via var_dump but this isn’t what i need. I need to be able to have $startfinish as ‘ex lima’ etc.

Any help - greatly appreciated!

Many thanks!

All good fun working with SimpleXML :smiley:

Try using the following:

foreach ($xml->tripDetails->tripDetail as $detail) {
switch ($detail[‘type’]) {
case ‘Whats not included’:
$whatsnotincluded = $detail;
break;
case ‘What’s Included’:
$whatsincluded = $detail;
break;
case ‘StartFinish’:
$startfinish = $detail;
break;
}
}

If the type attributes did not contain hyphens and spaces you could use variable variables :wink:

foreach ($xml->tripDetails->tripDetail as $detail) {
switch $detail[‘type’] = $detail;
}

Ant

Sponsor our Newsletter | Privacy Policy | Terms of Service