php,simplexml,foreach

时间:2011-08-01 08:29:45

标签: php simplexml

如何获得所有“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>`

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

尝试:

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

答案 2 :(得分:0)

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

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

你可以这样读:

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

答案 3 :(得分:0)

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

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

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