从SimpleXML对象中检索值



请原谅我问了这样一个"新手"问题,但是在过去的几个小时里,我一直试图找到一个简单的例子来说明如何循环遍历我返回的xml,但是我一无所获。我只是希望能够循环遍历xml并为每个订单提取"Amount"属性和"AmazonOrderId"属性。我不知道如何循环,也不知道如何获取相关数据。

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] => 5qhs64ktb88pdsj@marketplace.amazon.com
                                    [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] => 6kfc88nrsnm83fq@marketplace.amazon.com
                                    [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
                                )
...

SimpleXML真是太棒了,不是吗?

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

我使用(string)类型转换,因为你会得到SimpleXMLElement s返回那些标量值。

为每个订单提取'Amount'属性和'AmazonOrderId'属性。

如果我逐字逐句地告诉你,那么这就是你要找的:

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

然而,更有可能你要找的是:

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

因为你所谓的属性实际上是元素。如果您想了解更多关于XML和Xpath的知识,这是一本很好的读物:Xpath

最新更新