title: PHP simpleXML trying to process fairly complex file
body: <p>The file I have to work with has the following structure:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;FormattedReport xmlns = 'urn:crystal-reports:schemas' xmlns:xsi = 'http://www.w3.org/2000/10/XMLSchema-instance'&gt;
      &lt;FormattedAreaPair Level="0" Type="Report"&gt;
      &lt;FormattedAreaPair Level="1" Type="Details"&gt;
      &lt;FormattedArea Type="Details"&gt;
          &lt;FormattedSections&gt;
          &lt;FormattedSection SectionNumber="0"&gt;
          &lt;FormattedReportObjects&gt;
          FormattedReportObject xsi:type="CTFormattedField" Type="xsd:long" FieldName="{tblCon.ManifestNR}"&gt;&lt;ObjectName&gt;ManifestNR1&lt;/ObjectName&gt;
          &lt;FormattedValue&gt;1,907&lt;/FormattedValue&gt;
          &lt;Value&gt;1907.00&lt;/Value&gt;
          &lt;/FormattedReportObject&gt;
          &lt;FormattedReportObject xsi:type="CTFormattedField" Type="xsd:timeInstant" FieldName="{tblCon.ShippingDate}"&gt;&lt;ObjectName&gt;ShippingDate1&lt;/ObjectName&gt;
          &lt;FormattedValue&gt;14/04/2011&lt;/FormattedValue&gt;
          &lt;Value&gt;2011-04-14T00:00:00&lt;/Value&gt;
          &lt;/FormattedReportObject&gt;
          ... so on and so forth ...
          &lt;/FormattedReportObjects&gt;
          &lt;/FormattedSection&gt;
          &lt;/FormattedSections&gt;
          &lt;/FormattedArea&gt;
          &lt;/FormattedAreaPair&gt;
      &lt;FormattedReportObjects&gt;
          FormattedReportObject xsi:type="CTFormattedField" Type="xsd:long" FieldName="{tblCon.ManifestNR}"&gt;&lt;ObjectName&gt;ManifestNR1&lt;/ObjectName&gt;
          &lt;FormattedValue&gt;1,907&lt;/FormattedValue&gt;
          &lt;Value&gt;1907.00&lt;/Value&gt;
          &lt;/FormattedReportObject&gt;
          &lt;FormattedReportObject xsi:type="CTFormattedField" Type="xsd:timeInstant" FieldName="{tblCon.ShippingDate}"&gt;&lt;ObjectName&gt;ShippingDate1&lt;/ObjectName&gt;
          &lt;FormattedValue&gt;14/04/2011&lt;/FormattedValue&gt;
          &lt;Value&gt;2011-04-14T00:00:00&lt;/Value&gt;
          &lt;/FormattedReportObject&gt;
          ... so on and so forth ...
          &lt;/FormattedReportObjects&gt;
          &lt;/FormattedSection&gt;
          &lt;/FormattedSections&gt;
          &lt;/FormattedArea&gt;
          &lt;/FormattedAreaPair&gt;
   &lt;FormattedAreaPair Level="1" Type="Details"&gt;
      &lt;FormattedArea Type="Details"&gt;
          &lt;FormattedSections&gt;
          &lt;FormattedSection SectionNumber="0"&gt;
          &lt;FormattedReportObjects&gt;
          FormattedReportObject xsi:type="CTFormattedField" Type="xsd:long" FieldName="{tblCon.ManifestNR}"&gt;&lt;ObjectName&gt;ManifestNR1&lt;/ObjectName&gt;
          &lt;FormattedValue&gt;1,907&lt;/FormattedValue&gt;
          &lt;Value&gt;1907.00&lt;/Value&gt;
          &lt;/FormattedReportObject&gt;
          &lt;FormattedReportObject xsi:type="CTFormattedField" Type="xsd:timeInstant" FieldName="{tblCon.ShippingDate}"&gt;&lt;ObjectName&gt;ShippingDate1&lt;/ObjectName&gt;
          &lt;FormattedValue&gt;14/04/2011&lt;/FormattedValue&gt;
          &lt;Value&gt;2011-04-14T00:00:00&lt;/Value&gt;
          &lt;/FormattedReportObject&gt;
          ... so on and so forth ...
          &lt;/FormattedReportObjects&gt;
          &lt;/FormattedSection&gt;
          &lt;/FormattedSections&gt;
          &lt;/FormattedArea&gt;
          &lt;/FormattedAreaPair&gt;
      &lt;FormattedReportObjects&gt;
          FormattedReportObject xsi:type="CTFormattedField" Type="xsd:long" FieldName="{tblCon.ManifestNR}"&gt;&lt;ObjectName&gt;ManifestNR1&lt;/ObjectName&gt;
          &lt;FormattedValue&gt;1,907&lt;/FormattedValue&gt;
          &lt;Value&gt;1907.00&lt;/Value&gt;
          &lt;/FormattedReportObject&gt;
          &lt;FormattedReportObject xsi:type="CTFormattedField" Type="xsd:timeInstant" FieldName="{tblCon.ShippingDate}"&gt;&lt;ObjectName&gt;ShippingDate1&lt;/ObjectName&gt;
          &lt;FormattedValue&gt;14/04/2011&lt;/FormattedValue&gt;
          &lt;Value&gt;2011-04-14T00:00:00&lt;/Value&gt;
          &lt;/FormattedReportObject&gt;
          ... so on and so forth ...
          &lt;/FormattedReportObjects&gt;
          &lt;/FormattedSection&gt;
          &lt;/FormattedSections&gt;
          &lt;/FormattedArea&gt;
          &lt;/FormattedAreaPair&gt;
          &lt;/FormattedAreaPair&gt;
          &lt;/FormattedReport&gt;
  </code></pre>
  
  <p>So what I'm trying to do, is call a PHP function which will parse the XML and eventually store it in an SQL DB. </p>
  
  <p>for example:</p>
  
  <p>ManifestNR: 1903
  ShippingDate: 12/04/2011
  CarrierID: TNT03
  TrackingRef: 234234232
  ... etc for each record ...</p>
  
  <p>so i've set about trying to do this using DOM and then stumbled across simpleXML, I've read several tuts, and searched implementations here but I just can't seem to access the data in the final nodes (or any other data tbh). Is simpleXML a no-no with these kind of structures?</p>
  
  <p>The latest PHP I'm using is:</p>
  
  <pre><code>&lt;?php
  
  if (file_exists('tracking.xml')) {
      $xml = simplexml_load_file('tracking.xml');
  
    //  print_r($xml);
  
     foreach( $xml as $FormattedReport-&gt;FormattedAreaPair-&gt;FormattedAreaPair ) 
          {
          foreach($FormattedReport as $node-&gt;FormattedArea-&gt;FormattedSections-&gt;FormattedSection-&gt;FormattedReportObjects)
          echo $node-&gt;FormattedReportObject-&gt;Value;
          }
  
  } else {
      exit('Failed to open xml');
  }
  ?&gt;
  </code></pre>
  
  <p>I've tried to strip it right back to basics, but still no luck. Doesn't echo a result.</p>
  
  <p>Thanks for your time guys!</p>
  
  <p><strong>SOLVED</strong></p>
  
  <p>Anyone in similar circumstances heres a bit of direction.</p>
  
  <ol>
  <li>ignore the root node, thats your default $variable when you import the XML string/file</li>
  <li>If you have nested groups create a node to the parent first like so  $xml->FormattedAreaPair->FormattedAreaPair as $parentnode </li>
  <li>Using your parent node loop through all the children</li>
  <li>If you have an attribute field access it as follows: (string) $node['FieldName'])</li>
  <li>Compare the retrieved attribute with a string and then handle the result.</li>
  <li><p>Stop pulling your hair out.</p>
  
  <code>//print_r($xml); 
  foreach( $xml-&gt;FormattedAreaPair-&gt;FormattedAreaPair as $parentnode ) { 
      foreach($parentnode-&gt;FormattedArea-&gt;FormattedSections-&gt;FormattedSection-&gt;FormattedReportObjects-&gt;FormattedReportObject as $node){ 
          //echo "FormattedValue: ".$node-&gt;FormattedValue."&lt;br /&gt;"; 
          switch((string) $node['FieldName']){ 
              case '{tblCon.ManifestNR}': 
                  echo 'Manifest: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblCon.ShippingDate}': 
                  echo 'Shipping Date: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblCon.CarrierID}': 
                  echo 'Carrier ID: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblCon.CustConRefTX}': 
                  echo 'Customer Reference: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblCon.ServiceCodeTX}': 
                  echo 'Service Code: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblCon.TotalWeightNR}': 
                  echo 'Total Weight: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblCon.ValueNR}': 
                  echo 'Value: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblCon.TotalVolumeNR}': 
                  echo 'Total Volume: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblCon.GoodsDesc}': 
                  echo 'Goods Description: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{tblConAddr.ReceiverNameTX}': 
                  echo 'Receiver Name: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{@SalesOrder}': 
                  echo 'Sales Order: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break; 
              case '{@TrackingReference}': 
                  echo 'Tracking Reference: '.$node-&gt;FormattedValue."&lt;br /&gt;"; 
                  break;
          }
      }
  echo "---------------------------- &lt;br /&gt;";
  }
  </code>
  
  } 
  else { 
      exit('Failed to open xml'); 
  }
  
  ?></li>
  </ol>