我在解析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()
发布评论