如何在 PHP 中正确解析 SimpleXML 对象?

发布于 2024-08-23 03:35:24 字数 2762 浏览 11 评论 0原文

我正在使用 CloudFusion 类来获取 Amazon.com 数据,我的代码很简单:

$items = $pas->item_search( "shoes", array( 
    "ResponseGroup" => "Small", 
    "SearchIndex" => "Blended" ));
$items = $items->body->Items;
echo "<pre>";
print_r( $items );
echo "</pre>";

这会返回以下内容:

SimpleXMLElement Object (
    [Request] => SimpleXMLElement Object
        (
            [IsValid] => True
            [ItemSearchRequest] => SimpleXMLElement Object
                (
                    [Keywords] => shoes
                    [ResponseGroup] => Small
                    [SearchIndex] => Blended
                )

        )

    [TotalResults] => 737435
    [TotalPages] => 245816
    [SearchResultsMap] => SimpleXMLElement Object
        (
            [SearchIndex] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [IndexName] => Kitchen
                    ....
        )

    [Item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [ASIN] => B0001Z95QY
                    [DetailPageURL] => http://www.amazon.com/Household-Essentials-MS6030-Seasonal-Storage/dp/B0001Z95QY%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0001Z95QY
                    [ItemLinks] => SimpleXMLElement Object
                        (
                            [ItemLink] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [Description] => Technical Details
                                            [URL] => http://www.amazon.com/Household-Essentials-MS6030-Seasonal-Storage/dp/tech-data/B0001Z95QY%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0001Z95QY
                                        ) ....................
                )

            [1] => SimpleXMLElement Object
                (
                    [ASIN] => B001ACNBZ8
                    [DetailPageURL] => http://www.amazon.com/Peet-Shoe-Dryer-Boot-Original/dp/B001ACNBZ8%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB001ACNBZ8
                    [ItemLinks] => SimpleXMLElement Object
                        (...................
        )

)

我想要做的是深入到“Item”级别,然后运行 ​​foreach 来获取每个单独的条目。我尝试了 $items = $items->Item,但这仅返回第一个条目。

有什么想法吗?

I'm using the CloudFusion class to get Amazon.com data and my code is simple:

$items = $pas->item_search( "shoes", array( 
    "ResponseGroup" => "Small", 
    "SearchIndex" => "Blended" ));
$items = $items->body->Items;
echo "<pre>";
print_r( $items );
echo "</pre>";

This returns the following:

SimpleXMLElement Object (
    [Request] => SimpleXMLElement Object
        (
            [IsValid] => True
            [ItemSearchRequest] => SimpleXMLElement Object
                (
                    [Keywords] => shoes
                    [ResponseGroup] => Small
                    [SearchIndex] => Blended
                )

        )

    [TotalResults] => 737435
    [TotalPages] => 245816
    [SearchResultsMap] => SimpleXMLElement Object
        (
            [SearchIndex] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [IndexName] => Kitchen
                    ....
        )

    [Item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [ASIN] => B0001Z95QY
                    [DetailPageURL] => http://www.amazon.com/Household-Essentials-MS6030-Seasonal-Storage/dp/B0001Z95QY%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0001Z95QY
                    [ItemLinks] => SimpleXMLElement Object
                        (
                            [ItemLink] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [Description] => Technical Details
                                            [URL] => http://www.amazon.com/Household-Essentials-MS6030-Seasonal-Storage/dp/tech-data/B0001Z95QY%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0001Z95QY
                                        ) ....................
                )

            [1] => SimpleXMLElement Object
                (
                    [ASIN] => B001ACNBZ8
                    [DetailPageURL] => http://www.amazon.com/Peet-Shoe-Dryer-Boot-Original/dp/B001ACNBZ8%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB001ACNBZ8
                    [ItemLinks] => SimpleXMLElement Object
                        (...................
        )

)

What I'd like to do is get down to the "Item" level, then run a foreach to get each individual entry. I tried $items = $items->Item, but this returns only the first entry.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-08-30 03:35:24

首先,您应该避免在 SimpleXMLElement 上使用 print_r(),而只需使用 asXML() 查看 XML。这也是您应该在此处发布的内容,而不是 print_r() 的输出。

我无法破译您发布的代码,因此我会进行大胆猜测并建议您尝试以下操作:

foreach ($items->body->Items->Item as $Item)
{
}

无论如何,如果您想迭代某些内容, foreach 就是答案。

First of all, you should avoid using print_r() on SimpleXMLElement, instead just take a look at the XML using asXML(). That's also what you should post here, instead of print_r()'s output.

I can't decipher the code you have posted so I'll take a wild guess and suggest that you try something like:

foreach ($items->body->Items->Item as $Item)
{
}

At any rate, if you want to iterate over something, foreach is the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文