php、simplexml、foreach

发布于 2024-11-27 01:27:37 字数 5480 浏览 1 评论 0原文

我怎样才能获得所有“CommentingUser”?

SimpleXMLElement Object
(
    [Timestamp] => 2011-08-01T08:24:55.384Z
    [Ack] => Success
    [Version] => 731
    [Build] => E731_INTL_BUNDLED_13551333_R1
    [FeedbackDetailArray] => SimpleXMLElement Object
        (
            [FeedbackDetail] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [CommentingUser] => kerryd53
                            [CommentingUserScore] => 124
                            [CommentText] => wonderful book
                            [CommentTime] => 2011-08-01T08:02:20.000Z
                            [CommentType] => Positive
                            [ItemID] => 120395264453
                            [Role] => Seller
                            [FeedbackID] => 540466592022
                            [TransactionID] => 740714734002
                            [OrderLineItemID] => 120395264453-740714734002
                            [ItemTitle] => NEW 30 Days to Taming Your Tongue Workbook - Pegues,...
                            [ItemPrice] => 5.99
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [CommentingUser] => freddiec29
                            [CommentingUserScore] => 377
                            [CommentText] => fast shipping!
                            [CommentTime] => 2011-08-01T07:32:17.000Z
                            [CommentType] => Positive
                            [ItemID] => 150616990042
                            [Role] => Seller
                            [FeedbackID] => 540461424022
                            [TransactionID] => 621117682005
                            [OrderLineItemID] => 150616990042-621117682005
                            [ItemTitle] => NEW Rediform® Desk Pad/Wall Calendar, Chipboard, 21-3/4
                            [ItemPrice] => 5.99
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [CommentingUser] => agustinagustin44
                            [CommentingUserScore] => 14
                            [CommentText] => great buy!
                            [CommentTime] => 2011-08-01T07:27:34.000Z
                            [CommentType] => Positive
                            [ItemID] => 150598601980
                            [Role] => Seller
                            [FeedbackID] => 540460633022
                            [TransactionID] => 619873682005
                            [OrderLineItemID] => 150598601980-619873682005
                            [ItemTitle] => NEW Gundam Seed Anime Legends Collection 1
                            [ItemPrice] => 31.99
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [CommentingUser] => sollord
                            [CommentingUserScore] => 22
                            [CommentText] => Fast and accurate
                            [CommentTime] => 2011-08-01T07:20:00.000Z
                            [CommentType] => Positive
                            [ItemID] => 400222164187
                            [Role] => Seller
                            [FeedbackID] => 540459409022
                            [TransactionID] => 121359761027
                            [OrderLineItemID] => 400222164187-121359761027
                            [ItemTitle] => NEW Belkin PureAV® PF30 Home Theater Power Console
                            [ItemPrice] => 59.99
                        )



        )

)

我尝试了不同的技巧,但它们不起作用。

  $xmlResponse = simplexml_load_string($output);
print_r($xmlResponse);
  $totalPages =  $xmlResponse->PaginationResult->TotalNumberOfPages;
echo "there are $totalPages pages";

  $feedback = $xmlResponse->FeedbackDetailArray;
print_r($feedback);

  foreach($feedback as $rows)
   {

 $username = $rows->FeedbackDetail;
 echo $username->CommentingUser;
}

