php、simplexml、foreach
我怎样才能获得所有“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来您在 xml 对象中引用了错误的元素,
试试这个:
It looks like you refer to the wrong element inside the xml object,
try this:
尝试使用:
Try with:
好吧,如果你的 XML 看起来像这样(我不太理解上面代码的结构):
你可以这样读:
Well, if your XML looks something like this (I don't quite understand the structure from the above code):
you can read it like that:
如果您只需要 CommentingUser 元素,您还可以执行 xpath 查询来获取它们。
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.
http://www.php.net/manual/en/simplexmlelement.xpath.php