2

New to parsing XML and XML in general... I am working off an example, but there are a few things I cannot figure out :

I am currently using something such as this for values :

$order->reference = $doc->getElementsByTagName("reference")->item(0)->nodeValue;

which I realize is getting the value of the 1st occurrence of x and it works fine.

Now, since customer and purchaser 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.

Also, I expect multiple <orderItem> within <orderItems>... how can I iterate through these... preferably creating something like an array as I will have no idea how many there will be.

example xml :

<order>
  <reference/>
  <status>open | request | requested | acceptance | accepted | fulfillment | fulfilled | completion | completed | canceled | failed</status>
  <statusChanged>2010-08-15T00:00:00.000Z</statusChanged>
  <test>true | false</test>
  <due>2010-08-15T00:00:00.000Z</due>
  <returnStatus>none | partial | full</returnStatus>
  <currency/>
  <referrer/>
  <originIp/>
  <total>0.0</total>
  <tax>0.0</tax>
  <shipping>0.0</shipping>
  <sourceName/>
  <sourceKey/>
  <sourceCampaign/>
  <customer>
    <firstName/>
    <lastName/>
    <company/>
    <email/>
    <phoneNumber/>
  </customer>
  <purchaser>
    <firstName/>
    <lastName/>
    <company/>
    <email/>
    <phoneNumber/>
  </purchaser>
  <address>
    <addressLine1/>
    <addressLine2/>
    <city/>
    <region/>
    <regionCustom/>
    <postalCode/>
    <country/>
  </address>
  <orderItems>
    <orderItem>
      <productDisplay/>
      <productName/>
      <quantity>0</quantity>
      <subscriptionReference/>
    </orderItem>
  </orderItems>
  <payments>
    <payment>
      <status>open | request | requested | acceptance | accepted | fulfillment | fulfilled | completion | completed | canceled | failed</status>
      <statusChanged>2010-08-15T00:00:00.000Z</statusChanged>
      <methodType>paypal | creditcard | test | bank | check | purchase-order | free</methodType>
      <declinedReason>internal-error | unsupported-country | expired-card | declined | risk | processor-risk | connection | unknown | cc-address-verification | cc-cvv | voice-auth</declinedReason>
      <currency/>
      <total>0.0</total>
    </payment>
  </payments>
</order>

EDIT :

Getting no errors yet no values for customer / purchaser.

