2010-10-26 38 views
9

我在解析來自Google Checkout響應中的XML時遇到了一些問題。 XML直接來自於谷歌服務器,所以XML本身沒有問題。SimpleXmlElement和XPath,獲取空數組()

我想獲得的所有新進訂單通知標籤

我嘗試這樣的電話,但得到一個空數組()返回每次。

$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> 
+0

* XML直接來自谷歌服務器,所以XML本身沒有問題。*是的,谷歌在這方面幾乎就像@Jon Skeet。根據定義,它們的XML格式良好。 ;-) – LarsH 2010-10-26 21:16:34

回答

14

的問題很可能是默認的命名空間。見

實施例:

$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之前,請使用

+4

+1很好的解釋和實際的例子。夥計們,請花點時間瞭解命名空間。他們並不那麼辛苦,從長遠來看,理解它們比持續試圖躲避它們更容易。 – LarsH 2010-10-26 21:18:28