SimpleXmlElement and XPath, getting empty array()

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

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');

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>

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The issue is likely the default namespace. See

Example:

$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);

before feeding the XML to your SimpleXmlElement.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply