0

请原谅我问了这样一个“菜鸟”的问题,但我一直试图找到一个简单的例子来说明如何在过去几个小时内循环遍历我返回的 xml,但我无处可去。我只想能够循环浏览 xml 并为每个订单提取“金额”属性和“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
                                )
...
4

2 回答 2

3

SimpleXML 很棒,不是吗?

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

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

于 2013-03-14T00:34:22.617 回答
0

拉取每个订单的“金额”属性和“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

于 2013-03-14T00:57:41.160 回答