Jump to content

Extracting data from particular attribute using simplexml


gnrmatt

Recommended Posts

Hi,

 

I have an XML Feed which is using ID's or Types. I need to select the specific node and convert it to a variable.

 

Example 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>
</tripDetails>

 

I have been extracting this data by using:

 

<?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)
?>

 

However I am unsure how I can extract the "ex lima" from StartFinish as $startfinish.

 

Can anyone help?

 

Many thanks!

 

Link to comment
Share on other sites

SimpleXML is truly simple:

 

$xml->tripDetails->tripDetail[0];

 

You don't extract the contents by supplying attribute, you supply the order it is as a child to tripDetails, in this case zero-based first.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.