Accesing XML values

Hello!

I have been struggling trying to access the values in an XML object, I am not able to get to any of the values and tried it on many different ways until finally I decided to come asking for help, this is what the XML looks like


<?xml version="1.0" encoding="UTF-8"?>
<new-order-notification xmlns="http://checkout.google.com/schema/2" serial-number="1370518434546768187747-00001-7">
  <buyer-billing-address>
    <email>jkdchjskd.el@some.com</email>
    <contact-name>some info</contact-name>
    <company-name></company-name>
    <address1>direccion</address1>
    <address2></address2>
    <phone>5558765789075774</phone>
    <fax></fax>
    <structured-name>
      <first-name>some</first-name>
      <last-name>info</last-name>
    </structured-name>
    <country-code>MX</country-code>
    <city>some city</city>
    <region>MEX</region>
    <postal-code>54760</postal-code>
  </buyer-billing-address>
  <timestamp>2012-05-16T01:12:45.991Z</timestamp>
  <google-order-number>137045678951868187747</google-order-number>
  <order-summary>
    <total-chargeback-amount currency="USD">0.0</total-chargeback-amount>
    <google-order-number>137051345678868187747</google-order-number>
    <total-charge-amount currency="USD">0.0</total-charge-amount>
    <total-refund-amount currency="USD">0.0</total-refund-amount>
    <purchase-date>2012-05-16T01:12:45.000Z</purchase-date>
    <archived>false</archived>
    <shopping-cart>
      <items>
        <item>
          <item-name>Sample DVD 6</item-name>
          <item-description></item-description>
          <unit-price currency="USD">13.49</unit-price>
          <quantity>1</quantity>
        </item>
      </items>
    </shopping-cart>
    <order-adjustment>
      <merchant-codes />
      <total-tax currency="USD">0.0</total-tax>
      <adjustment-total currency="USD">0.0</adjustment-total>
    </order-adjustment>
    <buyer-id>619215jh985484704</buyer-id>
    <buyer-shipping-address>
      <email>some@xjhkhlkh.com</email>
      <contact-name>some info</contact-name>
      <company-name></company-name>
      <address1>direccion</address1>
      <address2></address2>
      <phone>55456475774</phone>
      <fax></fax>
      <structured-name>
        <first-name>some</first-name>
        <last-name>info</last-name>
      </structured-name>
      <country-code>MX</country-code>
      <city>some city</city>
      <region>MEX</region>
      <postal-code>54760</postal-code>
    </buyer-shipping-address>
    <buyer-marketing-preferences>
      <email-allowed>false</email-allowed>
    </buyer-marketing-preferences>
    <promotions />
    <order-total currency="USD">13.49</order-total>
    <fulfillment-order-state>NEW</fulfillment-order-state>
    <financial-order-state>REVIEWING</financial-order-state>
  </order-summary>
  <shopping-cart>
    <items>
      <item>
        <item-name>Sample DVD 6</item-name>
        <item-description></item-description>
        <unit-price currency="USD">13.49</unit-price>
        <quantity>1</quantity>
      </item>
    </items>
  </shopping-cart>
  <order-adjustment>
    <merchant-codes />
    <total-tax currency="USD">0.0</total-tax>
    <adjustment-total currency="USD">0.0</adjustment-total>
  </order-adjustment>
  <buyer-id>6192159hj85484704</buyer-id>
  <buyer-shipping-address>
    <email>someemail@xxxxxxx.com</email>
    <contact-name>some info</contact-name>
    <company-name></company-name>
    <address1>direccion</address1>
    <address2></address2>
    <phone>5558775774</phone>
    <fax></fax>
    <structured-name>
      <first-name>some</first-name>
      <last-name>info</last-name>
    </structured-name>
    <country-code>MX</country-code>
    <city>some city</city>
    <region>MEX</region>
    <postal-code>54760</postal-code>
  </buyer-shipping-address>
  <buyer-marketing-preferences>
    <email-allowed>false</email-allowed>
  </buyer-marketing-preferences>
  <promotions />
  <order-total currency="USD">13.49</order-total>
  <fulfillment-order-state>NEW</fulfillment-order-state>
  <financial-order-state>REVIEWING</financial-order-state>
</new-order-notification>

Basically it is from the Google checkout and I can get to the file and read it, even save it into a new file and get only the order number, other than that I can’t get to any of the values, here is how I’m doing it


$xml = simplexml_load_string( $data );
//$xml[0]['serial-number'] this does give me the serial number

So I can get the serial number, the rest of the values are unreachable for me I tried doing $xml->nameofavalue $xml[‘nameofavalue’] $xml->nameofagroup->nameofavalue $xml->nameofagroup->nameofavalue also the same but with $xml[0], doing


foreach($xml as $value)

does print the values that are not inside a group but I need to know the name of those values to be able to handle them accordingly so I need to access them based on their name, please help!!!

The problem is all the hyphens you have in the XML element names, because of them you need to use different code to manage the XML. See the below example.

$xml = simplexml_load_file('test.xml');

// Dump the value of the "buyer-billing-address" index
echo '<pre>';
print_r($xml->{'buyer-billing-address'});
echo '</pre>';

// Display the buyers email address
echo $xml->{'buyer-billing-address'}->email;

Thank you so much I really had problems with this, that definitely worked!!

For what it’s worth, there is an example (currently Example #3) detailing just such a case in the Basic SimpleXML usage chapter of the PHP manual. (: