开发者

Use PHP to parse XML

Here's an example of my xml data

<GetSendStatisticsResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<GetSendStatisticsResult>
<SendDataPoints>
  <member>
    <DeliveryAttempts>69</DeliveryAttempts>
    <Timestamp>2011-09-12T01:00:00Z</Timestamp>
    <Rejects>0</Rejects>
    <Bounces>0</Bounces>
    <Complaints>0</Complaints>
  </member>
  <member>
    <DeliveryAttempts>1</DeliveryAttempts>
    <Timestamp>2011-09-08T17:00:00Z</Timestamp>
    <Rejects>0</Rejects>
    <Bounces>0</Bounces>
    <Complaints>0</Complaints>
  </member>
  <member>
    <DeliveryAttempts>282</DeliveryAttempts>
    <Timestamp>2011-09-09T18:00:00Z</Timestamp>
    <Rejects>0</Rejects>
    <Bounces>0</Bounces>
    <Complaints>0</Complaints>
  </member>

And here's my code:

$xml = simplexml_load_string($resp);
$result = $xml->xpath('//member');
foreach($result->member as $member)
    {
    echo "<p>";
    echo "Delivery Attempts: ".$member->DeliveryAttempts."<br/>";
    echo "</p>";
    }

But it's failing to work. I'm also OK if $xml is converted into JSON or an Array. Whatever works best to 开发者_高级运维iterate through the xml to display DeliveryAttempts.


This is due to the XML namespace. Do something like this:

$xml->registerXPathNamespace( 'ses', 'http://ses.amazonaws.com/doc/2010-12-01/' );
$result = $x->xpath( '//ses:member' ) ;


Try

foreach ($result as $member) {
   ...
}

instead.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