By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,545 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

php extracting xml

P: 65
Im using PHP to extract data from an XML response.

for example:

Expand|Select|Wrap|Line Numbers
  1.  
  2.       <UserManageResult>
  3.  
  4.       <ErrorNumber>0</ErrorNumber>
  5.  
  6.       <ErrorMessage/>
  7.  
  8.       <guid>a806368a64ab4cd3a4fef3572e0f2276</guid>
  9.  
  10.       </UserManageResult>
  11.  
i would use this code to get the guid value. Which is working fine.

Expand|Select|Wrap|Line Numbers
  1.       $xml = new SimpleXMLElement($result);
  2.  
  3.       $guid = $xml->guid;
  4.  
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

Expand|Select|Wrap|Line Numbers
  1.  
  2.       <Orders TransactionStatus="success" AccountId="504158">
  3.  
  4.       −
  5.  
  6.       <Order Id="641756" Description="1 x 17420 Joseph Campau St" DateOrdered="08-Apr-2010">
  7.  
  8.       −
  9.  
  10.       <Pricing Currency="GBP">
  11.  
  12.       <CurrencyPrice>8.70</CurrencyPrice>
  13.  
  14.       <CurrencyPriceTax>1.30</CurrencyPriceTax>
  15.  
Apr 8 '10 #1
Share this Question
Share on Google+
2 Replies


P: 65
Hi All,

I think my solution lies with regex. Can anybody help me understand how to extract the order id?
Apr 8 '10 #2

Atli
Expert 5K+
P: 5,058
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:
Expand|Select|Wrap|Line Numbers
  1. <root>
  2.     <Orders TransactionStatus="success" AccountId="504158">
  3.         <Order Id="641756" Description="1 x 17420 Joseph Campau St" DateOrdered="08-Apr-2010">
  4.             <Pricing Currency="GBP" />
  5.             <CurrencyPrice>8.70</CurrencyPrice>
  6.             <CurrencyPriceTax>1.30</CurrencyPriceTax>
  7.         </Order>
  8.     </Orders>
  9. </root>
This should give you the Id of the first <Order> node.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $xml_obj = simplexml_load_file("http://example.com/myFile.xml");
  3. $orderID = $xml_obj->Orders->Order['Id'];
  4. echo $orderID; 
  5. ?>
Apr 8 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.