SimpleXmlElement和XPath,获得空array()

编程入门 行业动态 更新时间:2024-10-26 06:26:07
本文介绍了SimpleXmlElement和XPath,获得空array()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am having a little trouble with parsing XML from a google checkout response. The XML is coming straight from the google server so there is no problem with the XML itself.

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

I want to get hold of all the new-order-notification tags

我尝试了这个,但是每次返回一个空的array().

I tried this but get an empty array() returned everytime.

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

An XML snipet (Just the beginning)

<notification-history-response xmlns="checkout.google/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>

推荐答案

问题很可能是默认名称空间.见

The issue is likely the default namespace. See

  • SimpleXMLElement::registerXPathNamespace 为下一个XPath查询创建一个前缀/ns上下文
  • SimpleXMLElement::registerXPathNamespace Creates a prefix/ns context for the next XPath query

示例:

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

作为替代方案,如果没有其他名称空间,只需使用以下方式删除默认名称空间

As an alternative if there is no other namespaces, simply remove the default namespace with

str_replace('xmlns="checkout.google/schema/2"', '', $raw_xml);

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

before feeding the XML to your SimpleXmlElement.

更多推荐

SimpleXmlElement和XPath,获得空array()

本文发布于:2023-11-15 22:52:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SimpleXmlElement   XPath   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!