2011-08-01T08:24:55.384Z [Ack] => Success" />
开发者

php, simplexml, foreach

开发者 https://www.devze.com 2023-03-24 10:44 出处:网络
How can I get all \"CommentingUser\" ? SimpleXMLElement Object ( 1679625884 => 2011-08-01T08:24:55.384Z [Ack] => Success

How can I get all "CommentingUser" ?

SimpleXMLElement Object
(
    1679625884 => 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>`


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

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


Try with:

foreach ($xmlResponse -> FeedbackDetailArray -> FeedbackDetail as $row)
{
    echo $row -> 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;
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号