private function parseFsprgOrder($doc) {
    //show values coming in
    echo '<pre>',print_r($doc, 1),'</pre>';

    $order = new FsprgOrder();

    $order->reference = $doc->getElementsByTagName("reference")->item(0)->nodeValue;
    $order->status = $doc->getElementsByTagName("status")->item(0)->nodeValue;
    $order->statusChanged = strtotime($doc->getElementsByTagName("statusChanged")->item(0)->nodeValue);
    $order->test = $doc->getElementsByTagName("test")->item(0)->nodeValue;
    $order->due = strtotime($doc->getElementsByTagName("due")->item(0)->nodeValue);
    $order->returnStatus = $doc->getElementsByTagName("returnStatus")->item(0)->nodeValue;
    $order->currency = $doc->getElementsByTagName("currency")->item(0)->nodeValue;
    $order->referrer = $doc->getElementsByTagName("referrer")->item(0)->nodeValue;
    $order->originIp = $doc->getElementsByTagName("originIp")->item(0)->nodeValue;
    $order->total = $doc->getElementsByTagName("total")->item(0)->nodeValue;
    $order->tax = $doc->getElementsByTagName("tax")->item(0)->nodeValue;
    $order->shipping = $doc->getElementsByTagName("shipping")->item(0)->nodeValue;
    $order->sourceName = $doc->getElementsByTagName("sourceName")->item(0)->nodeValue;
    $order->sourceKey = $doc->getElementsByTagName("sourceKey")->item(0)->nodeValue;
    $order->sourceCampaign = $doc->getElementsByTagName("sourceCampaign")->item(0)->nodeValue;

    //customer info 
    $customerNodes = $doc->getElementsByTagName("customer")->item(0);

    if($customerNodes->length > 0)
    {
        $customer = new FsprgCustomer();

        $customer->firstName = $customerNodes->getElementsByTagName("firstName")->item(0)->nodeValue;
        $customer->lastName = $customerNodes->getElementsByTagName("lastName")->item(0)->nodeValue;
        $customer->company = $customerNodes->getElementsByTagName("company")->item(0)->nodeValue;
        $customer->email = $customerNodes->getElementsByTagName("email")->item(0)->nodeValue;
        $customer->phoneNumber = $customerNodes->getElementsByTagName("phoneNumber")->item(0)->nodeValue;

        $order->customer = $customer;
    }

    //purchaser info
    $purchaserNodes = $doc->getElementsByTagName("purchaser")->item(0);

    if($purchaserNodes->length > 0)
    {
        $purchaser = new FsprgPurchaser();

        $purchaser->firstName = $purchaserNodes->getElementsByTagName("firstName")->item(0)->nodeValue;
        $purchaser->lastName = $purchaserNodes->getElementsByTagName("lastName")->item(0)->nodeValue;
        $purchaser->company = $purchaserNodes->getElementsByTagName("company")->item(0)->nodeValue;
        $purchaser->email = $purchaserNodes->getElementsByTagName("email")->item(0)->nodeValue;
        $purchaser->phoneNumber = $purchaserNodes->getElementsByTagName("phoneNumber")->item(0)->nodeValue;

        $order->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;
}
2
  • what values you want to get from customer, purchaser and orderitems? Your answer lies in your question. Commented Aug 27, 2014 at 18:07
  • No kidding... I know what I want to get... I don't realize how to target them. I looked at all sort of tutorials, but everything was using simplexml and I am going to stick with the examples provided to me. Commented Aug 27, 2014 at 18:10

3 Answers 3

2
$customerNodes=order->getElemensByTagName('customer');
if($customerNodes->length>0)
    $customer=$customerNodes->item(0);
$purchaseNodes=order->getElemensByTagName('purchase');
if($purchaseNodes->length>0)
    $purchase=$purchaseNodes->item(0);
$orderItemNodes=order->getElemensByTagName('orderItem');
if($orderItemNodes->length>0)
    foreach($orderItemNodes as $orderItemNode){
        //process $orderItemNode    
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Trying to incorporate this, but getting an error... I see the logic you are going for though. Storing 'customer' as its own, if there is anything, then do the same process as I was just within 'customer' only.
You can get customer, purchase, orderitem from this. next you can access attributes using nodeValue.
I add my own solution below which works for me - had to make a few changes with yours to get it to work. Thanks
0

Check DOMXPath::evaluate(). This allows you to fetch values from a DOM using XPath expressions.

Fetching a list of nodes:

$xpath - new DOMXPath($domDocument);
$nodeList = $xpath->evaluate('/order');

Or a scalar value:

$xpath - new DOMXPath($domDocument);
$string = $xpath->evaluate('string(/order/customer/firstName)');

2 Comments

I'd like to stick to the same format it is in, but if I can't figure this out will come back to it. Thanks.
This is part of the dom extension. Think of it as a powerful version of getElementsByTagName().
0
//customer info 
$customerNodes = $doc->getElementsByTagName("customer");

if($customerNodes->length > 0)
{
    $customer = new FsprgCustomer();

    $customer->firstName = $customerNodes->item(0)->getElementsByTagName("firstName")->item(0)->nodeValue;
    $customer->lastName = $customerNodes->item(0)->getElementsByTagName("lastName")->item(0)->nodeValue;
    $customer->company = $customerNodes->item(0)->getElementsByTagName("company")->item(0)->nodeValue;
    $customer->email = $customerNodes->item(0)->getElementsByTagName("email")->item(0)->nodeValue;
    $customer->phoneNumber = $customerNodes->item(0)->getElementsByTagName("phoneNumber")->item(0)->nodeValue;

    $order->customer = $customer;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.