SimpleXmlElement和XPath,获得空array() [英] SimpleXmlElement and XPath, getting empty 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="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>
推荐答案
问题很可能是默认名称空间.见
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', 'http://checkout.google.com/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="http://checkout.google.com/schema/2"', '', $raw_xml);
在将XML馈送到您的SimpleXmlElement之前.
before feeding the XML to your SimpleXmlElement.
这篇关于SimpleXmlElement和XPath,获得空array()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!