title: xml parsing in php - getting specific values
body: <p>New to parsing XML and XML in general... I am working off an example, but there are a few things I cannot figure out :</p>
  
  <p>I am currently using something such as this for values :</p>
  
  <pre><code>$order-&gt;reference = $doc-&gt;getElementsByTagName("reference")-&gt;item(0)-&gt;nodeValue;
  </code></pre>
  
  <p>which I realize is getting the value of the 1st occurrence of x and it works fine.</p>
  
  <p>Now, since <code>customer</code> and <code>purchaser</code> have the same values within them how can I target one or the other?  There will only be a max of one instance for each.</p>
  
  <p>Also, I expect multiple <code>&lt;orderItem&gt;</code> within <code>&lt;orderItems&gt;</code>... how can I iterate through these... preferably creating something like an array as I will have no idea how many there will be.</p>
  
  <p><strong>example xml :</strong></p>
  
  <pre><code>&lt;order&gt;
    &lt;reference/&gt;
    &lt;status&gt;open | request | requested | acceptance | accepted | fulfillment | fulfilled | completion | completed | canceled | failed&lt;/status&gt;
    &lt;statusChanged&gt;2010-08-15T00:00:00.000Z&lt;/statusChanged&gt;
    &lt;test&gt;true | false&lt;/test&gt;
    &lt;due&gt;2010-08-15T00:00:00.000Z&lt;/due&gt;
    &lt;returnStatus&gt;none | partial | full&lt;/returnStatus&gt;
    &lt;currency/&gt;
    &lt;referrer/&gt;
    &lt;originIp/&gt;
    &lt;total&gt;0.0&lt;/total&gt;
    &lt;tax&gt;0.0&lt;/tax&gt;
    &lt;shipping&gt;0.0&lt;/shipping&gt;
    &lt;sourceName/&gt;
    &lt;sourceKey/&gt;
    &lt;sourceCampaign/&gt;
    &lt;customer&gt;
      &lt;firstName/&gt;
      &lt;lastName/&gt;
      &lt;company/&gt;
      &lt;email/&gt;
      &lt;phoneNumber/&gt;
    &lt;/customer&gt;
    &lt;purchaser&gt;
      &lt;firstName/&gt;
      &lt;lastName/&gt;
      &lt;company/&gt;
      &lt;email/&gt;
      &lt;phoneNumber/&gt;
    &lt;/purchaser&gt;
    &lt;address&gt;
      &lt;addressLine1/&gt;
      &lt;addressLine2/&gt;
      &lt;city/&gt;
      &lt;region/&gt;
      &lt;regionCustom/&gt;
      &lt;postalCode/&gt;
      &lt;country/&gt;
    &lt;/address&gt;
    &lt;orderItems&gt;
      &lt;orderItem&gt;
        &lt;productDisplay/&gt;
        &lt;productName/&gt;
        &lt;quantity&gt;0&lt;/quantity&gt;
        &lt;subscriptionReference/&gt;
      &lt;/orderItem&gt;
    &lt;/orderItems&gt;
    &lt;payments&gt;
      &lt;payment&gt;
        &lt;status&gt;open | request | requested | acceptance | accepted | fulfillment | fulfilled | completion | completed | canceled | failed&lt;/status&gt;
        &lt;statusChanged&gt;2010-08-15T00:00:00.000Z&lt;/statusChanged&gt;
        &lt;methodType&gt;paypal | creditcard | test | bank | check | purchase-order | free&lt;/methodType&gt;
        &lt;declinedReason&gt;internal-error | unsupported-country | expired-card | declined | risk | processor-risk | connection | unknown | cc-address-verification | cc-cvv | voice-auth&lt;/declinedReason&gt;
        &lt;currency/&gt;
        &lt;total&gt;0.0&lt;/total&gt;
      &lt;/payment&gt;
    &lt;/payments&gt;
  &lt;/order&gt;
  </code></pre>
  
  <p><strong>EDIT :</strong></p>
  
  <p>Getting no errors yet no values for customer / purchaser.</p>
  
  <pre><code>private function parseFsprgOrder($doc) {
      //show values coming in
      echo '&lt;pre&gt;',print_r($doc, 1),'&lt;/pre&gt;';
  
      $order = new FsprgOrder();
  
      $order-&gt;reference = $doc-&gt;getElementsByTagName("reference")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;status = $doc-&gt;getElementsByTagName("status")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;statusChanged = strtotime($doc-&gt;getElementsByTagName("statusChanged")-&gt;item(0)-&gt;nodeValue);
      $order-&gt;test = $doc-&gt;getElementsByTagName("test")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;due = strtotime($doc-&gt;getElementsByTagName("due")-&gt;item(0)-&gt;nodeValue);
      $order-&gt;returnStatus = $doc-&gt;getElementsByTagName("returnStatus")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;currency = $doc-&gt;getElementsByTagName("currency")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;referrer = $doc-&gt;getElementsByTagName("referrer")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;originIp = $doc-&gt;getElementsByTagName("originIp")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;total = $doc-&gt;getElementsByTagName("total")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;tax = $doc-&gt;getElementsByTagName("tax")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;shipping = $doc-&gt;getElementsByTagName("shipping")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;sourceName = $doc-&gt;getElementsByTagName("sourceName")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;sourceKey = $doc-&gt;getElementsByTagName("sourceKey")-&gt;item(0)-&gt;nodeValue;
      $order-&gt;sourceCampaign = $doc-&gt;getElementsByTagName("sourceCampaign")-&gt;item(0)-&gt;nodeValue;
  
      //customer info 
      $customerNodes = $doc-&gt;getElementsByTagName("customer")-&gt;item(0);
  
      if($customerNodes-&gt;length &gt; 0)
      {
          $customer = new FsprgCustomer();
  
          $customer-&gt;firstName = $customerNodes-&gt;getElementsByTagName("firstName")-&gt;item(0)-&gt;nodeValue;
          $customer-&gt;lastName = $customerNodes-&gt;getElementsByTagName("lastName")-&gt;item(0)-&gt;nodeValue;
          $customer-&gt;company = $customerNodes-&gt;getElementsByTagName("company")-&gt;item(0)-&gt;nodeValue;
          $customer-&gt;email = $customerNodes-&gt;getElementsByTagName("email")-&gt;item(0)-&gt;nodeValue;
          $customer-&gt;phoneNumber = $customerNodes-&gt;getElementsByTagName("phoneNumber")-&gt;item(0)-&gt;nodeValue;
  
          $order-&gt;customer = $customer;
      }
  
      //purchaser info
      $purchaserNodes = $doc-&gt;getElementsByTagName("purchaser")-&gt;item(0);
  
      if($purchaserNodes-&gt;length &gt; 0)
      {
          $purchaser = new FsprgPurchaser();
  
          $purchaser-&gt;firstName = $purchaserNodes-&gt;getElementsByTagName("firstName")-&gt;item(0)-&gt;nodeValue;
          $purchaser-&gt;lastName = $purchaserNodes-&gt;getElementsByTagName("lastName")-&gt;item(0)-&gt;nodeValue;
          $purchaser-&gt;company = $purchaserNodes-&gt;getElementsByTagName("company")-&gt;item(0)-&gt;nodeValue;
          $purchaser-&gt;email = $purchaserNodes-&gt;getElementsByTagName("email")-&gt;item(0)-&gt;nodeValue;
          $purchaser-&gt;phoneNumber = $purchaserNodes-&gt;getElementsByTagName("phoneNumber")-&gt;item(0)-&gt;nodeValue;
  
          $order-&gt;purchaser = $purchaser;
      }
  
      return $order;
  }
  
  class FsprgCustomer {
      public $firstName;
      public $lastName;
      public $company;
      public $email;
      public $phoneNumber;
  }
  
  class FsprgPurchaser {
      public $firstName;
      public $lastName;
      public $company;
      public $email;
      public $phoneNumber;
  }
  
  class FsprgOrder {
      public $reference;
      public $status;
      public $statusChanged;
      public $test;
      public $due;
      public $returnStatus;
      public $currency;
      public $referrer;
      public $originIp;
      public $total;
      public $tax;
      public $shipping;
      public $sourceName;
      public $sourceKey;
      public $sourceCampaign;
  }
  </code></pre>