SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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

    Code XML:
    <?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

    PHP Code:
    $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

    PHP Code:
    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!!!
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    PHP Code:
    $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

  3. #3
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    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.

    PHP Code:
    $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!!
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    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.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •