title: Parsing Simplexml children of children
body: <p>I've been trying to parse this data for a while now, but I don't really understand simpleXML and how it stores/retrieves.  I have the following xml: </p>
  
  <pre class="lang-xml prettyprint-override"><code>   &lt;ListOrderItemsResult&gt;
      &lt;OrderItems&gt;
        &lt;OrderItem&gt;
          &lt;QuantityOrdered&gt;1&lt;/QuantityOrdered&gt;
          &lt;Title&gt;Organic Chamomile &amp;amp; Lavender Shea Butter CP Soap Making Kit 2 Lbs.&lt;/Title&gt;
          &lt;ItemPrice&gt;
            &lt;CurrencyCode&gt;USD&lt;/CurrencyCode&gt;
            &lt;Amount&gt;29.99&lt;/Amount&gt;
          &lt;/ItemPrice&gt;
          &lt;ItemTax&gt;
            &lt;CurrencyCode&gt;USD&lt;/CurrencyCode&gt;
            &lt;Amount&gt;0.00&lt;/Amount&gt;
          &lt;/ItemTax&gt;
        &lt;/OrderItem&gt;
        &lt;OrderItem&gt;
          &lt;QuantityOrdered&gt;1&lt;/QuantityOrdered&gt;
          &lt;Title&gt;Eucalyptus &amp;amp; Mint Organic Shea Butter CP Soap Making Kit 3 lbs.&lt;/Title&gt;
          &lt;ItemPrice&gt;
            &lt;CurrencyCode&gt;USD&lt;/CurrencyCode&gt;
            &lt;Amount&gt;32.99&lt;/Amount&gt;
          &lt;/ItemPrice&gt;
          &lt;ItemTax&gt;
            &lt;CurrencyCode&gt;USD&lt;/CurrencyCode&gt;
            &lt;Amount&gt;0.00&lt;/Amount&gt;
          &lt;/ItemTax&gt;
          &lt;/ShippingDiscount&gt;
        &lt;/OrderItem&gt;
      &lt;/OrderItems&gt;
      &lt;AmazonOrderId&gt;12134&lt;/AmazonOrderId&gt;
    &lt;/ListOrderItemsResult&gt;
  </code></pre>
  
  <p>I'm trying to convert it into csv with each OrderItem being a row. The columns are: QuantityOrdered, Title, ItemPrice, ItemTax. So far I've managed to grab the everything but ItemPrice and ItemTax, because they have children nodes and I have no idea how to access them. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>    $OrderItem = $xmlDocs-&gt;xpath('//a:OrderItem'); 
  </code></pre>
  
  <p>foreach ($OrderItem as $n) {           </p>
  
  <pre><code>  foreach ($AmazonOrderId as $t) {
      $row[]=$t;
    }
     // Iterate through each child of &lt;OrderItem&gt; node
    $child = $xml-&gt;xpath('//a:OrderItem['.$i.']/*'); 
  
  
  
    foreach ($child as $value) {
      $row[] = $value;
    }
    print_r($row);
  
  
  
     $fs = fopen('141.csv', 'a');
     fputcsv($fs, $row);      
  
     $row = [];
     // Clean out array for next &lt;item&gt; (i.e., row)
     $i++;            // Move to next &lt;item&gt; (i.e., node position)
  </code></pre>
  
  <p>}</p>
  
  <p>fclose($fs);  </p>
  
  <p>I have it working, but it's since I can't figure out how to get to the child of the child, nothing shows up for ItemPrice and ItemTax. </p>
  
  <p>TL;DR - I'm trying to cycle through each OrderItem node and get the values of each child node and if the child node has children, I only want to get the value of the node named 'Amount'. </p>