gnrmatt Posted May 9, 2011 Share Posted May 9, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/235937-extracting-data-from-particular-attribute-using-simplexml/ Share on other sites More sharing options...
silkfire Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235937-extracting-data-from-particular-attribute-using-simplexml/#findComment-1213031 Share on other sites More sharing options...
gizmola Posted May 10, 2011 Share Posted May 10, 2011 I'd do: foreach($xml->tripDetails->tripDetail as $tripDetail) { $t = $tripDetail.attributes(); echo $t['type']; } Quote Link to comment https://forums.phpfreaks.com/topic/235937-extracting-data-from-particular-attribute-using-simplexml/#findComment-1213214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.