xml 看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
`<GetFeedbackResponse xmlns="urn:ebay:apis:eBLBaseComponents"><Timestamp>2011-08-01T08:39:00.631Z</Timestamp><Ack>Success</Ack><Version>731</Version>` `<Build>E731_INTL_BUNDLED_13551333_R1</Build><FeedbackDetailArray><FeedbackDetail><CommentingUser>amangi-machque</CommentingUser>`<CommentingUserScore>141</CommentingUserScore><CommentText>YAAAAAA!</CommentText><CommentTime>2011-08-01T08:38:16.000Z</CommentTime><CommentType>Positive</CommentType><ItemID>120420154018</ItemID><Role>Seller</Role><FeedbackID>540473547022</FeedbackID><TransactionID>744712455002</TransactionID><OrderLineItemID>120420154018-744712455002</OrderLineItemID><ItemTitle>NEW Keyspan High Speed USB Serial Adapter USA-19HS</ItemTitle><ItemPrice currencyID="USD">29.6</ItemPrice></FeedbackDetail><FeedbackDetail><CommentingUser>kerryd53</CommentingUser><CommentingUserScore>124</CommentingUserScore>`

How can I get all "CommentingUser" ?

SimpleXMLElement Object
(
    [Timestamp] => 2011-08-01T08:24:55.384Z
    [Ack] => Success
    [Version] => 731
    [Build] => E731_INTL_BUNDLED_13551333_R1
    [FeedbackDetailArray] => SimpleXMLElement Object
        (
            [FeedbackDetail] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [CommentingUser] => kerryd53
                            [CommentingUserScore] => 124
                            [CommentText] => wonderful book
                            [CommentTime] => 2011-08-01T08:02:20.000Z
                            [CommentType] => Positive
                            [ItemID] => 120395264453
                            [Role] => Seller
                            [FeedbackID] => 540466592022
                            [TransactionID] => 740714734002
                            [OrderLineItemID] => 120395264453-740714734002
                            [ItemTitle] => NEW 30 Days to Taming Your Tongue Workbook - Pegues,...
                            [ItemPrice] => 5.99
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [CommentingUser] => freddiec29
                            [CommentingUserScore] => 377
                            [CommentText] => fast shipping!
                            [CommentTime] => 2011-08-01T07:32:17.000Z
                            [CommentType] => Positive
                            [ItemID] => 150616990042
                            [Role] => Seller
                            [FeedbackID] => 540461424022
                            [TransactionID] => 621117682005
                            [OrderLineItemID] => 150616990042-621117682005
                            [ItemTitle] => NEW Rediform® Desk Pad/Wall Calendar, Chipboard, 21-3/4
                            [ItemPrice] => 5.99
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [CommentingUser] => agustinagustin44
                            [CommentingUserScore] => 14
                            [CommentText] => great buy!
                            [CommentTime] => 2011-08-01T07:27:34.000Z
                            [CommentType] => Positive
                            [ItemID] => 150598601980
                            [Role] => Seller
                            [FeedbackID] => 540460633022
                            [TransactionID] => 619873682005
                            [OrderLineItemID] => 150598601980-619873682005
                            [ItemTitle] => NEW Gundam Seed Anime Legends Collection 1
                            [ItemPrice] => 31.99
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [CommentingUser] => sollord
                            [CommentingUserScore] => 22
                            [CommentText] => Fast and accurate
                            [CommentTime] => 2011-08-01T07:20:00.000Z
                            [CommentType] => Positive
                            [ItemID] => 400222164187
                            [Role] => Seller
                            [FeedbackID] => 540459409022
                            [TransactionID] => 121359761027
                            [OrderLineItemID] => 400222164187-121359761027
                            [ItemTitle] => NEW Belkin PureAV® PF30 Home Theater Power Console
                            [ItemPrice] => 59.99
                        )



        )

)

I tried different tricks but they are not working .

  $xmlResponse = simplexml_load_string($output);
print_r($xmlResponse);
  $totalPages =  $xmlResponse->PaginationResult->TotalNumberOfPages;
echo "there are $totalPages pages";

  $feedback = $xmlResponse->FeedbackDetailArray;
print_r($feedback);

  foreach($feedback as $rows)
   {

 $username = $rows->FeedbackDetail;
 echo $username->CommentingUser;
}

The xml looks like this

<?xml version="1.0" encoding="UTF-8"?>
`<GetFeedbackResponse xmlns="urn:ebay:apis:eBLBaseComponents"><Timestamp>2011-08-01T08:39:00.631Z</Timestamp><Ack>Success</Ack><Version>731</Version>` `<Build>E731_INTL_BUNDLED_13551333_R1</Build><FeedbackDetailArray><FeedbackDetail><CommentingUser>amangi-machque</CommentingUser>`<CommentingUserScore>141</CommentingUserScore><CommentText>YAAAAAA!</CommentText><CommentTime>2011-08-01T08:38:16.000Z</CommentTime><CommentType>Positive</CommentType><ItemID>120420154018</ItemID><Role>Seller</Role><FeedbackID>540473547022</FeedbackID><TransactionID>744712455002</TransactionID><OrderLineItemID>120420154018-744712455002</OrderLineItemID><ItemTitle>NEW Keyspan High Speed USB Serial Adapter USA-19HS</ItemTitle><ItemPrice currencyID="USD">29.6</ItemPrice></FeedbackDetail><FeedbackDetail><CommentingUser>kerryd53</CommentingUser><CommentingUserScore>124</CommentingUserScore>`

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

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

发布评论

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

评论(4

神经大条 2024-12-04 01:27:37

看来您在 xml 对象中引用了错误的元素,
试试这个:

$feedback = $xmlResponse->FeedbackDetailArray->FeedbackDetail;
foreach($feedback as $row)

It looks like you refer to the wrong element inside the xml object,
try this:

$feedback = $xmlResponse->FeedbackDetailArray->FeedbackDetail;
foreach($feedback as $row)
岁月静好 2024-12-04 01:27:37

尝试使用:

foreach ($xmlResponse -> FeedbackDetailArray -> FeedbackDetail as $row)
{
    echo $row -> CommentingUser;
}

Try with:

foreach ($xmlResponse -> FeedbackDetailArray -> FeedbackDetail as $row)
{
    echo $row -> CommentingUser;
}
梦中楼上月下 2024-12-04 01:27:37

好吧,如果你的 XML 看起来像这样(我不太理解上面代码的结构):

<comments>
  <comment>
    <commentingUser>user1</commentingUser>
  </comment>
  <comment>
    <commentingUser>user2</commentingUser>
  </comment>
</coments>

你可以这样读:

foreach($xmlResponse->comment as $comment) {
   echo $comment->commentingUser;
}

Well, if your XML looks something like this (I don't quite understand the structure from the above code):

<comments>
  <comment>
    <commentingUser>user1</commentingUser>
  </comment>
  <comment>
    <commentingUser>user2</commentingUser>
  </comment>
</coments>

you can read it like that:

foreach($xmlResponse->comment as $comment) {
   echo $comment->commentingUser;
}
一抹苦笑 2024-12-04 01:27:37

如果您只需要 CommentingUser 元素,您还可以执行 xpath 查询来获取它们。

foreach ($xmlResponse->xpath('CommentingUser') as $user) {
  echo $user;
}

http://www.php.net/manual/en/simplexmlelement.xpath.php

If you're only gonna need the CommentingUser-elements, you can also do an xpath query to just fetch them.

foreach ($xmlResponse->xpath('CommentingUser') as $user) {
  echo $user;
}

http://www.php.net/manual/en/simplexmlelement.xpath.php

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