user1556695
user1556695

Reputation: 85

Retrieve a value from a SimpleXML object

Excuse me for asking such a "noob" question, but I have been trying to find a simple example of how to loop through my returned xml for the past few hours and I'm getting no where. I just want to be able to cycle through the xml and pull the 'Amount' attribute and 'AmazonOrderId' attribute for each Order. I don't know how to loop, nor how to grab the pertinent data.

SimpleXMLElement Object
(
    [ListOrdersResult] => SimpleXMLElement Object
        (
            [Orders] => SimpleXMLElement Object
                (
                    [Order] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [ShipmentServiceLevelCategory] => SecondDay
                                    [OrderTotal] => SimpleXMLElement Object
                                        (
                                            [Amount] => 5.93
                                            [CurrencyCode] => USD
                                        )

                                    [SellerOrderId] => 107-1261608-7067458
                                    [FulfillmentChannel] => AFN
                                    [BuyerEmail] => [email protected]
                                    [OrderStatus] => Shipped
                                    [BuyerName] => Derrick D Vann
                                    [ShipServiceLevel] => SecondDay
                                    [LastUpdateDate] => 2013-03-13T02:20:31Z
                                    [PurchaseDate] => 2013-03-11T06:14:40Z
                                    [NumberOfItemsUnshipped] => 0
                                    [MarketplaceId] => ATVPDKIKX0DER
                                    [SalesChannel] => Amazon.com
                                    [ShippingAddress] => SimpleXMLElement Object
                                        (
                                            [Phone] => 202 746-2567
                                            [PostalCode] => 20001-4040
                                            [Name] => Derrick Vann
                                            [CountryCode] => US
                                            [StateOrRegion] => DC
                                            [AddressLine1] => 2120 Vermont Ave NW Apt 117
                                            [City] => Washington
                                        )

                                    [NumberOfItemsShipped] => 1
                                    [AmazonOrderId] => 107-1261608-7067458
                                    [PaymentMethod] => Other
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [ShipmentServiceLevelCategory] => Expedited
                                    [OrderTotal] => SimpleXMLElement Object
                                        (
                                            [Amount] => 23.30
                                            [CurrencyCode] => USD
                                        )

                                    [SellerOrderId] => 104-9066827-4446667
                                    [FulfillmentChannel] => AFN
                                    [BuyerEmail] => [email protected]
                                    [OrderStatus] => Shipped
                                    [BuyerName] => Quoc Bui
                                    [ShipServiceLevel] => Expedited
                                    [LastUpdateDate] => 2013-03-13T09:34:26Z
                                    [PurchaseDate] => 2013-03-11T08:07:13Z
                                    [NumberOfItemsUnshipped] => 0
                                    [MarketplaceId] => ATVPDKIKX0DER
                                    [SalesChannel] => Amazon.com
                                    [ShippingAddress] => SimpleXMLElement Object
                                        (
                                            [Phone] => (02) 9560 3639
                                            [PostalCode] => 2204
                                            [Name] => Quoc Minh Bui
                                            [CountryCode] => AU
                                            [StateOrRegion] => New South Wales
                                            [AddressLine1] => 19 Centennial St
                                            [City] => Marrickville
                                        )

                                    [NumberOfItemsShipped] => 1
                                    [AmazonOrderId] => 104-9066827-4446667
                                    [PaymentMethod] => Other
                                )
...

Upvotes: 0

Views: 244

Answers (2)

hakre
hakre

Reputation: 198214

pull the 'Amount' attribute and 'AmazonOrderId' attribute for each Order.

If I take you word-by-word, then this is what you're looking for:

$allThoseAttributes = $xml->xpath('//Order/@Amount|//Order/@AmazonOrderId');

However, more likely you're looking for:

$orders = [];
foreach ($xml->xpath('//Order') as $order) {
    $orders[] = [
        'amazon' => (string)  $order->AmazonOrderId
        'total'  => (string)  $order->OrderTotal->Amount;
    ];
}

because what you call attributes are infact elements. If you want to learn more about XML and Xpath, this is a good read: XPath

Upvotes: 0

Brandon - Free Palestine
Brandon - Free Palestine

Reputation: 16676

SimpleXML is just great, isn't it?

foreach ($xml->ListOrdersResult->Orders->Order as $order) {
    $amazonOrderId = (string) $order->AmazonOrderId;
    $orderTotal    = (string) $order->OrderTotal->Amount;
}

I use (string) type casting because you'd get SimpleXMLElements back for those scalar values otherwise.

Upvotes: 3

Related Questions