php extracting xml

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whitep8
    New Member
    • Oct 2009
    • 65

    php extracting xml

    Im using PHP to extract data from an XML response.

    for example:

    Code:
       
          <UserManageResult>
       
          <ErrorNumber>0</ErrorNumber>
       
          <ErrorMessage/>
       
          <guid>a806368a64ab4cd3a4fef3572e0f2276</guid>
       
          </UserManageResult>
    i would use this code to get the guid value. Which is working fine.

    Code:
          $xml = new SimpleXMLElement($result);
      
          $guid = $xml->guid;
    my question is, how do i extract the value of id from within Order from this response. I only need the id, nothing else, and this will be a single response

    Code:
       
          <Orders TransactionStatus="success" AccountId="504158">
       
          −
       
          <Order Id="641756" Description="1 x 17420 Joseph Campau St" DateOrdered="08-Apr-2010">
       
          −
       
          <Pricing Currency="GBP">
       
          <CurrencyPrice>8.70</CurrencyPrice>
       
          <CurrencyPriceTax>1.30</CurrencyPriceTax>
  • whitep8
    New Member
    • Oct 2009
    • 65

    #2
    Hi All,

    I think my solution lies with regex. Can anybody help me understand how to extract the order id?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hey.

      You should avoid using Regular Expressions on XML (and HTML). It's generally better to use a DOM parser (like SimpleXML).

      Assuming the XML you posted was meant to look more like this:
      [code=xml]<root>
      <Orders TransactionStat us="success" AccountId="5041 58">
      <Order Id="641756" Description="1 x 17420 Joseph Campau St" DateOrdered="08-Apr-2010">
      <Pricing Currency="GBP" />
      <CurrencyPrice> 8.70</CurrencyPrice>
      <CurrencyPriceT ax>1.30</CurrencyPriceTa x>
      </Order>
      </Orders>
      </root>[/code]

      This should give you the Id of the first <Order> node.
      [code=php]<?php
      $xml_obj = simplexml_load_ file("http://example.com/myFile.xml");
      $orderID = $xml_obj->Orders->Order['Id'];
      echo $orderID;
      ?>[/code]

      Comment

      Working...