使用PHP来解析XML

时间:2011-09-12 16:54:53

标签: php amazon-ses

以下是我的xml数据的示例

<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>

这是我的代码:

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

但它没有奏效。如果将$ xml转换为JSON或Array,我也没关系。无论什么最适合迭代xml来显示DeliveryAttempts。

2 个答案:

答案 0 :(得分:0)

尝试

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

代替。

答案 1 :(得分:0)

这是由XML命名空间引起的。做这样的事情:

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