title: Import XML in PHP to add to Mysql DB
body: <p>I've been trying to crack this one and just haven't been able to.</p>
  
  <p>Basically I have an XML file that contains train line timetable information, and it is really complex.</p>
  
  <p>I need to capture every bit of information and add to a MySQL DB, but as you'll see the parent/child and key/pair is quite dynamic and sometimes it has more or less stuff.</p>
  
  <p>I have been looking a SimpleXML, but cannot get the values out of the XML, in a way it is flexible to the change in information/data.</p>
  
  <p>Please note that from the 1st Journey to the second, there new fields, which as you probably guessed, it needs to captured.</p>
  
  <p>How can get the elements inside the XLM below:</p>
  
  <p></p>
  
  <p>And also for each of the elements like:</p>
  
  <p></p>
  
  <p>The OR is really important to capture as also the other values.</p>
  
  <p>Many thanks</p>
  
  <p>Lucio</p>
  
  <p><strong>Experimental PHP code:</strong></p>
  
  <pre><code>&lt;?php
  $xml = simplexml_load_file($argv[1]) or die("Error: Cannot create object");
  //
  //  TimeTableID
  //
  $timetableId = $xml-&gt;attributes()-&gt;timetableID;
  echo "Timetable id: " . $timetableId . "\n\r";
  //
  // Journey
  // I've added the atributes-&gt;
  foreach ($xml-&gt;Journey as $Journey) {
      echo "Journey data: " . $Journey["rid"] . "\n\r";
      echo "Journey data: " . $Journey["uid"] . "\n\r";
      echo "Journey data: " . $Journey["trainId"] . "\n\r";
      echo "Journey data: " . $Journey["ssd"] . "\n\r";
      echo "Journey data: " . $Journey["toc"] . "\n\r";
  }
  ?&gt;
  </code></pre>
  
  <p><strong>XML file extract:</strong></p>
  
  <pre><code>&lt;PportTimetable timetableID="20180124020740"&gt;
  &lt;Journey rid="201801247176474" uid="G76474" trainId="2H98" ssd="2018-01-24" toc="SN"&gt;     
      &lt;OR tpl="BCKNMJC" act="TB" ptd="07:15" wtd="07:15" /&gt;
      &lt;IP tpl="BIRKBCK" act="T " pta="07:18" ptd="07:18" wta="07:17:30" wtd="07:18" /&gt;
      &lt;PP tpl="CRYSBRJ" wtp="07:20" /&gt;
      &lt;IP tpl="CRYSTLP" act="T " plat="1" pta="07:22" ptd="07:22" wta="07:21:30" wtd="07:22" /&gt;
      &lt;IP tpl="GIPSYH" act="T " pta="07:25" ptd="07:25" wta="07:24:30" wtd="07:25" /&gt;
      &lt;IP tpl="WNORWOD" act="T " plat="1" pta="07:28" ptd="07:28" wta="07:27:30" wtd="07:28" /&gt;
      &lt;PP tpl="WNORWDJ" wtp="07:29" /&gt;
      &lt;IP tpl="TULSEH" act="T " plat="3" pta="07:31" ptd="07:34" wta="07:31" wtd="07:34" /&gt;
      &lt;IP tpl="NDULWCH" act="T " pta="07:37" ptd="07:37" wta="07:36:30" wtd="07:37:30" /&gt;
      &lt;IP tpl="EDULWCH" act="T " pta="07:39" ptd="07:40" wta="07:39" wtd="07:40" /&gt;
      &lt;IP tpl="PKHMRYC" act="T " plat="2" pta="07:42" ptd="07:43" wta="07:42" wtd="07:43" /&gt;
      &lt;IP tpl="PCKHMQD" act="T " plat="1" pta="07:45" ptd="07:45" wta="07:45" wtd="07:45:30" /&gt;
      &lt;PP tpl="OLDKRDJ" wtp="07:46" /&gt;
      &lt;DT tpl="SBRMNDS" act="TF" pta="07:52" wta="07:52" /&gt;
    &lt;/Journey&gt;
  
    &lt;Journey rid="201801248980806" uid="Y80806" trainId="5G45" ssd="2018-01-24" toc="VT" trainCat="EE" isPassengerSvc="false"&gt;
      &lt;OPOR tpl="WVRMPTN" act="TB" plat="1" wtd="22:40" /&gt;
      &lt;PP tpl="WVRMTNJ" wtp="22:41:30" /&gt;
      &lt;PP tpl="WVRMSRJ" wtp="22:44" /&gt;
      &lt;OPDT tpl="OXLEYCS" act="TF" wta="22:47" /&gt;
    &lt;/Journey&gt;
  &lt;/PportTimetable&gt;
  </code></pre>