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

我想掌握所有新订单通知标签

我试过了,但是每次都返回一个空的array()。

$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摘录(只是开始)
<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>

最佳答案

问题可能是默认 namespace 。看

  • SimpleXMLElement::registerXPathNamespace
    为下一个XPath查询
  • 创建一个前缀/ns上下文

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

    或者,如果没有其他 namespace ,只需使用以下命令删除默认 namespace
    str_replace('xmlns="http://checkout.google.com/schema/2"', '', $raw_xml);
    

    在将XML馈送到您的SimpleXmlElement之前。

    关于php - SimpleXmlElement和XPath,获得空array(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4024197/

    10-12 02:20