More reference: http://www.sitepoint.com/parsing-xml-with-simplexml/
Given the XML:
<Calendar> <Account> <Name>Sample Account Name</Name> <Client>Test User</Client> </Account> <CalendarDates> <CalendarDate> <Date>2014-03-01</Date> <Events> <Event> <Name>Event Number One</Name> <StartingAt>2014-03-01T09:30:00</StartingAt> <Categories>General</Categories> </Event> <Event> <Name>Another Event</Name> <StartingAt>2014-03-01T09:30:00</StartingAt> <Categories>Meeting</Categories> </Event> <Event> <Name>Hockey Game</Name> <StartingAt>2014-03-01T10:00:00</StartingAt> <Categories>Sports</Categories> </Event> </Events> </CalendarDate> </CalendarDates> </Calendar>
Perhaps this was entered into a textarea on a form and we have read it it into a variable. We can access the various data nodes using SimpleXML:
$oXML = new SimpleXMLElement($_POST['textarea_name']); // comes from our fictitious form // We obtain the general account info (Name, Client) using $AccountNode = $oXML->Account; // This accesses the Calendar Dates Node. $DatesNode = $oXML->CalendarDates; echo '<ul>'; foreach($DatesNode->CalendarDate as $date){ foreach($EventsNode->Event as $event){ echo '<li>'; echo date("g:i A",strtotime($event->StartingAt)) . "<br>"; echo $event->Name . "<br>"; echo $even->Categories; echo '</li>'; } } echo '</ul>';
But what if we have to get the XML from somewhere else?
$durl = 'http://somelink.com/calendars/calendarname.xml'; $sXML = download_page($durl); $oXML = new SimpleXMLElement($sXML);