SimpleXmlElement和XPath,获取空数组()

时间:2010-10-26 13:34:38

标签: php xpath simplexml xml-namespaces

我在从Google Checkout响应中解析XML时遇到了一些麻烦。 XML直接来自谷歌服务器,因此XML本身没有问题。

我希望获得所有新订单通知标记

我试过这个,但每次都会返回一个空数组()。

$xml = new SimpleXmlElement($raw_xml);
$notifications = $xml->xpath('notifications');
$notifications = $xml->xpath('/notification-history-response/notifications/new-order-notification');
$notifications = $xml->xpath('//new-order-notification');

XML snipet(刚刚开始)

<notification-history-response xmlns="http://checkout.google.com/schema/2" serial-number="c5cda190-0eb1-4f91-87cd-e656e5598d38">
  <notifications>
    <new-order-notification serial-number="271578974677716-00001-7">
      <buyer-billing-address>
        <address1>19 sandbox st</address1>
        <address2></address2>

1 个答案:

答案 0 :(得分:19)

问题可能是默认命名空间。参见

示例:

$sxe->registerXPathNamespace('x', 'http://checkout.google.com/schema/2');
$result = $sxe->xpath('//x:notifications');

作为替代方案,如果没有其他名称空间,只需使用

删除默认名称空间
str_replace('xmlns="http://checkout.google.com/schema/2"', '', $raw_xml);

在将XML提供给SimpleXmlElement之前。